Class: ROM::CommandRegistry

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

Overview

Specialized registry class for commands

Instance Attribute Summary collapse

Attributes inherited from Registry

#cache

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Registry

#each, #fetch, #key?, #map, #merge, new, #to_hash

Methods included from Initializer

extended

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



109
110
111
112
113
114
115
# File 'lib/rom/command_registry.rb', line 109

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

Instance Attribute Details

#compilerCommandCompiler (readonly)

Returns A command compiler instance.

Returns:



32
# File 'lib/rom/command_registry.rb', line 32

option :compiler, optional: true

#mapperObject#call (readonly)

Returns Default mapper for processing command results.

Returns:

  • (Object#call)

    Default mapper for processing command results



28
# File 'lib/rom/command_registry.rb', line 28

option :mapper, optional: true

#mappersMapperRegistry (readonly)

Returns Optional mapper registry.

Returns:



24
# File 'lib/rom/command_registry.rb', line 24

option :mappers, optional: true

#relation_nameRelation::Name (readonly)

Returns The name of a relation.

Returns:



20
# File 'lib/rom/command_registry.rb', line 20

option :relation_name

Class Method Details

.element_not_found_errorObject

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.



35
36
37
# File 'lib/rom/command_registry.rb', line 35

def self.element_not_found_error
  CommandNotFoundError
end

Instance Method Details

#[](*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

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 send through :entity mapper

Parameters:

  • name (Symbol)

    The name of a registered command

Returns:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rom/command_registry.rb', line 57

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

#elementsRegistry

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.

Internal command registry

Returns:



16
# File 'lib/rom/command_registry.rb', line 16

param :elements

#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:



83
84
85
# File 'lib/rom/command_registry.rb', line 83

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

#set_compiler(compiler) ⇒ 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.



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

def set_compiler(compiler)
  options[:compiler] = @compiler = compiler
end

#set_mappers(mappers) ⇒ 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.



93
94
95
# File 'lib/rom/command_registry.rb', line 93

def set_mappers(mappers)
  options[:mappers] = @mappers = mappers
end