Cuprum

An opinionated implementation of the Command pattern for Ruby applications. Cuprum wraps your business logic in a consistent, object-oriented interface and features status and error management, composability and control flow management.

Class: Cuprum::CommandFactory

Parent Namespace
Cuprum
Inherited Classes
Module > Object
Defined In
lib/cuprum/command_factory.rb

Table Of Contents

Overview

Builder class for instantiating command objects.

Examples

class SpaceFactory < Cuprum::CommandFactory
  command(:build, BuildCommand)

  command(:fly) { |launch_site:| FlyCommand.new(launch_site) }

  command_class(:dream) { DreamCommand }
end

factory = SpaceFactory.new

factory::Build #=> BuildCommand
factory.build  #=> an instance of BuildCommand

rocket = factory.build.call({ size: 'big' }) #=> an instance of Rocket
rocket.size                                  #=> 'big'

command = factory.fly(launch_site: 'KSC') #=> an instance of FlyCommand
command.call(rocket)
#=> launches the rocket from KSC

factory::Dream #=> DreamCommand
factory.dream  #=> an instance of DreamCommand

Back To Top

Class Methods

.command(name, command_class) => Object
.command(name) { |*args| } => Object

Defines a command for the factory.

Overloads

.command(name, command_class) => Object

Defines a command using the given factory class. For example, when a command is defined with the name “whirlpool” and the WhirlpoolCommand class:

A factory instance will define the constant ::Whirlpool, and accessing factory::Whirlpool will return the WhirlpoolCommand class.

A factory instance will define the method #whirlpool, and calling factory#whirlpool will return an instance of WhirlpoolCommand. Any arguments passed to the #whirlpool method will be forwarded to the constructor when building the command.

Examples
class MoveFactory < Cuprum::CommandFactory
  command :cut, CutCommand
end

factory = MoveFactory.new
factory::Cut #=> CutCommand
factory.cut  #=> an instance of CutCommand
Parameters
  • name (String, Symbol) — The name of the command.
  • command_class (Class) — The command class. Must be a subclass of Cuprum::Command.
.command(name) { |*args| } => Object

Defines a command using the given block, which must return an instance of a Cuprum::Command subclass. For example, when a command is defined with the name “dive” and a block that returns an instance of the DiveCommand class:

A factory instance will define the method #dive, and calling factory#dive will call the block and return the resulting command instance. Any arguments passed to the #dive method will be forwarded to the block when building the command.

The block will be evaluated in the context of the factory instance, so it has access to any methods or instance variables defined for the factory instance.

Examples
class MoveFactory < Cuprum::CommandFactory
  command :fly { |destination| FlyCommand.new(destination) }
end

factory = MoveFactory.new
factory.fly_command('Indigo Plateau')
#=> an instance of FlyCommand with a destination of 'Indigo Plateau'
Parameters
  • name (String, Symbol) — The name of the command.
Yields
  • The block will be executed in the context of the factory instance.
Yield Parameters
  • args (Array) — Any arguments given to the method factory.name() will be passed on the block.
Yield Returns
  • (Cuprum::Command) — The block return an instance of a Cuprum::Command subclass, or else raise an error.

.command_class(name, **metadata, &defn) => 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.

Examples

class MoveFactory < Cuprum::CommandFactory
  command_class :flash do
    Class.new(FlashCommand) do
      def brightness
        :intense
      end
    end
  end
end

factory = MoveFactory.new
factory::Flash #=> a subclass of FlashCommand
factory.flash  #=> an instance of factory::Flash

command = factory.flash
command.brightness #=> :intense

Parameters

Yields

Yield Parameters

Yield Returns

Raises

Back To Top

Instance Methods

#command?(command_name) => Boolean

Returns

#commands => Array<Symbol>

Returns

Back To Top


Back to Documentation | Versions | 1.3 | Reference | Cuprum