Class: Codgen::Template
- Inherits:
-
Object
- Object
- Codgen::Template
- Defined in:
- lib/codgen/template.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Used to unit test.
-
#input_path ⇒ Object
readonly
Used to unit test.
-
#template ⇒ Object
readonly
Used to unit test.
Class Method Summary collapse
Instance Method Summary collapse
- #fill_template ⇒ Object
-
#initialize(template_info, data_root) ⇒ Template
constructor
A new instance of Template.
Constructor Details
#initialize(template_info, data_root) ⇒ Template
Returns a new instance of Template.
9 10 11 12 13 14 15 16 |
# File 'lib/codgen/template.rb', line 9 def initialize(template_info, data_root) @input_path = template_info['in'] @out_raw = template_info['out'] source_raw = template_info['source'] source_location = source_raw != nil ? source_raw.split('.') : [] @data = Flattener.merge(data_root, source_location) @template = Template.get_template(@input_path) end |
Instance Attribute Details
#data ⇒ Object (readonly)
Used to unit test
38 39 40 |
# File 'lib/codgen/template.rb', line 38 def data @data end |
#input_path ⇒ Object (readonly)
Used to unit test
38 39 40 |
# File 'lib/codgen/template.rb', line 38 def input_path @input_path end |
#template ⇒ Object (readonly)
Used to unit test
38 39 40 |
# File 'lib/codgen/template.rb', line 38 def template @template end |
Class Method Details
.get_template(path) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/codgen/template.rb', line 30 def self.get_template(path) if File.exist?(path) File.read(path) else Logger.error('Could not find file "'+path+'"') end end |
Instance Method Details
#fill_template ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/codgen/template.rb', line 18 def fill_template if @data.is_a?(Array) output = Hash.new @data.each { |data| output.store(Mustache.render(@out_raw, data), Mustache.render(@template, data)) } return output elsif @data.is_a?(Hash) return { Mustache.render(@out_raw, data) => Mustache.render(@template, @data) } end end |