class ComposedCommands::ComposedCommand

Public Class Methods

commands() click to toggle source
# File lib/composed_commands/composed_command.rb, line 6
def self.commands
  inherited_commands = if self.superclass.respond_to?(:commands)
    self.superclass.commands
  else
    []
  end
  Array(inherited_commands) + Array(@commands)
end
use(klass, options = {}) click to toggle source
# File lib/composed_commands/composed_command.rb, line 15
def self.use(klass, options = {})
  (@commands ||= []).push(ComposedCommands::CommandFactory.new(klass, options))
end

Public Instance Methods

commands() click to toggle source
# File lib/composed_commands/composed_command.rb, line 19
def commands
  self.class.commands.map do |command_factory|
    command = command_factory.create
    before_execute(command) if respond_to? :before_execute
    command
  end
end

Protected Instance Methods

execute(*args) click to toggle source
# File lib/composed_commands/composed_command.rb, line 28
def execute(*args)
  commands.inject(args) do |data, command|
    command.perform(*Array(data))

    if command.halted?
      case
      when command.failed?
        fail! command.message
      when command.succeed?
        success! command.result
      else
        raise "Unexpected state for interrupted command: `#{command.current_state.name}`"
      end
    end

    command.result
  end
end