Module: ROM::Command::ClassInterface
- Included in:
- ROM::Command
- Defined in:
- lib/rom/commands/class_interface.rb
Class Method Summary collapse
-
.extended(klass) ⇒ Object
private
Sets up the base class.
Instance Method Summary collapse
-
#[](adapter) ⇒ Class
Return adapter specific sub-class based on the adapter identifier.
-
#adapter_namespace(adapter) ⇒ Module
private
Return namespaces that contains command subclasses of a specific adapter.
-
#after(*hooks) ⇒ Array<Hash, Symbol>
Set after-execute hooks.
-
#before(*hooks) ⇒ Array<Hash, Symbol>
Set before-execute hooks.
-
#build(relation, options = EMPTY_HASH) ⇒ Command
Build a command class for a specific relation with options.
-
#create_class(name, type) {|Class| ... } ⇒ Class, Object
Create a command class with a specific type.
-
#default_name ⇒ Symbol
private
Return default name of the command class based on its name.
-
#extend_for_relation(relation) ⇒ Class
Extend a command class with relation view methods.
-
#inherited(klass) ⇒ Object
private
This hook sets up default class state.
-
#options ⇒ Hash
private
Return default options based on class macros.
- #relation_methods_mod(relation_class) ⇒ Object private
-
#set_hooks(type, hooks) ⇒ Object
private
Set new or more hooks.
-
#use(plugin, _options = EMPTY_HASH) ⇒ Object
Use a configured plugin in this relation.
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
22 23 24 25 26 |
# File 'lib/rom/commands/class_interface.rb', line 22 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
41 42 43 |
# File 'lib/rom/commands/class_interface.rb', line 41 def [](adapter) adapter_namespace(adapter).const_get(Dry::Core::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
52 53 54 55 56 |
# File 'lib/rom/commands/class_interface.rb', line 52 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
209 210 211 212 213 214 215 |
# File 'lib/rom/commands/class_interface.rb', line 209 def after(*hooks) if hooks.size > 0 set_hooks(:after, hooks) else @after end end |
#before(hook) ⇒ Array<Hash, Symbol> #before(hook_opts) ⇒ Array<Hash, Symbol>
Set before-execute hooks
164 165 166 167 168 169 170 |
# File 'lib/rom/commands/class_interface.rb', line 164 def before(*hooks) if hooks.size > 0 set_hooks(:before, hooks) else @before end end |
#build(relation, options = EMPTY_HASH) ⇒ Command
Build a command class for a specific relation with options
72 73 74 |
# File 'lib/rom/commands/class_interface.rb', line 72 def build(relation, = EMPTY_HASH) new(relation, self..merge()) end |
#create_class(name, type) {|Class| ... } ⇒ Class, Object
Create a command class with a specific type
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rom/commands/class_interface.rb', line 86 def create_class(name, type, &block) klass = Dry::Core::ClassBuilder .new(name: "#{Dry::Core::Inflector.classify(type)}[:#{name}]", parent: type) .call if block yield(klass) else klass end end |
#default_name ⇒ Symbol
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
237 238 239 |
# File 'lib/rom/commands/class_interface.rb', line 237 def default_name Dry::Core::Inflector.underscore(Dry::Core::Inflector.demodulize(name)).to_sym end |
#extend_for_relation(relation) ⇒ Class
Extend a command class with relation view methods
123 124 125 |
# File 'lib/rom/commands/class_interface.rb', line 123 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
13 14 15 16 17 |
# File 'lib/rom/commands/class_interface.rb', line 13 def inherited(klass) super klass.instance_variable_set(:'@before', before.dup) klass.instance_variable_set(:'@after', after.dup) end |
#options ⇒ Hash
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
246 247 248 |
# File 'lib/rom/commands/class_interface.rb', line 246 def { 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.
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/rom/commands/class_interface.rb', line 251 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
220 221 222 223 224 225 226 227 228 |
# File 'lib/rom/commands/class_interface.rb', line 220 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 = EMPTY_HASH) ⇒ Object
Use a configured plugin in this relation
112 113 114 |
# File 'lib/rom/commands/class_interface.rb', line 112 def use(plugin, = EMPTY_HASH) ROM.plugin_registry.commands.fetch(plugin, adapter).apply_to(self) end |