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.
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/cuprum/command_factory.rb', line 95 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.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/cuprum/command_factory.rb', line 147 def command_class(name, **, &defn) guard_abstract_factory! raise ArgumentError, 'must provide a block'.freeze unless block_given? name = normalize_command_name(name) (@command_definitions ||= {})[name] = .merge(__const_defn__: defn) const_name = tools.string.camelize(name) define_method(name) do |*args, &block| command_class = const_get(const_name) build_command(command_class, *args, &block) end end |
Instance Method Details
#command?(command_name) ⇒ Boolean
Returns true if the factory defines the given command, otherwise false.
234 235 236 237 238 |
# File 'lib/cuprum/command_factory.rb', line 234 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.
241 242 243 |
# File 'lib/cuprum/command_factory.rb', line 241 def commands self.class.send(:command_definitions).keys end |
#const_defined?(const_name, inherit = true) ⇒ Boolean
246 247 248 |
# File 'lib/cuprum/command_factory.rb', line 246 def const_defined?(const_name, inherit = true) command?(const_name) || super end |
#const_missing(const_name) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/cuprum/command_factory.rb', line 251 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 |