Class: Codgen::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/codgen/template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_info, data_root) ⇒ Template

Returns a new instance of Template.

Parameters:

  • template_info (JSON_Hash)
  • data_root (JSON_Hash)


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

#dataObject (readonly)

Used to unit test



38
39
40
# File 'lib/codgen/template.rb', line 38

def data
  @data
end

#input_pathObject (readonly)

Used to unit test



38
39
40
# File 'lib/codgen/template.rb', line 38

def input_path
  @input_path
end

#templateObject (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_templateObject



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