Class: ROM::CommandRegistry

Inherits:
Registry
  • Object
show all
Defined in:
core/lib/rom/command_registry.rb

Overview

Specialized registry class for commands

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object (private)

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.

Allow retrieving commands using dot-notation



115
116
117
118
119
120
121
# File 'core/lib/rom/command_registry.rb', line 115

def method_missing(name, *)
  if key?(name)
    self[name]
  else
    super
  end
end

Instance Attribute Details

#compilerCommandCompiler (readonly)

Returns A command compiler instance.

Returns:

  • (CommandCompiler)

    A command compiler instance



34
# File 'core/lib/rom/command_registry.rb', line 34

option :compiler, optional: true

#mapperObject#call (readonly)

Returns Default mapper for processing command results.

Returns:

  • (Object#call)

    Default mapper for processing command results



30
# File 'core/lib/rom/command_registry.rb', line 30

option :mapper, optional: true

#mappersMapperRegistry (readonly)

Returns Optional mapper registry.

Returns:



26
# File 'core/lib/rom/command_registry.rb', line 26

option :mappers, optional: true

#relation_nameRelation::Name (readonly)

Returns The name of a relation.

Returns:

  • (Relation::Name)

    The name of a relation



22
# File 'core/lib/rom/command_registry.rb', line 22

option :relation_name

Instance Method Details

#[](name) ⇒ Command, Command::Composite #[](*args) ⇒ Command, Command::Composite

Return a command from the registry

If mapper is set command will be turned into a composite command with auto-mapping

Overloads:

  • #[](name) ⇒ Command, Command::Composite

    Examples:

    create_user = rom.commands[:users][:create]
    create_user[name: 'Jane']
    
    # with mapping, assuming :entity mapper is registered for :users relation
    create_user = rom.commands[:users].map_with(:entity)[:create]
    create_user[name: 'Jane'] # => result is sent through :entity mapper

    Parameters:

    • name (Symbol)

      The command identifier from the registry

  • #[](*args) ⇒ Command, Command::Composite

    Parameters:

    • *args (Array)

      ROM::CommandCompiler arguments

    See Also:

    • ROM::CommandCompiler#call

Returns:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'core/lib/rom/command_registry.rb', line 63

def [](*args)
  if args.size.equal?(1)
    command = super
    mapper = options[:mapper]

    if mapper
      command.curry >> mapper
    else
      command
    end
  else
    cache.fetch_or_store(args.hash) { compiler.(*args) }
  end
end

#map_with(mapper_name) ⇒ CommandRegistry

Specify a mapper that should be used for commands from this registry

Examples:

entity_commands = rom.commands[:users].map_with(:entity)

Parameters:

  • mapper_name (Symbol)

    The name of a registered mapper

Returns:



89
90
91
# File 'core/lib/rom/command_registry.rb', line 89

def map_with(mapper_name)
  with(mapper: mappers[mapper_name])
end