class Flo::Command

Definition of a single command. In general you should not call {#initialize 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

Attributes

definition_lambda[R]
providers[R]
state_class[R]
tasks[R]

Public Class Methods

new(opts={}, &blk) click to toggle source

Creates a new command instance @option opts [Hash] providers ({}) Providers that the command will need

access to

@yield [args*]The block containing the definition for the command.

Arguments passed into {#call} are available within the block
# 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

Public Instance Methods

call(args={}) click to toggle source

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. @param args [Hash] arguments to be passed to each task

# 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
optional_parameters() click to toggle source

Returns a list of any optional parameters

Optional parameters are generated automatically by inspecting the optional parameters for the definition lambda @return [Array<Symbol>] An array of symbols representing optional parameters

# File lib/flo/command.rb, line 107
def optional_parameters
  definition_lambda.parameters.select { |key,_value| key == :key }.map { |_key,value| value }
end
perform(provider_sym, method_sym, provider_options={}) click to toggle source

@api dsl DSL method: Define a task that will be performed during the execution stage when {#call} is invoked. @param provider_sym [Symbol] The provider to send the message to @param method_sym [Symbol] The method you wish to call on the provider @param provider_options [Hash] 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
# 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
Also aliased as: validate
required_parameters() click to toggle source

Returns a list of any required parameters

Required parameters are generated automatically by inspecting the required parameters for the definition lambda @return [Array<Symbol>] An array of symbols representing required parameters

# File lib/flo/command.rb, line 96
def required_parameters
  definition_lambda.parameters.select { |key,_value| key == :req }.map { |_key,value| value }
end
state(provider_sym) click to toggle source

@api dsl 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. @param provider_sym [Symbol] The provider that you wish to query

@return [State] An object that will return a lambda, delegating the method

call to the provider specified
# File lib/flo/command.rb, line 65
def state(provider_sym)
  state_class.new(providers[provider_sym])
end
validate(provider_sym, method_sym, provider_options={})
Alias for: perform

Private Instance Methods

cleanroom() click to toggle source
# File lib/flo/command.rb, line 130
def cleanroom
  @cleanroom ||= self.class.send(:cleanroom).new(self)
end
convert_block_to_lambda(blk) click to toggle source
# File lib/flo/command.rb, line 119
def convert_block_to_lambda(blk)
  # jruby and rubinius can convert a proc directly into a lambda
  if (converted_block = lambda(&blk)).lambda?
    converted_block
  else
    # Otherwise, hacky method to take advantage of #define_method's automatic lambda conversion
    cleanroom.define_singleton_method(:_command_definition, &blk)
    cleanroom.method(:_command_definition).to_proc
  end
end
evaluate_command_definition(args) click to toggle source
# File lib/flo/command.rb, line 115
def evaluate_command_definition(args)
  cleanroom.instance_exec(*args, &definition_lambda)
end