class Generator

This class generates templates (hence the name “Generator”)

Constants

EMPTY
WHITESPACE

Public Class Methods

new(args) { |self| ... } click to toggle source
# File lib/ngi/generator.rb, line 73
def initialize(args)
  @type, @config = args[:type], args[:config]

  @component = args[:component]
  @component['language'] = @config['language'][@component['type']]

  @template_file = TemplateDir.new(@component).read

  yield(self) if block_given?
end
run(args) click to toggle source

Use this function to be able to say Ngi::Delegate::Generator.run() inside the executable file. This function simply goes through all of the methods in order to interactively prompt the user to generate a new template

# File lib/ngi/generator.rb, line 189
def self.run(args)
  Generator.new(args) do |g|
    g.new_file_name

    # we don't need to define the module if we're creating a module
    g.module_name unless args[:type] == 'module'

    # 'run', 'config', and 'routes' don't have custom names in AngularJS
    g.name unless %w(run config routes index).include? args[:type]

    g.inject unless ['index'].include? args[:type]

    g.replace

    g.tag

    g.write
  end

  puts 'All done!'
end

Public Instance Methods

inject() click to toggle source
# File lib/ngi/generator.rb, line 104
def inject
  special = %w(routes controller).include?(@type)
  auto_injections = [
    { for_type: 'routes', service: '$routeProvider' },
    { for_type: 'controller', service: '$scope' }
  ]

  for_type = -> (inj) { inj[:for_type] == @type }
  injection = special ? auto_injections.find(&for_type)[:service] : nil

  auto_injection_statement = special ? " (already injected #{injection})" : ''

  print "[?] Inject#{auto_injection_statement}: "

  # accepts a comma-delimited list
  # EXAMPLE: testService, testService2
  # => [testService,testService2]
  @dependencies = AcceptInput.str(:comma_delimited_to_array)

  @dependencies << injection unless injection.nil?
end
module_name() click to toggle source
# File lib/ngi/generator.rb, line 92
def module_name
  print '[?] Module name: '

  @module_name = AcceptInput.str(:condensed)
end
name() click to toggle source
# File lib/ngi/generator.rb, line 98
def name
  print "[?] #{@type.capitalize} name: "

  @name = AcceptInput.str(:condensed)
end
new_file_name() click to toggle source
# File lib/ngi/generator.rb, line 86
def new_file_name
  print '[?] New file name: '

  @new_file = AcceptInput.str(:condensed)
end
overwrite?() click to toggle source
# File lib/ngi/generator.rb, line 169
def overwrite?
  AskLoop.ask(
    check: 'y', prompt: 'File exists already, overwrite it? (y/n) '
  )
end
replace() click to toggle source
# File lib/ngi/generator.rb, line 126
def replace
  # inject may or may not have run...
  # if it wasn't run, then @dependencies was never set
  @dependencies ||= []
  has_dependencies = @dependencies.size > 0

  # TODO: map aliases from config file
  @type = 'config' if @type == 'routes'

  # Regex replacements to generate the template
  cdv = lambda do |s, (dep, i)|
    s + dep.to_s + (i == @dependencies.size - 1 ? '' : ', ')
  end

  array_string = has_dependencies ? @dependencies.to_s.gsub(/"/, '\'') : '[]'

  if has_dependencies == true
    cdv_string = @dependencies.each_with_index.inject('', &cdv)
  else
    cdv_string = ''
  end

  cdv_regex = /\{\{inject\s\|\scomma_delimited_variables\}\}/

  @template_file =  @template_file
                    .gsub(/\{\{type\}\}/, @type)
                    .gsub(/\{\{name\}\}/, @name || '')
                    .gsub(/\{\{module\}\}/, @module_name || '')
                    .gsub(/\{\{inject\s\|\sarray_string\}\}/, array_string)
                    .gsub(cdv_regex, cdv_string)
end
tag() click to toggle source
# File lib/ngi/generator.rb, line 158
def tag
  # If Liquid-style tags are used in a template that can be used
  # for multiple components, remove those parts that don't
  # belong to the type of component user wants to generate
  @template_file =  @template_file
                    .gsub(/\{\%\sif\s#{@type}\s\%\}(.*)\{\%\sendif\s#{@type}\s\%\}/m, '\1')
                    .gsub(/\s\{\%\sif\s.*\s\%\}.*\{\%\sendif\s.*\s\%\}/m, '')
end
write() click to toggle source
# File lib/ngi/generator.rb, line 167
def write
  # create the new file
  def overwrite?
    AskLoop.ask(
      check: 'y', prompt: 'File exists already, overwrite it? (y/n) '
    )
  end

  overwrite? if File.exist?(@new_file)

  File.open(@new_file, 'w') do |file|
    file.write(@template_file)
    file.close
  end
end