Class: Oschii::Template

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, name = nil) ⇒ Template

Returns a new instance of Template.



3
4
5
6
7
8
9
# File 'lib/oschii/template.rb', line 3

def initialize(config, name = nil)
  @config = config
  @name = name || config['name']
  @params = {}
  @embedded_customs = []
  @startups = []
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/oschii/template.rb', line 11

def config
  @config
end

#embedded_customsObject (readonly)

Returns the value of attribute embedded_customs.



11
12
13
# File 'lib/oschii/template.rb', line 11

def embedded_customs
  @embedded_customs
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/oschii/template.rb', line 11

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



11
12
13
# File 'lib/oschii/template.rb', line 11

def params
  @params
end

#startupsObject (readonly)

Returns the value of attribute startups.



11
12
13
# File 'lib/oschii/template.rb', line 11

def startups
  @startups
end

Instance Method Details

#bind_params(custom_config) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/oschii/template.rb', line 69

def bind_params(custom_config)
  missing_params = []
  param_names.each do |param|
    value = custom_config[param]
    if value
      params[param] = value
    else
      missing_params << param
    end
  end

  unless missing_params.empty?
    missing_params.each do |param|
      puts "No value for '#{param}'"
    end
    raise Oschii::TemplateError, "Custom:#{custom_config['name']}: Missing parameters"
  end
end

#build_components(custom_config, name_prefix = '') ⇒ Object



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
# File 'lib/oschii/template.rb', line 28

def build_components(custom_config, name_prefix = '')
  bind_params custom_config

  @embedded_customs = []

  config["Customs"]&.each do |inner_custom_def|
    @embedded_customs << fill_params(inner_custom_def)
  end

  BASIC_COMPONENT_TYPES.map do |type|
    config["#{type}s"]&.map do |comp|
      comp_conf = comp.dup

      comp_conf['name'] = "#{name_prefix}#{custom_config['name']}_#{comp['name']}"

      string = comp_conf.to_json

      params.each do |name, value|
        case value
        when Integer, Float, TrueClass, FalseClass
          string.gsub! "\"${#{name}}\"", "#{value}"
        else
          string.gsub! "${#{name}}", "#{value}"
        end
      end

      # Expand in-Template Component References
      string.gsub!(':@', ":#{name_prefix}#{custom_config['name']}_")

      Component.new(type, JSON.parse(string))
    end
  end.flatten.compact
end

#fill_params(json) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/oschii/template.rb', line 13

def fill_params(json)
  string = json.to_json

  params.each do |name, value|
    case value
    when Integer, Float, TrueClass, FalseClass
      string.gsub! "\"${#{name}}\"", "#{value}"
    else
      string.gsub! "${#{name}}", "#{value}"
    end
  end

  JSON.parse(string)
end

#inspectObject



92
93
94
# File 'lib/oschii/template.rb', line 92

def inspect
  "#<#{self.class} '#{name}'>"
end

#param_namesObject



62
63
64
65
66
# File 'lib/oschii/template.rb', line 62

def param_names
  config.to_s.scan(/\$\{\w+}/).collect do |tag|
    tag.gsub('${', '').gsub('}', '')
  end.uniq
end

#to_sObject



88
89
90
# File 'lib/oschii/template.rb', line 88

def to_s
  inspect
end