-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_processor.rb
70 lines (59 loc) · 1.83 KB
/
template_processor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
=begin
--------------------------------------------------------------------------------
A utility class that reads a template file and replaces the key strings in it.
Each key string should look something like this:
<%= @settings.root_user %>
This class will accept a hash of settings and make them available to the template.
process() does the substitution as above
process_complete() does the substitution and complains about any keys for which
values were not found.
--------------------------------------------------------------------------------
=end
require 'erb'
#
# The main class
#
class TemplateProcessor
def process_string(raw)
ERB.new(raw).result(binding)
end
#
# Process the source file and either return the result,
# or write it to a target file.
#
def process(source, target=nil)
raise SettingsError.new("#{source} doesn't exist") unless File.exist?(source)
if target
File.open(source) do |source_file|
File.open(target, 'w') do |target_file|
target_file.write(process_string(source_file.read()))
end
end
else
File.open(source) do |source_file|
process_string(source_file.read())
end
end
end
#
# Check to insure that all keys have defined values before processing
#
def process_complete(source, target)
missing = list_required_keys(source) - @settings.keys.map() {|k| "@settings.#{k}"}
raise SettingsError.new("Missing values for these keys: #{missing}") unless missing.empty?
process(source, target)
end
def list_required_keys(source)
# Look for <%= name %>
matches = []
File.open(source) do |source_file|
source_file.each do |line|
matches << line.scan(/<%=\s*([^%\s]+)\s*%>/)
end
end
return matches.flatten().sort.uniq
end
def initialize(props)
@settings = props
end
end