Class: Build::Rulebook

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ Rulebook

Returns a new instance of Rulebook.



25
26
27
28
29
# File 'lib/build/rulebook.rb', line 25

def initialize(name = nil)
	@name = name
	@rules = {}
	@processes = {}
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



31
32
33
# File 'lib/build/rulebook.rb', line 31

def rules
  @rules
end

Class Method Details

.for(environment) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/build/rulebook.rb', line 84

def self.for(environment)
	rulebook = self.new(environment.name)
	
	environment.defined.each do |name, define|
		rulebook << define.klass.build(name, &define.block)
	end
	
	return rulebook
end

Instance Method Details

#<<(rule) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/build/rulebook.rb', line 33

def << rule
	@rules[rule.name] = rule

	# A cache for fast process/file-type lookup:
	processes = @processes[rule.process_name] ||= []
	processes << rule
end

#[](name) ⇒ Object



41
42
43
# File 'lib/build/rulebook.rb', line 41

def [] name
	@rules[name]
end

#with(superclass, **state) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/build/rulebook.rb', line 45

def with(superclass, **state)
	task_class = Class.new(superclass)
	
	# name = @name
	# task_class.send(:define_method, :to_s) do
	# 	"name"
	# end
	
	# Define methods for all processes, e.g. task_class#compile
	@processes.each do |key, rules|
		# Define general rules, which use rule applicability for disambiguation:
		task_class.send(:define_method, key) do |arguments, &block|
			rule = rules.find{|rule| rule.applicable? arguments }
			
			if rule
				invoke_rule(rule, arguments, &block)
			else
				raise NoApplicableRule.new(key, arguments)
			end
		end
	end
	
	# Define methods for all rules, e.g. task_class#compile_cpp
	@rules.each do |key, rule|
		task_class.send(:define_method, rule.full_name) do |arguments, &block|
			invoke_rule(rule, arguments, &block)
		end
	end
	
	# Typically, this defines methods like #environment and #target which can be accessed in the build rule.
	state.each do |key, value|
		task_class.send(:define_method, key) do
			value
		end
	end
	
	return task_class
end