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



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

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)


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

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

#adapterSymbol

Return configured adapter identifier

Returns:

  • (Symbol)


117
118
119
# File 'lib/rom/commands/class_interface.rb', line 117

def adapter
  config.component.adapter
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)


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

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



203
204
205
206
207
208
209
# File 'lib/rom/commands/class_interface.rb', line 203

def after(*hooks)
  if hooks.empty?
    @after
  else
    set_hooks(:after, hooks)
  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



158
159
160
161
162
163
164
# File 'lib/rom/commands/class_interface.rb', line 158

def before(*hooks)
  if hooks.empty?
    @before
  else
    set_hooks(:before, hooks)
  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:



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

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

#create_class(type: self, meta: {}, rel_meta: {}, plugins: {}) {|Class| ... } ⇒ Class, Object

Create a command class with a specific type

Parameters:

  • name (Symbol)

    Command name

  • type (Class) (defaults to: self)

    Command class

Yields:

  • (Class)

Returns:

  • (Class, Object)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rom/commands/class_interface.rb', line 87

def create_class(type: self, meta: {}, rel_meta: {}, plugins: {}, **, &block)
  klass = Dry::Core::ClassBuilder.new(name: type.name, parent: type).call

  result = meta.fetch(:result, :one)
  klass.config.result = rel_meta.fetch(:combine_type, result)

  meta.each do |name, value|
    if klass.respond_to?(name)
      klass.public_send(name, value)
    else
      klass.config[name] = value
    end
  end

  plugins.each do |plugin, options|
    klass.use(plugin, **options)
  end

  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)


231
232
233
# File 'lib/rom/commands/class_interface.rb', line 231

def default_name
  Inflector.underscore(Inflector.demodulize(name)).to_sym
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



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

def inherited(klass)
  super
  klass.instance_variable_set(:@before, before.dup)
  klass.instance_variable_set(:@after, after.dup)
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



214
215
216
217
218
219
220
221
222
# File 'lib/rom/commands/class_interface.rb', line 214

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