class ROM::Finalize::FinalizeCommands

Attributes

notifications[R]

Public Class Methods

new(relations, gateways, command_classes, notifications) click to toggle source

Build command registry hash for provided relations

@param [RelationRegistry] relations registry @param [Hash] gateways @param [Array] command_classes a list of command subclasses

@api private

# File lib/rom/setup/finalize/finalize_commands.rb, line 19
def initialize(relations, gateways, command_classes, notifications)
  @relations = relations
  @gateways = gateways
  @command_classes = command_classes
  @notifications = notifications
end

Public Instance Methods

run!() click to toggle source

@return [Hash]

@api private

# File lib/rom/setup/finalize/finalize_commands.rb, line 29
def run!
  commands = @command_classes.map do |klass|
    relation = @relations[klass.relation]
    gateway = @gateways[relation.gateway]

    notifications.trigger(
      'configuration.commands.class.before_build',
      command: klass, gateway: gateway, dataset: relation.dataset, adapter: relation.adapter
    )

    klass.extend_for_relation(relation) if klass.restrictable

    klass.build(relation)
  end

  registry = Registry.new
  compiler = CommandCompiler.new(@gateways, @relations, registry, notifications)

  @relations.each do |(name, relation)|
    rel_commands = commands.select { |c| c.relation.name == relation.name }

    rel_commands.each do |command|
      identifier = command.class.register_as || command.class.default_name
      relation.commands.elements[identifier] = command
    end

    relation.commands.set_compiler(compiler)
    relation.commands.set_mappers(relation.mappers)

    registry.elements[name] = relation.commands
  end

  registry
end