Class: Excavator::Command

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

Overview

Public: A Command is building block in Excavator. Commands are built incrementally (normally via methods in Excavator::DSL).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner, options = {}) ⇒ Command

Returns a new instance of Command.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/excavator/command.rb', line 31

def initialize(runner, options = {})
  @runner            = runner
  @name              = options[:name]
  @desc              = options[:desc]
  @block             = options[:block]
  @param_definitions = options[:param_definitions] || []
  @namespace         = options[:namespace]
  @param_parser      = options[:param_parser] ||
                       Excavator.param_parser_class.new
  @params            = {}
end

Instance Attribute Details

#blockObject

The logic for the command



12
13
14
# File 'lib/excavator/command.rb', line 12

def block
  @block
end

#descObject

Descriptors



9
10
11
# File 'lib/excavator/command.rb', line 9

def desc
  @desc
end

#nameObject

Descriptors



9
10
11
# File 'lib/excavator/command.rb', line 9

def name
  @name
end

#namespaceObject

Descriptors



9
10
11
# File 'lib/excavator/command.rb', line 9

def namespace
  @namespace
end

#param_definitionsObject (readonly)

A list of Param objects



15
16
17
# File 'lib/excavator/command.rb', line 15

def param_definitions
  @param_definitions
end

#paramsObject (readonly)

Public: Parsed params (parsed using ParamParser)



18
19
20
# File 'lib/excavator/command.rb', line 18

def params
  @params
end

#raw_paramsObject (readonly)

Public: An Array copy of arguments passed into this command (i.e, before ParamParser#parse! is called)



22
23
24
# File 'lib/excavator/command.rb', line 22

def raw_params
  @raw_params
end

#runnerObject (readonly)

Public: Reference to the Runner



29
30
31
# File 'lib/excavator/command.rb', line 29

def runner
  @runner
end

#unparsed_paramsObject (readonly)

Public: An Array of unparsed parameters. These are the left over arguments after ParamParser#parse! is called.



26
27
28
# File 'lib/excavator/command.rb', line 26

def unparsed_params
  @unparsed_params
end

Instance Method Details

#add_param(param) ⇒ Object

Public: Add a Param to this command.

Examples

command = Command.new
command.add_param(Param.new(:test))

Returns nothing.



51
52
53
# File 'lib/excavator/command.rb', line 51

def add_param(param)
  self.param_definitions << param
end

#execute(*args) ⇒ Object

Public: Execute the Command’s block within a Excavator::Environment instance. Arguments are parsed, setup with default values, and checked to ensure the Command has all the proper parameters.

args - An Array of arguments to pass into the block. This is normally

the same format as ARGV.

Examples

command = Command.new( ... )
command.execute(["-a", "abc", "--foo", "bar"])

Returns the value returned by the Command’s block.



68
69
70
71
72
# File 'lib/excavator/command.rb', line 68

def execute(*args)
  args.flatten!
  parse_params args
  run
end

#execute_with_params(parsed_params = {}) ⇒ Object

Public: Execute this command. This is like #execute except the parameters is a Hash.

parsed_params - A Hash of params to pass into the Command’s block.

Examples

command = Command.new( ... )
command.execute({:a => "abc", :foo => "bar})

Returns the value returned by the Command’s block.



85
86
87
88
# File 'lib/excavator/command.rb', line 85

def execute_with_params(parsed_params = {})
  parse_params [parsed_params]
  run
end

#full_nameObject

Public: The full name of the Command. This includes the Namespace’s name and the Namespace’s ancestor’s names.

Returns a String.



94
95
96
# File 'lib/excavator/command.rb', line 94

def full_name
  namespace.nil? ? name.to_s : namespace.full_name(name)
end