Class: Build::Rule
- Inherits:
-
Object
- Object
- Build::Rule
- Defined in:
- lib/build/rule.rb
Overview
A rule is a function with a specific set of input and output parameters, which can match against a given set of specific inputs and outputs. For example, there might be several rules for compiling, but the specific rules depend on the language being compiled.
Defined Under Namespace
Classes: Parameter
Instance Attribute Summary collapse
-
#full_name ⇒ Object
readonly
compile_cpp.
-
#name ⇒ Object
readonly
compile.cpp.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#primary_output ⇒ Object
readonly
Returns the value of attribute primary_output.
-
#process_name ⇒ Object
readonly
compile.
Instance Method Summary collapse
- #<<(parameter) ⇒ Object
- #==(other) ⇒ Object
-
#applicable?(arguments) ⇒ Boolean
Check if this rule can process these parameters.
- #apply(&block) ⇒ Object
- #apply!(scope, arguments) ⇒ Object
- #files(arguments) ⇒ Object
-
#initialize(process_name, type) ⇒ Rule
constructor
A new instance of Rule.
- #input(name, options = {}, &block) ⇒ Object
-
#normalize(arguments, scope) ⇒ Object
The scope is the context in which the dynamic rule computation is done, usually an instance of Task.
- #output(name, options = {}, &block) ⇒ Object
- #parameter(name, options = {}, &block) ⇒ Object
- #result(arguments) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(process_name, type) ⇒ Rule
Returns a new instance of Rule.
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/build/rule.rb', line 96 def initialize(process_name, type) @name = process_name + "." + type @full_name = @name.gsub(/[^\w]/, '_') @process_name = process_name.gsub('-', '_').to_sym @type = type @apply = nil @parameters = [] end |
Instance Attribute Details
#full_name ⇒ Object (readonly)
compile_cpp
117 118 119 |
# File 'lib/build/rule.rb', line 117 def full_name @full_name end |
#name ⇒ Object (readonly)
compile.cpp
109 110 111 |
# File 'lib/build/rule.rb', line 109 def name @name end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
111 112 113 |
# File 'lib/build/rule.rb', line 111 def parameters @parameters end |
#primary_output ⇒ Object (readonly)
Returns the value of attribute primary_output.
119 120 121 |
# File 'lib/build/rule.rb', line 119 def primary_output @primary_output end |
#process_name ⇒ Object (readonly)
compile
114 115 116 |
# File 'lib/build/rule.rb', line 114 def process_name @process_name end |
Instance Method Details
#<<(parameter) ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/build/rule.rb', line 133 def << parameter @parameters << parameter if parameter.output? @primary_output ||= parameter end end |
#==(other) ⇒ Object
196 197 198 |
# File 'lib/build/rule.rb', line 196 def ==(other) other.kind_of?(self.class) and @name == other.name and @parameters == other.parameters end |
#applicable?(arguments) ⇒ Boolean
Check if this rule can process these parameters
142 143 144 145 146 147 148 149 150 |
# File 'lib/build/rule.rb', line 142 def applicable?(arguments) @parameters.each do |parameter| next if parameter.implicit? return false unless parameter.applicable?(arguments) end return true end |
#apply(&block) ⇒ Object
182 183 184 |
# File 'lib/build/rule.rb', line 182 def apply(&block) @apply = Proc.new(&block) end |
#apply!(scope, arguments) ⇒ Object
186 187 188 |
# File 'lib/build/rule.rb', line 186 def apply!(scope, arguments) scope.instance_exec(arguments, &@apply) if @apply end |
#files(arguments) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/build/rule.rb', line 161 def files(arguments) input_files = [] output_files = [] @parameters.each do |parameter| # This could probably be improved a bit, we are assuming all parameters are file based: value = arguments[parameter.name] next unless value case parameter.direction when :input input_files << value when :output output_files << value end end return Build::Files::Composite.new(input_files), Build::Files::Composite.new(output_files) end |
#input(name, options = {}, &block) ⇒ Object
121 122 123 |
# File 'lib/build/rule.rb', line 121 def input(name, = {}, &block) self << Parameter.new(:input, name, , &block) end |
#normalize(arguments, scope) ⇒ Object
The scope is the context in which the dynamic rule computation is done, usually an instance of Task.
153 154 155 156 157 158 159 |
# File 'lib/build/rule.rb', line 153 def normalize(arguments, scope) Hash[ @parameters.collect do |parameter| [parameter.name, parameter.compute(arguments, scope)] end ] end |
#output(name, options = {}, &block) ⇒ Object
129 130 131 |
# File 'lib/build/rule.rb', line 129 def output(name, = {}, &block) self << Parameter.new(:output, name, , &block) end |
#parameter(name, options = {}, &block) ⇒ Object
125 126 127 |
# File 'lib/build/rule.rb', line 125 def parameter(name, = {}, &block) self << Parameter.new(:argument, name, , &block) end |
#result(arguments) ⇒ Object
190 191 192 193 194 |
# File 'lib/build/rule.rb', line 190 def result(arguments) if @primary_output arguments[@primary_output.name] end end |
#to_s ⇒ Object
200 201 202 |
# File 'lib/build/rule.rb', line 200 def to_s "#<#{self.class} #{@name.dump}>" end |