Class: Flo::Command
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
-
#call(args = {}) ⇒ Object
Invoke the command that has already been defined.
-
#initialize(opts = {}) {|args*| ... } ⇒ Command
constructor
Creates a new command instance.
-
#perform(provider_sym, method_sym, provider_options = {}) ⇒ Object
(also: #validate)
DSL method: Define a task that will be performed during the execution stage when #call is invoked.
-
#state(provider_sym) ⇒ State
DSL method: Returns an object representing the current state of the provider during the execution stage when #call is invoked.
Constructor Details
#initialize(opts = {}) {|args*| ... } ⇒ Command
Creates a new command instance
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
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.
45 46 47 |
# File 'lib/flo/command.rb', line 45 def perform(provider_sym, method_sym, ={}) tasks << @task_class.new(providers[provider_sym], method_sym, ) 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.
65 66 67 |
# File 'lib/flo/command.rb', line 65 def state(provider_sym) state_class.new(providers[provider_sym]) end |