Class: Flo::Command

Inherits:
Object
  • Object
show all
Includes:
Cleanroom
Defined in:
lib/flo/command.rb

Overview

Definition of a single command. In general you should not call Command.new directly, but instead define the command using Runner#register_command.

When a command is generated using Runner#register_command (typically in a flo configuration file), only the DSL methods will be available

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) {|args*| ... } ⇒ Command

Creates a new command instance

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • providers (Hash) — default: {}

    Providers that the command will need access to

Yields:

  • (args*)

    The block containing the definition for the command. Arguments passed into #call are available within the block

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
# File 'lib/flo/command.rb', line 27

def initialize(opts={}, &blk)
  raise ArgumentError.new('.new must be called with a block defining the command') unless blk
  @state_class = opts[:state_class] || Flo::State
  @task_class = opts[:task_class] || Flo::Task
  @providers = opts[:providers] || {}
  @tasks = []
  @definition_lambda = convert_block_to_lambda(blk)
end

Instance Method Details

#call(args = {}) ⇒ Object

Invoke the command that has already been defined.

This will run the command, processing any tasks defined by #perform and #validate in order, stopping execution if any of the tasks fails. Arguments passed in here will be merged with the provider options defined in each task.

Parameters:

  • args (Hash) (defaults to: {})

    arguments to be passed to each task



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/flo/command.rb', line 78

def call(args={})
  evaluate_command_definition(args)
  response = tasks.map do |task|

    response = task.call(args)

    # bail early if the task failed
    return response unless response.success?
    response
  end.last
end

#perform(provider_sym, method_sym, provider_options = {}) ⇒ Object Also known as: validate

DSL method: Define a task that will be performed during the execution stage when #call is invoked.

Parameters:

  • provider_sym (Symbol)

    The provider to send the message to

  • method_sym (Symbol)

    The method you wish to call on the provider

  • provider_options (Hash) (defaults to: {})

    A hash of options to be passed when invoking the method on the provider. Any lambda values will be called during the execution stage when ##call is invoked



45
46
47
# File 'lib/flo/command.rb', line 45

def perform(provider_sym, method_sym, provider_options={})
  tasks << @task_class.new(providers[provider_sym], method_sym, provider_options)
end

#state(provider_sym) ⇒ State

DSL method: Returns an object representing the current state of the provider during the execution stage when #call is invoked. Any methods called on the State instance will return a lambda. This is intended to be used in the parameters passed to the #perform method, as you often want these parameters lazily evaluated during the execution stage, not when the definition is parsed.

Parameters:

  • provider_sym (Symbol)

    The provider that you wish to query

Returns:

  • (State)

    An object that will return a lambda, delegating the method call to the provider specified



65
66
67
# File 'lib/flo/command.rb', line 65

def state(provider_sym)
  state_class.new(providers[provider_sym])
end