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.
Builder class for instantiating command objects.
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.command(name, command_class) => Object.command(name) { |*args| } => ObjectDefines a command for the factory.
.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.
class MoveFactory < Cuprum::CommandFactory
command :cut, CutCommand
end
factory = MoveFactory.new
factory::Cut #=> CutCommand
factory.cut #=> an instance of CutCommand.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.
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'.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.
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#command?(command_name) => Boolean
#commands => Array<Symbol>
Back to Documentation | Versions | 1.3 | Reference | Cuprum