Class: Configure::Configurable

Inherits:
Object
  • Object
show all
Defined in:
lib/ngi/configure.rb

Overview

Holds all the configure functions for the properties that are configurable

Class Method Summary collapse

Class Method Details

.create_template_file(args) ⇒ Object



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
# File 'lib/ngi/configure.rb', line 88

def self.create_template_file(args)
  component = args[:component]
  language = args[:language]
  template = args[:template]

  template_dir = TemplateDir.new(component, language, template).d

  destination = File.dirname(template_dir)

  # Create the directory "user" unless it already exists
  FileUtils.mkdir_p(destination) unless File.directory?(destination)

  # The actual custom file
  custom_file = template

  # Copy the custom file into the new or existing "user" directory
  if File.exist? custom_file
    # TODO: Add a 'safety net' by checking if the user
    # has already added the custom template before
    # so that they don't overwrite it
    FileUtils.cp(custom_file, destination)
  else
    puts "Cannot find custom template file: '#{custom_file}'
Check to make sure the custom template file exists,
and that you're in the correct directory of the custom template."

    exit
  end
end

.language(config) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ngi/configure.rb', line 73

def self.language(config)
  v = JSer.new(config.lang_types).to_str
  type = AskLoop.ask(check: config.lang_types, valid: v)
  curr_lang = config.config['language'][type]
  lang_opts = config.languages[type].reject { |l| l if curr_lang == l }

  v = JSer.new(lang_opts).to_str
  language = AskLoop.ask(check: lang_opts, valid: v)

  answer = config.config['language']
  answer[type] = language

  answer
end

.templates(config) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/ngi/configure.rb', line 118

def self.templates(config)
  v_c = JSer.new(config.components).to_str
  component = AskLoop.ask(check: config.components, valid: v_c)
  chosen_component = -> (c) { c['name'] == component }

  print '[?] Use the following template file: '
  file_name = AcceptInput.str(:stripped)

  print '[?] What language is this template for?'
  type = config.components_hash.find(&chosen_component)['type']
  v_l = JSer.new(config.languages[type]).to_str
  language = AskLoop.ask(check: config.languages[type], valid: v_l)
  chosen_language = -> (t) { t['language'] == language }

  # Our answer will be an empty array first,
  # because Tempaltes: might not exist the
  # config.yml file
  answer = []

  # Set the answer to Templates: from the config.yml
  # file if it exists
  answer = config.config['templates'] if config.config.key? 'templates'

  # Then we want to see if the component already exists in
  # the config file
  exists = false
  existing_component = answer.find(&chosen_component)
  exists = true if !existing_component == false

  if file_name != 'default'
    if exists == true
      # Remove the current existing component (it's already saved above)
      answer = answer
               .reject(&chosen_component)

      # Remove the existing template object
      existing_component['templates'] = existing_component['templates']
                                        .reject(&chosen_language)

      # Put in the updated template object
      existing_component['templates'] << {
        'language' => language,
        'template' => file_name
      }

      # Push the existing component back into the templates array
      answer << existing_component

    elsif exists == false

      # If it isn't already there,
      # go ahead and add it to the templates array
      answer << {
        'name' => component,
        'type' => type,
        'templates' => [
          {
            'language' => language,
            'template' => file_name
          }
        ]
      }
    end

    create_template_file(
      config: config.config,
      component: answer.last,
      language: language,
      template: file_name
    )
  else
    # If default is chosen as the template,
    # then delete the template from the templates
    # array for that component
    answer
      .find(&chosen_component)['templates']
      .delete_if(&chosen_language)

    # If the templates array of the component
    # is empty, then delete the entire component
    # from the answer
    answer
      .delete_if(&chosen_component) if answer
                                       .find(&chosen_component)['templates']
                                       .size == 0
  end

  if answer.size == 0
    nil
  else
    answer
  end
end