Class: Viperaptor::ModuleTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/viperaptor/code_generation/module_template.rb

Overview

Represents a single Viperaptor module template

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = nil) ⇒ ModuleTemplate

Returns a new instance of ModuleTemplate.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/viperaptor/code_generation/module_template.rb', line 80

def initialize(name, options = nil)
  spec_path = TemplateHelper.obtain_spec(name)

  unless options
    spec = YAML.load_file(spec_path)
  else
    spec_source = IO.read(spec_path)
    spec_template = Liquid::Template.parse(spec_source)
    spec_content = spec_template.render(options)
    spec = YAML.load(spec_content)
  end

  viperaptor = spec[TEMPLATE_VIPERAPTOR_KEY]
  if !viperaptor.nil? && Gem::Version.new(Viperaptor::VERSION) < Gem::Version.new(viperaptor)
    puts("ERROR: Your viperaptor version is '#{Viperaptor::VERSION}' template requires '#{viperaptor}'. Please update it.".red)
    exit(1)
  end

  custom_parameters = load_custom_parameters_from_spec(spec)

  if !custom_parameters.empty? && !options.nil?
    spec_source = IO.read(spec_path)
    spec_template = Liquid::Template.parse(spec_source)

    if options["custom_parameters"].nil?
      options["custom_parameters"] = {}
    end

    new_options = options["custom_parameters"].merge(custom_parameters)

    new_options.each_key do |k|
      options["custom_parameters"][k] = new_options[k]
    end

    spec_content = spec_template.render(options)
    spec = YAML.load(spec_content)
  end

  @custom_parameters = options["custom_parameters"]
  @code_files = spec[TEMPLATE_CODE_FILES_KEY]
  @test_files = spec[TEMPLATE_TEST_FILES_KEY]
  @template_name = spec[TEMPLATE_NAME_KEY]
  @template_path = TemplateHelper.obtain_path(name)
  @dependencies = spec[TEMPLATE_DEPENDENCIES_KEY]
end

Instance Attribute Details

#code_filesObject (readonly)

Returns the value of attribute code_files.



8
9
10
# File 'lib/viperaptor/code_generation/module_template.rb', line 8

def code_files
  @code_files
end

#custom_parametersObject (readonly)

Returns the value of attribute custom_parameters.



8
9
10
# File 'lib/viperaptor/code_generation/module_template.rb', line 8

def custom_parameters
  @custom_parameters
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



8
9
10
# File 'lib/viperaptor/code_generation/module_template.rb', line 8

def dependencies
  @dependencies
end

#template_nameObject (readonly)

Returns the value of attribute template_name.



8
9
10
# File 'lib/viperaptor/code_generation/module_template.rb', line 8

def template_name
  @template_name
end

#template_pathObject (readonly)

Returns the value of attribute template_path.



8
9
10
# File 'lib/viperaptor/code_generation/module_template.rb', line 8

def template_path
  @template_path
end

#test_filesObject (readonly)

Returns the value of attribute test_files.



8
9
10
# File 'lib/viperaptor/code_generation/module_template.rb', line 8

def test_files
  @test_files
end

Instance Method Details

#load_custom_parameters_from_spec(spec) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/viperaptor/code_generation/module_template.rb', line 10

def load_custom_parameters_from_spec(spec)
  custom_parameters = spec[TEMPLATE_CUSTOM_PARAMETERS_KEY]
  result = {}
  unless custom_parameters.nil?
    custom_parameters.sort_by { |a| (a["order"] || 0) }.each do |desc|
      skip = false

      if !desc["only_if"].nil? && desc["only_if"].is_a?(Hash)
        desc["only_if"].each do |key, value|
          if value.is_a?(Array)
            if result[key].nil?
              puts("ERROR: Required custom_parameter '#{key}' not defined".red)
              exit(1)
            end

            if !value.include?(result[key])
              skip = true
              break
            end
          end
        end
      end

      if !skip
        if !desc["only_if"].nil? && desc["only_if_not"].is_a?(Hash)
          desc["only_if"].each do |key, value|
            if value.is_a?(Array)
              if result[key].nil?
                break
              end

              if value.include?(result[key])
                skip = true
                break
              end
            end
          end
        end
      end

      if !skip
        if desc["options"].instance_of?(Array)
          prompt = TTY::Prompt.new
          choices = desc["options"]
          value = prompt.select("#{desc["description"]}?", choices, per_page: choices.count)

          case value
          when "yes"
            value = "true"
          when "no"
            value = "false"
          else
            #
          end

          result[desc["name"]] = value
        elsif desc["options"].nil?
          prompt = TTY::Prompt.new
          value = prompt.ask("#{desc["description"]}?")
          result[desc["name"]] = value
        else
          puts("ERROR: Invalid variable 'options' of template custom parameter '#{desc["name"]}'".red)
          exit(1)
        end
      end
    end
  end
  return result
end