Class: Excavator::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/excavator/environment.rb

Overview

Public: Environment instances are created for a Command’s block to execute in. It is designed for modification and manipulation. Adding other methods into this class will allow all Commands to reference them.

Examples

module Helpers
  def pretty_logger(msg)
    puts "*~*~*~*"
    puts msg
    puts "*~*~*~*"
  end
end

# Add Helpers to Environment
Excavator::Environment.modify Helpers

param :msg
command :print do
  # #pretty_logger is now available
  pretty_logger params[:msg]
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Environment

Returns a new instance of Environment.



38
39
40
41
42
43
# File 'lib/excavator/environment.rb', line 38

def initialize(options = {})
  @runner          = options[:runner]
  @params          = options[:params]
  @raw_params      = options[:raw_params]
  @unparsed_params = options[:unparsed_params]
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



36
37
38
# File 'lib/excavator/environment.rb', line 36

def params
  @params
end

#raw_paramsObject (readonly)

Returns the value of attribute raw_params.



36
37
38
# File 'lib/excavator/environment.rb', line 36

def raw_params
  @raw_params
end

#runnerObject (readonly)

Returns the value of attribute runner.



36
37
38
# File 'lib/excavator/environment.rb', line 36

def runner
  @runner
end

#unparsed_paramsObject (readonly)

Returns the value of attribute unparsed_params.



36
37
38
# File 'lib/excavator/environment.rb', line 36

def unparsed_params
  @unparsed_params
end

Class Method Details

.modify(*mods, &block) ⇒ Object

Public: A convenience method to include other modules in this class.



31
32
33
34
# File 'lib/excavator/environment.rb', line 31

def self.modify(*mods, &block)
  mods.each { |m| include m }
  instance_eval &block if block
end

Instance Method Details

#execute(command, params = {}) ⇒ Object

Public: Execute another command. This is a convenience method to execute other commands defined in Excavator. #execute will return the value of the executed command (useful for building reusable blocks of code).

command - A String full name of a command. This should include namespaces. params - A Hash of Param name to values to pass to this command.

Examples

namespace :test do
  command :prepare do
    puts params[:foo]
    # => "bar"
  end

  command :run do
    execute "test:prepare", {:foo => "bar"}
  end
end

Returns value from executed command.



66
67
68
69
# File 'lib/excavator/environment.rb', line 66

def execute(command, params = {})
  command = runner.find_command(command)
  command.execute(params)
end