2011-06-24 02:11:55 +04:00
|
|
|
module Rake
|
|
|
|
|
2014-07-15 07:07:37 +04:00
|
|
|
##
|
2011-06-24 02:11:55 +04:00
|
|
|
# TaskArguments manage the arguments passed to a task.
|
|
|
|
#
|
|
|
|
class TaskArguments
|
|
|
|
include Enumerable
|
|
|
|
|
2014-07-15 07:07:37 +04:00
|
|
|
# Argument names
|
2011-06-24 02:11:55 +04:00
|
|
|
attr_reader :names
|
|
|
|
|
2014-07-15 07:07:37 +04:00
|
|
|
# Create a TaskArgument object with a list of argument +names+ and a set
|
|
|
|
# of associated +values+. +parent+ is the parent argument object.
|
2011-06-24 02:11:55 +04:00
|
|
|
def initialize(names, values, parent=nil)
|
|
|
|
@names = names
|
|
|
|
@parent = parent
|
|
|
|
@hash = {}
|
2013-10-12 01:35:01 +04:00
|
|
|
@values = values
|
2011-06-24 02:11:55 +04:00
|
|
|
names.each_with_index { |name, i|
|
|
|
|
@hash[name.to_sym] = values[i] unless values[i].nil?
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-07-15 07:07:37 +04:00
|
|
|
# Retrieve the complete array of sequential values
|
2013-10-12 01:35:01 +04:00
|
|
|
def to_a
|
|
|
|
@values.dup
|
|
|
|
end
|
|
|
|
|
2014-07-15 07:07:37 +04:00
|
|
|
# Retrieve the list of values not associated with named arguments
|
2013-10-12 01:35:01 +04:00
|
|
|
def extras
|
|
|
|
@values[@names.length..-1] || []
|
|
|
|
end
|
|
|
|
|
2011-06-24 02:11:55 +04:00
|
|
|
# Create a new argument scope using the prerequisite argument
|
|
|
|
# names.
|
|
|
|
def new_scope(names)
|
2013-10-12 01:35:01 +04:00
|
|
|
values = names.map { |n| self[n] }
|
|
|
|
self.class.new(names, values + extras, self)
|
2011-06-24 02:11:55 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Find an argument value by name or index.
|
|
|
|
def [](index)
|
|
|
|
lookup(index.to_sym)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Specify a hash of default values for task arguments. Use the
|
|
|
|
# defaults only if there is no specific value for the given
|
|
|
|
# argument.
|
|
|
|
def with_defaults(defaults)
|
|
|
|
@hash = defaults.merge(@hash)
|
|
|
|
end
|
|
|
|
|
2014-07-15 07:07:37 +04:00
|
|
|
# Enumerates the arguments and their values
|
2011-06-24 02:11:55 +04:00
|
|
|
def each(&block)
|
|
|
|
@hash.each(&block)
|
|
|
|
end
|
|
|
|
|
2014-07-15 07:07:37 +04:00
|
|
|
# Extracts the argument values at +keys+
|
2011-06-28 06:45:29 +04:00
|
|
|
def values_at(*keys)
|
|
|
|
keys.map { |k| lookup(k) }
|
|
|
|
end
|
|
|
|
|
2014-07-15 07:07:37 +04:00
|
|
|
# Returns the value of the given argument via method_missing
|
2012-11-16 01:59:37 +04:00
|
|
|
def method_missing(sym, *args)
|
2011-06-24 02:11:55 +04:00
|
|
|
lookup(sym.to_sym)
|
|
|
|
end
|
|
|
|
|
2014-07-15 07:07:37 +04:00
|
|
|
# Returns a Hash of arguments and their values
|
2011-06-24 02:11:55 +04:00
|
|
|
def to_hash
|
|
|
|
@hash
|
|
|
|
end
|
|
|
|
|
2014-07-15 07:07:37 +04:00
|
|
|
def to_s # :nodoc:
|
2011-06-24 02:11:55 +04:00
|
|
|
@hash.inspect
|
|
|
|
end
|
|
|
|
|
2014-07-15 07:07:37 +04:00
|
|
|
def inspect # :nodoc:
|
2011-06-24 02:11:55 +04:00
|
|
|
to_s
|
|
|
|
end
|
|
|
|
|
2014-07-15 07:07:37 +04:00
|
|
|
# Returns true if +key+ is one of the arguments
|
|
|
|
def has_key?(key)
|
|
|
|
@hash.has_key?(key)
|
|
|
|
end
|
|
|
|
|
2011-06-24 02:11:55 +04:00
|
|
|
protected
|
|
|
|
|
2014-07-15 07:07:37 +04:00
|
|
|
def lookup(name) # :nodoc:
|
2011-06-24 02:11:55 +04:00
|
|
|
if @hash.has_key?(name)
|
|
|
|
@hash[name]
|
|
|
|
elsif @parent
|
|
|
|
@parent.lookup(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-15 07:07:37 +04:00
|
|
|
EMPTY_TASK_ARGS = TaskArguments.new([], []) # :nodoc:
|
2011-06-24 02:11:55 +04:00
|
|
|
end
|