Class: Generator

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

Overview

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

Defined Under Namespace

Classes: AcceptInput, AskLoop, TemplateDir

Constant Summary collapse

WHITESPACE =
/\s*/
EMPTY =
''

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) {|_self| ... } ⇒ Generator

Returns a new instance of Generator.

Yields:

  • (_self)

Yield Parameters:

  • _self (Generator)

    the object that the method was called on



73
74
75
76
77
78
79
80
81
82
# 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

Class Method Details

.run(args) ⇒ Object

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



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# 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

Instance Method Details

#injectObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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_nameObject



92
93
94
95
96
# File 'lib/ngi/generator.rb', line 92

def module_name
  print '[?] Module name: '

  @module_name = AcceptInput.str(:condensed)
end

#nameObject



98
99
100
101
102
# File 'lib/ngi/generator.rb', line 98

def name
  print "[?] #{@type.capitalize} name: "

  @name = AcceptInput.str(:condensed)
end

#new_file_nameObject



86
87
88
89
90
# File 'lib/ngi/generator.rb', line 86

def new_file_name
  print '[?] New file name: '

  @new_file = AcceptInput.str(:condensed)
end

#overwrite?Boolean

create the new file

Returns:

  • (Boolean)


169
170
171
172
173
# File 'lib/ngi/generator.rb', line 169

def overwrite?
  AskLoop.ask(
    check: 'y', prompt: 'File exists already, overwrite it? (y/n) '
  )
end

#replaceObject



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
# 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

#tagObject



158
159
160
161
162
163
164
165
# 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

#writeObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# 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