class Configure::Configurable

Holds all the configure functions for the properties that are configurable

Public Class Methods

create_template_file(args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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