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)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/codgen/template.rb', line 10

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)
  @template_provider = Codgen.template_provider.named(template_info['template engine'])
  if @template_provider == nil
    @template_provider = Codgen.template_provider.for_extension(File.extname(@input_path))
  end
  if @template_provider == nil
    @template_provider = Codgen.template_provider.named('static')
  end
end

Instance Attribute Details

#dataObject (readonly)

Used to unit test



46
47
48
# File 'lib/codgen/template.rb', line 46

def data
  @data
end

#input_pathObject (readonly)

Used to unit test



46
47
48
# File 'lib/codgen/template.rb', line 46

def input_path
  @input_path
end

#templateObject (readonly)

Used to unit test



46
47
48
# File 'lib/codgen/template.rb', line 46

def template
  @template
end

Class Method Details

.get_template(path) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/codgen/template.rb', line 38

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



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/codgen/template.rb', line 26

def fill_template
  if @data.is_a?(Array)
    output = Hash.new
    @data.each { |data|
      output.store(Mustache.render(@out_raw, data), @template_provider.render(@template, data))
    }
    return output
  elsif @data.is_a?(Hash)
  return { Mustache.render(@out_raw, data) => @template_provider.render(@template, data) }
  end
end