Class: Generator
- Inherits:
-
Object
- Object
- Generator
- Defined in:
- lib/ngi/generator.rb
Overview
This class generates templates (hence the name “Generator”)
Defined Under Namespace
Classes: AskLoop
Constant Summary collapse
- Utils =
::Utils
- WHITESPACE =
/\s*/
- EMPTY =
''
- CURRENT_DIR =
File.dirname(__FILE__)
Class Method Summary collapse
-
.run(args) ⇒ Object
Use this function to be able to say AngularInit::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.
Instance Method Summary collapse
-
#initialize(args) {|_self| ... } ⇒ Generator
constructor
SET UP ALL THE CONFIG OPTIONS Generator.new (the initialization function) is called in self.run.
- #inject ⇒ Object
- #module_name ⇒ Object
- #name ⇒ Object
- #new_file_name ⇒ Object
-
#overwrite? ⇒ Boolean
create the new file.
- #replace ⇒ Object
- #tag ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(args) {|_self| ... } ⇒ Generator
SET UP ALL THE CONFIG OPTIONS Generator.new (the initialization function) is called in self.run
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/ngi/generator.rb', line 39 def initialize(args) @type, @config = args[:type], args[:config]['global'] component = @config['components'].find { |t| t['name'] == @type } # basically just rebuilding the object so we can use it here @component = { of_type: component['type'], language: @config['language'][component['type']], using: component['using'], template: component['template'] } template_dir = "#{CURRENT_DIR}/../templates/" template_dir << "#{@component[:of_type]}/#{@component[:language]}/#{@component[:using]}/#{@component[:template]}" @template_file = IO.read(template_dir) yield(self) if block_given? end |
Class Method Details
.run(args) ⇒ Object
Use this function to be able to say AngularInit::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
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/ngi/generator.rb', line 160 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 # REVIEW: use symbols instead of strings? 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
#inject ⇒ Object
79 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 |
# File 'lib/ngi/generator.rb', line 79 def inject # REVIEW: use symbols instead of strings? special = %w(routes controller).include?(@type) auto_injections = [ { for_type: 'routes', service: '$routeProvider' }, { for_type: 'controller', service: '$scope' } ] injection = special ? auto_injections.select { |inj| inj[:for_type] == @type }[0][: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 = $stdin.gets.split(',').map(&:strip).reject(&:empty?) # automatically inject $scope into a controller # FIXME: don't use index accessors (numbers are confusing) @dependencies << auto_injections[1][:service] if @type == 'controller' && !@dependencies.include?(auto_injections[1][:service]) # automatically insert $routeProvider into a routes config @dependencies << auto_injections[0][:service] if @type == 'routes' && !@dependencies.include?(auto_injections[0][:service]) end |
#module_name ⇒ Object
67 68 69 70 71 |
# File 'lib/ngi/generator.rb', line 67 def module_name print '[?] Module name: ' @module_name = $stdin.gets.gsub(WHITESPACE, EMPTY) end |
#name ⇒ Object
73 74 75 76 77 |
# File 'lib/ngi/generator.rb', line 73 def name print "[?] #{@type.capitalize} name: " @name = $stdin.gets.gsub(WHITESPACE, EMPTY) end |
#new_file_name ⇒ Object
61 62 63 64 65 |
# File 'lib/ngi/generator.rb', line 61 def new_file_name print '[?] New file name: ' @new_file = $stdin.gets.gsub(WHITESPACE, EMPTY) end |
#overwrite? ⇒ Boolean
create the new file
145 146 147 |
# File 'lib/ngi/generator.rb', line 145 def overwrite? AskLoop.ask(check: 'y', prompt: 'File exists already, overwrite it? (y/n) ') end |
#replace ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/ngi/generator.rb', line 106 def replace # inject may or may not have run... if it wasn't run, then @dependencies was never set # for example, for html templates, we don't run the inject function # @dependencies = @dependencies || [] @dependencies ||= [] has_dependencies = @dependencies.size > 0 # Use 'config' as the type, since 'routes' is really an alias for a specific type of 'config'. # TODO: map aliases from config file @type = 'config' if @type == 'routes' # Regex replacements to generate the template @template_file = @template_file .gsub(/\{\{type\}\}/, @type) .gsub(/\{\{name\}\}/, @name || '') .gsub(/\{\{module\}\}/, "#{@module_name}") .gsub( /\{\{inject\s\|\sarray_string\}\}/, has_dependencies ? @dependencies.to_s.gsub(/"/, '\'') : '[]' ) .gsub( /\{\{inject\s\|\scomma_delimited_variables\}\}/, has_dependencies ? @dependencies.each_with_index.inject('') { |str, (dep, i)| str += dep.to_s + (i == @dependencies.size - 1 ? '' : ', ') } : '' ) end |
#tag ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/ngi/generator.rb', line 134 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 ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/ngi/generator.rb', line 143 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 |