Module: ROM::Command::ClassInterface

Included in:
ROM::Command
Defined in:
lib/rom/commands/class_interface.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ 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.

Sets up the base class


24
25
26
27
28
# File 'lib/rom/commands/class_interface.rb', line 24

def self.extended(klass)
  super
  klass.set_hooks(:before, [])
  klass.set_hooks(:after, [])
end

Instance Method Details

#[](adapter) ⇒ Class

Return adapter specific sub-class based on the adapter identifier

This is a syntax sugar to make things consistent

Examples:

ROM::Commands::Create[:memory]
# => ROM::Memory::Commands::Create

Parameters:

  • adapter (Symbol)

    identifier

Returns:

  • (Class)

43
44
45
# File 'lib/rom/commands/class_interface.rb', line 43

def [](adapter)
  adapter_namespace(adapter).const_get(Inflector.demodulize(name))
end

#adapter_namespace(adapter) ⇒ Module

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.

Return namespaces that contains command subclasses of a specific adapter

Parameters:

  • adapter (Symbol)

    identifier

Returns:

  • (Module)

54
55
56
57
58
# File 'lib/rom/commands/class_interface.rb', line 54

def adapter_namespace(adapter)
  ROM.adapters.fetch(adapter).const_get(:Commands)
rescue KeyError
  raise AdapterNotPresentError.new(adapter, :relation)
end

#after(hook) ⇒ Array<Hash, Symbol> #after(hook_opts) ⇒ Array<Hash, Symbol>

Set after-execute hooks

Overloads:

  • #after(hook) ⇒ Array<Hash, Symbol>

    Set an after hook as a method name

    Examples:

    class CreateUser < ROM::Commands::Create[:sql]
      relation :users
      register_as :create
    
      after :my_hook
    
      def my_hook(tuple, *)
        puts "hook called#
      end
    end
  • #after(hook_opts) ⇒ Array<Hash, Symbol>

    Set an after hook as a method name with arguments

    Examples:

    class CreateUser < ROM::Commands::Create[:sql]
      relation :users
      register_as :create
    
      after my_hook: { arg1: 1, arg1: 2 }
    
      def my_hook(tuple, arg1:, arg2:)
        puts "hook called with args: #{arg1} and #{arg2}"
      end
    end

    Parameters:

    • hook (Hash<Symbol=>Hash>)

      Options with method name and pre-set args

Returns:

  • (Array<Hash, Symbol>)

    A list of all configured after hooks


211
212
213
214
215
216
217
# File 'lib/rom/commands/class_interface.rb', line 211

def after(*hooks)
  if !hooks.empty?
    set_hooks(:after, hooks)
  else
    @after
  end
end

#before(hook) ⇒ Array<Hash, Symbol> #before(hook_opts) ⇒ Array<Hash, Symbol>

Set before-execute hooks

Overloads:

  • #before(hook) ⇒ Array<Hash, Symbol>

    Set an before hook as a method name

    Examples:

    class CreateUser < ROM::Commands::Create[:sql]
      relation :users
      register_as :create
    
      before :my_hook
    
      def my_hook(tuple, *)
        puts "hook called#
      end
    end
  • #before(hook_opts) ⇒ Array<Hash, Symbol>

    Set an before hook as a method name with arguments

    Examples:

    class CreateUser < ROM::Commands::Create[:sql]
      relation :users
      register_as :create
    
      before my_hook: { arg1: 1, arg2: 2 }
    
      def my_hook(tuple, arg1:, arg2:)
        puts "hook called with args: #{arg1} and #{arg2}"
      end
    end

    Parameters:

    • hook (Hash<Symbol=>Hash>)

      Options with method name and pre-set args

Returns:

  • (Array<Hash, Symbol>)

    A list of all configured before hooks


166
167
168
169
170
171
172
# File 'lib/rom/commands/class_interface.rb', line 166

def before(*hooks)
  if !hooks.empty?
    set_hooks(:before, hooks)
  else
    @before
  end
end

#build(relation, **options) ⇒ Command

Build a command class for a specific relation with options

Examples:

class CreateUser < ROM::Commands::Create[:memory]
end

command = CreateUser.build(rom.relations[:users])

Parameters:

Returns:


74
75
76
# File 'lib/rom/commands/class_interface.rb', line 74

def build(relation, **options)
  new(relation, **self.options, **options)
end

#create_class(name, type, inflector: Inflector) {|Class| ... } ⇒ Class, Object

Create a command class with a specific type

Parameters:

  • name (Symbol)

    Command name

  • type (Class)

    Command class

Yields:

  • (Class)

Returns:

  • (Class, Object)

88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rom/commands/class_interface.rb', line 88

def create_class(name, type, inflector: Inflector, &block)
  klass = Dry::Core::ClassBuilder
    .new(name: "#{inflector.classify(type)}[:#{name}]", parent: type)
    .call

  if block
    yield(klass)
  else
    klass
  end
end

#default_nameSymbol

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.

Return default name of the command class based on its name

During setup phase this is used by defalut as register_as option

Returns:

  • (Symbol)

239
240
241
# File 'lib/rom/commands/class_interface.rb', line 239

def default_name
  Inflector.underscore(Inflector.demodulize(name)).to_sym
end

#extend_for_relation(relation) ⇒ Class

Extend a command class with relation view methods

Parameters:

Returns:

  • (Class)

125
126
127
# File 'lib/rom/commands/class_interface.rb', line 125

def extend_for_relation(relation)
  include(relation_methods_mod(relation.class))
end

#inherited(klass) ⇒ 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.

This hook sets up default class state


15
16
17
18
19
# File 'lib/rom/commands/class_interface.rb', line 15

def inherited(klass)
  super
  klass.instance_variable_set(:'@before', before.dup)
  klass.instance_variable_set(:'@after', after.dup)
end

#optionsHash

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.

Return default options based on class macros

Returns:

  • (Hash)

248
249
250
# File 'lib/rom/commands/class_interface.rb', line 248

def options
  {input: input, result: result, before: before, after: after}
end

#relation_methods_mod(relation_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.


253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/rom/commands/class_interface.rb', line 253

def relation_methods_mod(relation_class)
  Module.new do
    relation_class.view_methods.each do |meth|
      module_eval <<-RUBY, __FILE__, __LINE__ + 1
        def #{meth}(*args)
          response = relation.public_send(:#{meth}, *args)

          if response.is_a?(relation.class)
            new(response)
          else
            response
          end
        end
      RUBY
    end
  end
end

#set_hooks(type, hooks) ⇒ 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.

Set new or more hooks


222
223
224
225
226
227
228
229
230
# File 'lib/rom/commands/class_interface.rb', line 222

def set_hooks(type, hooks)
  ivar = :"@#{type}"

  if instance_variable_defined?(ivar)
    instance_variable_get(ivar).concat(hooks)
  else
    instance_variable_set(ivar, hooks)
  end
end

#use(plugin, **options) ⇒ Object

Use a configured plugin in this relation

Examples:

class CreateUser < ROM::Commands::Create[:memory]
  use :pagintion

  per_page 30
end

Parameters:

  • plugin (Symbol)
  • options (Hash)

Options Hash (**options):

  • :adapter (Symbol) — default: :default

    first adapter to check for plugin


114
115
116
# File 'lib/rom/commands/class_interface.rb', line 114

def use(plugin, **options)
  ROM.plugin_registry[:command].fetch(plugin, adapter).apply_to(self, **options)
end