Class: ROM::Finalize::FinalizeRelations

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/setup/finalize/finalize_relations.rb

Defined Under Namespace

Classes: RegistryReaders

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gateways, relation_classes, notifications:, mappers: nil, plugins: EMPTY_ARRAY) ⇒ FinalizeRelations

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build relation registry of specified descendant classes

This is used by the setup

Parameters:

  • gateways (Hash)
  • relation_classes (Array)

    a list of relation descendants



30
31
32
33
34
35
36
# File 'lib/rom/setup/finalize/finalize_relations.rb', line 30

def initialize(gateways, relation_classes, notifications:, mappers: nil, plugins: EMPTY_ARRAY)
  @gateways = gateways
  @relation_classes = relation_classes
  @mappers = mappers
  @plugins = plugins
  @notifications = notifications
end

Instance Attribute Details

#notificationsObject (readonly)

Returns the value of attribute notifications.



10
11
12
# File 'lib/rom/setup/finalize/finalize_relations.rb', line 10

def notifications
  @notifications
end

Instance Method Details

#build_relation(klass, registry) ⇒ ROM::Relation

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/MethodLength, Metrics/AbcSize

Returns:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rom/setup/finalize/finalize_relations.rb', line 91

def build_relation(klass, registry)
  # TODO: raise a meaningful error here and add spec covering the case
  #       where klass' gateway points to non-existant repo
  gateway = @gateways.fetch(klass.gateway)

  plugins = schema_plugins

  schema = klass.schema_proc.call do
    plugins.each { |plugin| app_plugin(plugin) }
  end

  klass.set_schema!(schema) if klass.schema.nil?

  notifications.trigger(
    'configuration.relations.schema.allocated',
    schema: schema, gateway: gateway, registry: registry
  )

  relation_plugins.each do |plugin|
    plugin.apply_to(klass)
  end

  notifications.trigger(
    'configuration.relations.schema.set',
    schema: schema, relation: klass, registry: registry, adapter: klass.adapter
  )

  rel_key = schema.name.to_sym
  dataset = gateway.dataset(schema.name.dataset).instance_exec(klass, &klass.dataset)

  notifications.trigger(
    'configuration.relations.dataset.allocated',
    dataset: dataset, relation: klass, adapter: klass.adapter
  )

  options = {
    __registry__: registry,
    mappers: mapper_registry(rel_key, klass),
    schema: schema,
    **plugin_options
  }

  klass.new(dataset, **options)
end

#mapper_registry(rel_key, rel_class) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



138
139
140
141
142
143
144
145
146
# File 'lib/rom/setup/finalize/finalize_relations.rb', line 138

def mapper_registry(rel_key, rel_class)
  registry = rel_class.mapper_registry(cache: @mappers.cache)

  if @mappers.key?(rel_key)
    registry.merge(@mappers[rel_key])
  else
    registry
  end
end

#plugin_optionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



149
150
151
# File 'lib/rom/setup/finalize/finalize_relations.rb', line 149

def plugin_options
  relation_plugins.map(&:config).map(&:to_hash).reduce(:merge) || EMPTY_HASH
end

#relation_namesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



164
165
166
# File 'lib/rom/setup/finalize/finalize_relations.rb', line 164

def relation_names
  @relation_classes.map(&:relation_name).map(&:relation).uniq
end

#relation_pluginsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



154
155
156
# File 'lib/rom/setup/finalize/finalize_relations.rb', line 154

def relation_plugins
  @plugins.select { |p| p.type == :relation }
end

#run!Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/AbcSize, Metrics/MethodLength

Returns:

  • (Hash)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rom/setup/finalize/finalize_relations.rb', line 43

def run!
  relation_registry = RelationRegistry.new do |registry, relations|
    registry_readers = RegistryReaders.new(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, readers: registry_readers)

      notifications.trigger(
        'configuration.relations.class.ready',
        relation: klass,
        adapter: klass.adapter
      )

      relations[key] = build_relation(klass, registry)
    end

    registry.each_value 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

#schema_pluginsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



159
160
161
# File 'lib/rom/setup/finalize/finalize_relations.rb', line 159

def schema_plugins
  @plugins.select { |p| p.type == :schema }
end