Sha256: 3c2b6051d23b7db3a7346a86bb53941676bf4261e4b37be1d16b997d37cc42cd

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

require 'rom/relation_registry'

module ROM
  class Finalize
    class FinalizeRelations
      # Build relation registry of specified descendant classes
      #
      # This is used by the setup
      #
      # @param [Hash] gateways
      # @param [Array] relation_classes a list of relation descendants
      #
      # @api private
      def initialize(gateways, relation_classes)
        @gateways = gateways
        @relation_classes = relation_classes
      end

      # @return [Hash]
      #
      # @api private
      def run!
        registry = {}

        @relation_classes.each do |klass|
          # 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)
          ds_proc = klass.dataset_proc || -> { self }
          dataset = gateway.dataset(klass.dataset).instance_exec(&ds_proc)

          relation = klass.new(dataset, __registry__: registry)

          name = klass.register_as

          if registry.key?(name)
            raise RelationAlreadyDefinedError,
              "Relation with `register_as #{name.inspect}` registered more " \
              "than once"
          end

          registry[name] = relation
        end

        registry.each_value do |relation|
          relation.class.finalize(registry, relation)
        end

        RelationRegistry.new(registry)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rom-1.0.0 lib/rom/setup/finalize/relations.rb
rom-1.0.0.rc1 lib/rom/setup/finalize/relations.rb
rom-1.0.0.beta2 lib/rom/setup/finalize/relations.rb
rom-1.0.0.beta1 lib/rom/setup/finalize/relations.rb