Class: Lono::Template
- Inherits:
-
Object
- Object
- Lono::Template
- Includes:
- ERB::Util
- Defined in:
- lib/lono/template.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #base64(value) ⇒ Object
-
#bracket_positions(line) ⇒ Object
Input: String Output: Array of parse positions.
- #build ⇒ Object
- #cfn_object?(s) ⇒ Boolean
-
#decompose(line) ⇒ Object
Input String line of code to decompose into chunks, some can be transformed into objects Output Array of strings, some can be transformed into objects.
- #evaluate(line) ⇒ Object
- #find_in_map(*args) ⇒ Object
- #get_att(*args) ⇒ Object
- #get_azs(region = "AWS::Region") ⇒ Object
-
#initialize(name, block, options = {}) ⇒ Template
constructor
A new instance of Template.
- #join(delimiter, values) ⇒ Object
-
#parse_positions(line) ⇒ Object
Input: Array - bracket_positions Ouput: Array - positions that can be use to determine what to parse.
- #partial(path, vars = {}) ⇒ Object
- #recompose(decomposition) ⇒ Object
- #ref(name) ⇒ Object
- #select(index, list) ⇒ Object
- #source(path) ⇒ Object
-
#transform(data) ⇒ Object
transform each line of bash script to array with cloud formation template objects.
- #transform_array(arr) ⇒ Object
- #user_data(path, vars = {}) ⇒ Object
- #variables(vars = {}) ⇒ Object
Constructor Details
#initialize(name, block, options = {}) ⇒ Template
Returns a new instance of Template.
9 10 11 12 13 |
# File 'lib/lono/template.rb', line 9 def initialize(name, block, ={}) @name = name @block = block @options = end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/lono/template.rb', line 8 def name @name end |
Instance Method Details
#base64(value) ⇒ Object
60 61 62 |
# File 'lib/lono/template.rb', line 60 def base64(value) %Q|{"Fn::Base64"=>"#{value}"}| end |
#bracket_positions(line) ⇒ Object
Input:
String
Output:
Array of parse positions
The positions of tokens taking into account when brackets start and close, handles nested brackets.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/lono/template.rb', line 102 def bracket_positions(line) positions,pair,count = [],[],0 line.split('').each_with_index do |char,i| pair << i if pair.empty? first_pair_char = line[pair[0]] if first_pair_char == '{' # object logic if char == '{' count += 1 end if char == '}' count -= 1 if count == 0 pair << i positions << pair pair = [] end end else # string logic lookahead = line[i+1] if lookahead == '{' pair << i positions << pair pair = [] end end end # end of loop # for string logic when lookahead does not contain a object token # need to clear out what's left to match the final pair if !pair.empty? pair << line.size - 1 positions << pair end positions end |
#build ⇒ Object
15 16 17 18 19 |
# File 'lib/lono/template.rb', line 15 def build instance_eval(&@block) template = IO.read(@source) ERB.new(template).result(binding) end |
#cfn_object?(s) ⇒ Boolean
189 190 191 192 193 194 195 |
# File 'lib/lono/template.rb', line 189 def cfn_object?(s) exact = %w[Ref] pattern = %w[Fn::] exact_match = !!exact.detect {|word| s.include?(word)} pattern_match = !!pattern.detect {|p| s =~ Regexp.new(p)} (exact_match || pattern_match) && s =~ /^{/ && s =~ /=>/ end |
#decompose(line) ⇒ Object
Input
String line of code to decompose into chunks, some can be transformed into objects
Output
Array of strings, some can be transformed into objects
Example: line = ‘abcd{dd}e’ # nested brackets template.decompose(line).should == [‘a’,‘b’,‘c’,‘d{dd}’,‘e’]
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/lono/template.rb', line 159 def decompose(line) positions = parse_positions(line) return [line] if positions.empty? result = [] str = '' until positions.empty? left = positions.shift right = positions.shift token = line[left..right] # if cfn object, add to the result set but after clearing out # the temp str that is being built up when the token is just a string if cfn_object?(token) unless str.empty? # first token might be a object result << str str = '' end result << token else str << token # keeps building up the string end end # at the of the loop there's a leftover string, unless the last token # is an object result << str unless str.empty? result end |
#evaluate(line) ⇒ Object
201 202 203 |
# File 'lib/lono/template.rb', line 201 def evaluate(line) recompose(decompose(line)) end |
#find_in_map(*args) ⇒ Object
56 57 58 |
# File 'lib/lono/template.rb', line 56 def find_in_map(*args) %Q|{"Fn::FindInMap" => [ #{transform_array(args)} ]}| end |
#get_att(*args) ⇒ Object
64 65 66 |
# File 'lib/lono/template.rb', line 64 def get_att(*args) %Q|{"Fn::GetAtt" => [ #{transform_array(args)} ]}| end |
#get_azs(region = "AWS::Region") ⇒ Object
68 69 70 |
# File 'lib/lono/template.rb', line 68 def get_azs(region="AWS::Region") %Q|{"Fn::GetAZs"=>"#{region}"}| end |
#join(delimiter, values) ⇒ Object
72 73 74 |
# File 'lib/lono/template.rb', line 72 def join(delimiter, values) %Q|{"Fn::Join" => ["#{delimiter}", [ #{transform_array(values)} ]]}| end |
#parse_positions(line) ⇒ Object
Input:
Array - bracket_positions
Ouput:
Array - positions that can be use to determine what to parse
146 147 148 149 |
# File 'lib/lono/template.rb', line 146 def parse_positions(line) positions = bracket_positions(line) positions.flatten end |
#partial(path, vars = {}) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/lono/template.rb', line 31 def partial(path,vars={}) path = "#{@options[:project_root]}/templates/partial/#{path}" template = IO.read(path) variables(vars) ERB.new(template).result(binding) end |
#recompose(decomposition) ⇒ Object
197 198 199 |
# File 'lib/lono/template.rb', line 197 def recompose(decomposition) decomposition.map { |s| cfn_object?(s) ? eval(s) : s } end |
#ref(name) ⇒ Object
52 53 54 |
# File 'lib/lono/template.rb', line 52 def ref(name) %Q|{"Ref"=>"#{name}"}| end |
#select(index, list) ⇒ Object
76 77 78 |
# File 'lib/lono/template.rb', line 76 def select(index, list) %Q|{"Fn::Select" => ["#{index}", [ #{transform_array(list)} ]]}| end |
#source(path) ⇒ Object
21 22 23 |
# File 'lib/lono/template.rb', line 21 def source(path) @source = path[0..0] == '/' ? path : "#{@options[:project_root]}/templates/#{path}" end |
#transform(data) ⇒ Object
transform each line of bash script to array with cloud formation template objects
86 87 88 89 90 91 92 93 |
# File 'lib/lono/template.rb', line 86 def transform(data) data = evaluate(data) if data[-1].is_a?(String) data[0..-2] + ["#{data[-1]}\n"] else data + ["\n"] end end |
#transform_array(arr) ⇒ Object
80 81 82 83 |
# File 'lib/lono/template.rb', line 80 def transform_array(arr) arr.map! {|x| x =~ /=>/ ? x : x.inspect } arr.join(',') end |
#user_data(path, vars = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lono/template.rb', line 38 def user_data(path, vars={}) path = "#{@options[:project_root]}/templates/user_data/#{path}" template = IO.read(path) variables(vars) result = ERB.new(template).result(binding) output = [] result.split("\n").each do |line| output += transform(line) end json = output.to_json json[0] = '' # remove first char: [ json.chop! # remove last char: ] end |
#variables(vars = {}) ⇒ Object
25 26 27 28 29 |
# File 'lib/lono/template.rb', line 25 def variables(vars={}) vars.each do |var,value| instance_variable_set("@#{var}", value) end end |