Class: Trooper::Action
- Inherits:
-
Object
- Object
- Trooper::Action
- Includes:
- DSL::Bundler, DSL::Folders, DSL::Rake
- Defined in:
- lib/trooper/action.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#commands ⇒ Object
Returns the value of attribute commands.
-
#config ⇒ Object
Returns the value of attribute config.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#call(configuration) ⇒ Object
(also: #execute)
Public: Eval’s the block passed on initialize.
-
#initialize(name, description, options = {}, &block) ⇒ Action
constructor
Public: Define a new action.
-
#method_missing(method_sym, *arguments, &block) ⇒ Object
:nodoc:.
-
#ok? ⇒ Boolean
(also: #everything_ok?)
Public: Validates the action object.
-
#prerequisite_call(configuration) ⇒ Object
Public: Modifies the commands list to include the prerequisite list checker .
-
#reset_commands! ⇒ Object
Public: Resets Action commands to blank.
-
#run(command) ⇒ Object
Public: Appends command(string) to commands(array).
-
#type ⇒ Object
Public: What type of action this is.
Methods included from DSL::Bundler
#bundle_exec, #bundle_install, #rake
Methods included from DSL::Rake
Methods included from DSL::Folders
#cd, #create_folder, #create_folders, #delete_folder
Constructor Details
#initialize(name, description, options = {}, &block) ⇒ Action
Public: Define a new action.
name - The name of the action. description - A description of action to be used in the cli output. options - The Hash options used to refine the selection (default: {}):
:local - A boolean of whether this action should be run locally (optional).
:on - A symbol(:first_host, :last_host) to determine if to run on the first or last host (optional).
block - A block containing the tasks to run in this action.
Examples
Action.new(:my_action, 'Does great things') { run 'touch file' }
Returns a new action object.
28 29 30 31 |
# File 'lib/trooper/action.rb', line 28 def initialize(name, description, = {}, &block) @name, @description, @options, @config = name, description, , {} @call_count, @commands, @block = 0, [], block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *arguments, &block) ⇒ Object
:nodoc:
130 131 132 |
# File 'lib/trooper/action.rb', line 130 def method_missing(method_sym, *arguments, &block) # :nodoc: config[method_sym] || super end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
11 12 13 |
# File 'lib/trooper/action.rb', line 11 def block @block end |
#commands ⇒ Object
Returns the value of attribute commands.
12 13 14 |
# File 'lib/trooper/action.rb', line 12 def commands @commands end |
#config ⇒ Object
Returns the value of attribute config.
12 13 14 |
# File 'lib/trooper/action.rb', line 12 def config @config end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
11 12 13 |
# File 'lib/trooper/action.rb', line 11 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/trooper/action.rb', line 11 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/trooper/action.rb', line 11 def @options end |
Instance Method Details
#call(configuration) ⇒ Object Also known as: execute
Public: Eval’s the block passed on initialize.
configuration - The configuration object to be used for block eval.
Examples
@action.call(config_object) # => ['touch file']
Returns an array of commands(strings).
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/trooper/action.rb', line 42 def call(configuration) @config = configuration @call_count += 1 if everything_ok? && continue_call? reset_commands! build_commands commands else reset_commands! end end |
#ok? ⇒ Boolean Also known as: everything_ok?
Public: Validates the action object.
Examples
@action.ok? # => true
Returns true or raise an InvalidActionError exception.
80 81 82 83 84 85 86 87 88 |
# File 'lib/trooper/action.rb', line 80 def ok? begin build_commands reset_commands! true rescue Exception => e raise InvalidActionError, "Action missing config variables - #{e.}" end end |
#prerequisite_call(configuration) ⇒ Object
Public: Modifies the commands list to include the prerequisite list checker .
configuration - The configuration object to be used for block eval.
Examples
@action.call(config_object) # => "..."
Returns a command String.
66 67 68 69 70 71 |
# File 'lib/trooper/action.rb', line 66 def prerequisite_call(configuration) original_commands = call configuration original_commands << "echo '#{self.name}' >> #{prerequisite_list}" original_commands << "echo '#{self.description}'" "touch #{prerequisite_list}; if grep -vz #{self.name} #{prerequisite_list}; then #{original_commands.join(' && ')}; else echo 'Already Done'; fi" end |
#reset_commands! ⇒ Object
Public: Resets Action commands to blank.
Examples
@action.reset_commands! # => []
Returns a blank array.
126 127 128 |
# File 'lib/trooper/action.rb', line 126 def reset_commands! self.commands = [] end |
#run(command) ⇒ Object
Public: Appends command(string) to commands(array).
command - A String to be added to the commands array.
Examples
@action.run 'touch file' # => 'touch file'
@action.run '' # => nil
Returns the command or nil.
115 116 117 |
# File 'lib/trooper/action.rb', line 115 def run(command) commands << command if command != '' end |
#type ⇒ Object
Public: What type of action this is.
Examples
@action.type # => :action
@action.type # => :local_action
Returns a Symbol.
99 100 101 |
# File 'lib/trooper/action.rb', line 99 def type && [:local] ? :local_action : :action end |