Skip to content

Commit

Permalink
Revert "Optimise the repository relations reader"
Browse files Browse the repository at this point in the history
This reverts commit efd8da6.
  • Loading branch information
flash-gordon committed Jan 7, 2025
1 parent efd8da6 commit 369a9bb
Showing 1 changed file with 8 additions and 35 deletions.
43 changes: 8 additions & 35 deletions repository/lib/rom/repository/relation_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,28 @@ module ROM
class Repository
# @api private
class RelationReader < ::Module
extend ::Dry::Core::ClassAttributes

# @api private
attr_reader :klass

# @api private
attr_reader :relations

defines :relation_readers

defines :mutex
mutex(Mutex.new)

defines :relation_cache
relation_cache(Concurrent::Hash.new)

module InstanceMethods
# @api private
def set_relation(name) # rubocop:disable Naming/AccessorMethodName
container
.relations[name]
.with(auto_struct: auto_struct, struct_namespace: struct_namespace)
end

def relation_reader(name, relation_cache)
key = [name, auto_struct, struct_namespace]
relation_cache[key] ||= set_relation(name)
.with(auto_struct: auto_struct)
.struct_namespace(struct_namespace)
end
end

# @api private
def mutex
ROM::Repository::RelationReader.mutex
end

# @api private
def initialize(klass, relations)
super()
@klass = klass
@relations = relations
mutex.synchronize do
unless self.class.relation_readers
self.class.relation_readers(
build_relation_readers(relations, self.class.relation_cache)
)
end
end
klass.include self.class.relation_readers
define_readers!
end

# @api private
Expand All @@ -62,12 +37,10 @@ def included(klass)
private

# @api private
def build_relation_readers(relations, relation_cache)
Module.new do
relations.each do |name|
define_method(name) do
relation_reader(name, relation_cache)
end
def define_readers!
relations.each do |name|
define_method(name) do
@relations[name] ||= set_relation(name)
end
end
end
Expand Down

0 comments on commit 369a9bb

Please sign in to comment.