class Parameter
Represents a single input, output, or argument parameter of a rule.
Definitions
def initialize(direction, name, options = {}, &block)
Initialize the parameter.
Signature
-
parameter
directionSymbol One of
:input,:output, or:argument.-
parameter
nameSymbol The parameter name.
-
parameter
optionsHash Options such as
:default,:optional,:implicit,:pattern.
Implementation
def initialize(direction, name, options = {}, &block)
@direction = direction
@name = name
@options = options
@dynamic = block_given? ? Proc.new(&block) : nil
end
def input?
Signature
-
returns
Boolean Whether this is an input parameter.
Implementation
def input?
@direction == :input
end
def output?
Signature
-
returns
Boolean Whether this is an output parameter.
Implementation
def output?
@direction == :output
end
def dynamic?
Signature
-
returns
Boolean Whether this parameter has a dynamic computation block.
Implementation
def dynamic?
@dynamic != nil
end
def default?
Do we have a default value for this parameter?
Implementation
def default?
@options.key?(:default)
end
def implicit?
Signature
-
returns
Boolean Whether this parameter is implicitly computed and can be overridden.
Implementation
def implicit?
dynamic? and @options[:implicit]
end
def optional?
Optional parameters are those that are either defined as optional or implicit.
Implementation
def optional?
@options[:optional] || implicit? || default?
end
def applicable?(arguments)
Check whether the given arguments satisfy this parameter.
Signature
-
parameter
argumentsHash The arguments to check.
-
returns
Boolean Whether this parameter is satisfied.
Implementation
def applicable? arguments
value = arguments.fetch(@name) do
# Value couldn't be found, if it wasn't optional, this parameter didn't apply:
return optional?
end
# If a pattern is provided, we must match it.
if pattern = @options[:pattern]
return Array(value).all?{|item| pattern.match(item)}
end
return true
end
def compute(arguments, scope)
Compute the value for this parameter given the arguments and scope.
Signature
-
parameter
argumentsHash The current argument set.
-
parameter
scopeObject The task scope used for dynamic evaluation.
-
returns
Object The computed parameter value.
Implementation
def compute(arguments, scope)
if implicit?
# Can be replaced if supplied:
arguments[@name] || scope.instance_exec(arguments, &@dynamic) || @options[:default]
elsif dynamic?
# Argument is optional:
scope.instance_exec(arguments[@name], arguments, &@dynamic) || @options[:default]
elsif arguments.key?(@name)
arguments[@name]
else
@options[:default]
end
end
def hash
Signature
-
returns
Integer A hash value for this parameter.
Implementation
def hash
[self.class, @direction, @name, @options].hash
end
def eql?(other)
TODO fix implementation
Implementation
def eql? other
other.kind_of?(self.class) and @direction.eql?(other.direction) and @name.eql?(other.name) and @options.eql?(other.options) # and @dynamic == other.dynamic
end
def inspect
Signature
-
returns
String A human-readable representation of the parameter.
Implementation
def inspect
"#{direction}:#{@name} (#{options.inspect})"
end