Skip to content

Commit

Permalink
Use cached relations for root relation too
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-gordon committed Jan 8, 2025
1 parent e3667fe commit f1e80f0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
5 changes: 3 additions & 2 deletions repository/lib/rom/repository/class_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ def [](name)
# @option options [Boolean] :auto_struct Enable/Disable auto-struct mapping
#
# @api public
def new(container = nil, **options)
def new(container = nil, root: Undefined, **options)
container ||= options.fetch(:container)

unless self < relation_reader
include relation_reader.new(
relations: container.relations.elements.keys,
cache: container.cache
cache: container.cache,
root: root
)
end

Expand Down
10 changes: 7 additions & 3 deletions repository/lib/rom/repository/relation_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,21 @@ def define_readers_for_relation(cache, name)
end

# @api private
def initialize(relations:, cache:)
def initialize(relations:, cache:, root: Undefined)
super()

add_readers(relations, cache)
add_readers(relations, cache, root)
end

# @api private
def add_readers(relations, cache)
def add_readers(relations, cache, root)
include cache.fetch_or_store(:relation_readers) {
Readers.new(relations)
}

unless Undefined.equal?(root)
define_method(:root) { |**kwargs| public_send(root, **kwargs) }
end
end
end
end
Expand Down
13 changes: 4 additions & 9 deletions repository/lib/rom/repository/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ class Root < Repository
# @return [Symbol]
defines :root

# @!attribute [r] root
# @return [Relation] The root relation
attr_reader :root
# @api public
def self.new(*args, **kwargs)
super(*args, **kwargs, root: root)
end

# Sets descendant root relation
#
Expand All @@ -54,12 +55,6 @@ def self.inherited(klass)
super
klass.root(root)
end

# @see Repository#initialize
def initialize(*, **)
super
@root = prepare_relation(self.class.root)
end
end
end
end

0 comments on commit f1e80f0

Please sign in to comment.