Class: Cuprum::CommandFactory
- Inherits:
-
Module
- Object
- Module
- Cuprum::CommandFactory
- Defined in:
- lib/cuprum/command_factory.rb
Overview
Builder class for instantiating command objects.
Class Method Summary collapse
-
.command(name, klass = nil, **metadata, &defn) ⇒ Object
Defines a command for the factory.
-
.command_class(name, **metadata) {|*args| ... } ⇒ Object
Defines a command using the given block, which must return a subclass of Cuprum::Command.
Instance Method Summary collapse
-
#command?(command_name) ⇒ Boolean
True if the factory defines the given command, otherwise false.
-
#commands ⇒ Array<Symbol>
A list of the commands defined by the factory.
- #const_defined?(const_name, inherit = true) ⇒ Boolean
- #const_missing(const_name) ⇒ Object
Class Method Details
.command(name, command_class) ⇒ Object .command(name) {|args| ... } ⇒ Object
Defines a command for the factory.
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/cuprum/command_factory.rb', line 97 def command(name, klass = nil, **, &defn) guard_abstract_factory! if klass define_command_from_class(klass, name: name, metadata: ) elsif block_given? define_command_from_block(defn, name: name, metadata: ) else require_definition! end end |
.command_class(name, **metadata) {|*args| ... } ⇒ Object
Defines a command using the given block, which must return a subclass of Cuprum::Command. For example, when a command is defined with the name “rock_climb” and a block returning a subclass of RockClimbCommand:
A factory instance will define the constant ::RockClimb, and accessing factory::RockClimb will call the block and return the resulting command class. This value is memoized, so subsequent factory::RockClimb accesses on the same factory instance will return the same command class.
A factory instance will define the method #rock_climb, and calling factory#rock_climb will access the constant at ::RockClimb and return an instance of that subclass of RockClimbCommand. Any arguments passed to the #whirlpool method will be forwarded to the constructor when building the command.
149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/cuprum/command_factory.rb', line 149 def command_class(name, **, &defn) guard_abstract_factory! raise ArgumentError, 'must provide a block' unless block_given? method_name = normalize_command_name(name) (@command_definitions ||= {})[method_name] = .merge(__const_defn__: defn) define_lazy_command_method(method_name) end |
Instance Method Details
#command?(command_name) ⇒ Boolean
Returns true if the factory defines the given command, otherwise false.
256 257 258 259 260 |
# File 'lib/cuprum/command_factory.rb', line 256 def command?(command_name) command_name = normalize_command_name(command_name) commands.include?(command_name) end |
#commands ⇒ Array<Symbol>
Returns a list of the commands defined by the factory.
263 264 265 |
# File 'lib/cuprum/command_factory.rb', line 263 def commands self.class.send(:command_definitions).keys end |
#const_defined?(const_name, inherit = true) ⇒ Boolean
268 269 270 |
# File 'lib/cuprum/command_factory.rb', line 268 def const_defined?(const_name, inherit = true) # rubocop:disable Style/OptionalBooleanParameter command?(const_name) || super end |
#const_missing(const_name) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/cuprum/command_factory.rb', line 273 def const_missing(const_name) definitions = self.class.send(:command_definitions) command_name = normalize_command_name(const_name) command_defn = definitions.dig(command_name, :__const_defn__) return super unless command_defn command_class = command_defn.is_a?(Proc) ? instance_exec(&command_defn) : command_defn const_set(const_name, command_class) command_class end |