Class: Build::Rule::Parameter

Inherits:
Object
  • Object
show all
Defined in:
lib/build/rule.rb

Overview

Represents a single input, output, or argument parameter of a rule.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(direction, name, options = {}, &block) ⇒ Parameter

Initialize the parameter.



26
27
28
29
30
31
32
33
# File 'lib/build/rule.rb', line 26

def initialize(direction, name, options = {}, &block)
	@direction = direction
	@name = name
	
	@options = options
	
	@dynamic = block_given? ? Proc.new(&block) : nil
end

Instance Attribute Details

#direction ⇒ Object (readonly)

Returns the value of attribute direction.



35
36
37
# File 'lib/build/rule.rb', line 35

def direction
  @direction
end

#dynamic ⇒ Object (readonly)

Returns the value of attribute dynamic.



39
40
41
# File 'lib/build/rule.rb', line 39

def dynamic
  @dynamic
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/build/rule.rb', line 36

def name
  @name
end

#options ⇒ Object (readonly)

Returns the value of attribute options.



38
39
40
# File 'lib/build/rule.rb', line 38

def options
  @options
end

Instance Method Details

#applicable?(arguments) ⇒ Boolean

Check whether the given arguments satisfy this parameter.

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/build/rule.rb', line 74

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

#compute(arguments, scope) ⇒ Object

Compute the value for this parameter given the arguments and scope.



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/build/rule.rb', line 92

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

#default? ⇒ Boolean

Do we have a default value for this parameter?

Returns:

  • (Boolean)


57
58
59
# File 'lib/build/rule.rb', line 57

def default?
	@options.key?(:default)
end

#dynamic? ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/build/rule.rb', line 52

def dynamic?
	@dynamic != nil
end

#eql?(other) ⇒ Boolean

TODO fix implementation

Returns:

  • (Boolean)


112
113
114
# File 'lib/build/rule.rb', line 112

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

#hash ⇒ Object



107
108
109
# File 'lib/build/rule.rb', line 107

def hash
	[self.class, @direction, @name, @options].hash
end

#implicit? ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/build/rule.rb', line 62

def implicit?
	dynamic? and @options[:implicit]
end

#input? ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/build/rule.rb', line 42

def input?
	@direction == :input
end

#inspect ⇒ Object



117
118
119
# File 'lib/build/rule.rb', line 117

def inspect
	"#{direction}:#{@name} (#{options.inspect})"
end

#optional? ⇒ Boolean

Optional parameters are those that are either defined as optional or implicit.

Returns:

  • (Boolean)


67
68
69
# File 'lib/build/rule.rb', line 67

def optional?
	@options[:optional] || implicit? || default?
end

#output? ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/build/rule.rb', line 47

def output?
	@direction == :output
end