Last active
July 18, 2024 08:47
-
-
Save DangerDawson/0eb82f7e61cc791a31af120427f128ac to your computer and use it in GitHub Desktop.
ROM relations optimisation 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ROM | |
class Finalize | |
class FinalizeRelations | |
attr_reader :notifications | |
module BuildRelationReaders | |
def self.build(relations) | |
Module.new do | |
relations.each do |name| | |
define_method(name) do | |
__registry__[name] | |
end | |
end | |
end | |
end | |
end | |
# @return [Hash] | |
# | |
# @api private | |
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize | |
def run! | |
relation_registry = RelationRegistry.new do |registry, relations| | |
relation_readers_module = BuildRelationReaders.build(relation_names) | |
@relation_classes.each do |klass| | |
unless klass.adapter | |
raise MissingAdapterIdentifierError, | |
"Relation class +#{klass}+ is missing the adapter identifier" | |
end | |
key = klass.relation_name.to_sym | |
if registry.key?(key) | |
raise RelationAlreadyDefinedError, | |
"Relation with name #{key.inspect} registered more than once" | |
end | |
klass.use(:registry_reader, klass: klass, relation_readers_module: relation_readers_module) | |
notifications.trigger('configuration.relations.class.ready', relation: klass, adapter: klass.adapter) | |
relations[key] = build_relation(klass, registry) | |
end | |
registry.each do |_, relation| | |
notifications.trigger( | |
'configuration.relations.object.registered', | |
relation: relation, registry: registry | |
) | |
end | |
end | |
notifications.trigger( | |
'configuration.relations.registry.created', registry: relation_registry | |
) | |
relation_registry | |
end | |
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize | |
end | |
end | |
end | |
module ROM | |
module Plugins | |
module Relation | |
# Allows relations to access all other relations through registry | |
# | |
# For now this plugin is always enabled | |
# | |
# @api public | |
class RegistryReader | |
# @api private | |
def initialize(klass:, relation_readers_module:) | |
klass.include relation_readers_module | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment