| 1 |
module Rake |
| 2 |
|
| 3 |
#################################################################### |
| 4 |
# TaskArguments manage the arguments passed to a task. |
| 5 |
# |
| 6 |
class TaskArguments |
| 7 |
include Enumerable |
| 8 |
|
| 9 |
attr_reader :names |
| 10 |
|
| 11 |
# Create a TaskArgument object with a list of named arguments |
| 12 |
# (given by :names) and a set of associated values (given by |
| 13 |
# :values). :parent is the parent argument object. |
| 14 |
def initialize(names, values, parent=nil) |
| 15 |
@names = names |
| 16 |
@parent = parent |
| 17 |
@hash = {} |
| 18 |
names.each_with_index { |name, i| |
| 19 |
@hash[name.to_sym] = values[i] unless values[i].nil? |
| 20 |
} |
| 21 |
end |
| 22 |
|
| 23 |
# Create a new argument scope using the prerequisite argument |
| 24 |
# names. |
| 25 |
def new_scope(names) |
| 26 |
values = names.collect { |n| self[n] } |
| 27 |
self.class.new(names, values, self) |
| 28 |
end |
| 29 |
|
| 30 |
# Find an argument value by name or index. |
| 31 |
def [](index) |
| 32 |
lookup(index.to_sym) |
| 33 |
end |
| 34 |
|
| 35 |
# Specify a hash of default values for task arguments. Use the |
| 36 |
# defaults only if there is no specific value for the given |
| 37 |
# argument. |
| 38 |
def with_defaults(defaults) |
| 39 |
@hash = defaults.merge(@hash) |
| 40 |
end |
| 41 |
|
| 42 |
def each(&block) |
| 43 |
@hash.each(&block) |
| 44 |
end |
| 45 |
|
| 46 |
def method_missing(sym, *args, &block) |
| 47 |
lookup(sym.to_sym) |
| 48 |
end |
| 49 |
|
| 50 |
def to_hash |
| 51 |
@hash |
| 52 |
end |
| 53 |
|
| 54 |
def to_s |
| 55 |
@hash.inspect |
| 56 |
end |
| 57 |
|
| 58 |
def inspect |
| 59 |
to_s |
| 60 |
end |
| 61 |
|
| 62 |
protected |
| 63 |
|
| 64 |
def lookup(name) |
| 65 |
if @hash.has_key?(name) |
| 66 |
@hash[name] |
| 67 |
elsif @parent |
| 68 |
@parent.lookup(name) |
| 69 |
end |
| 70 |
end |
| 71 |
end |
| 72 |
|
| 73 |
EMPTY_TASK_ARGS = TaskArguments.new([], []) |
| 74 |
end |