Class: Rails::Generator::Commands::Create
- Inherits:
-
Object
- Object
- Rails::Generator::Commands::Create
- Defined in:
- lib/invoicing_generator/generator_extensions.rb
Instance Method Summary collapse
-
#add_routes(*lines) ⇒ Object
Based on the ‘route_resources’ method, but less restrictive.
-
#nested_class_template(relative_source, class_details, template_options = {}) ⇒ Object
A bit like the ‘template’ method, but wraps the rendered template output in a Ruby class definition, potentially nested in one or more module definitions.
Instance Method Details
#add_routes(*lines) ⇒ Object
Based on the ‘route_resources’ method, but less restrictive. Adds arbitrary lines to the config/routes.rb file.
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/invoicing_generator/generator_extensions.rb', line 40 def add_routes(*lines) text = (lines.flatten.map do |line| line.strip! logger.route line " #{line}\n" end).join sentinel = 'ActionController::Routing::Routes.draw do |map|' unless [:pretend] gsub_file('config/routes.rb', /(#{Regexp.escape(sentinel)})/mi) {|match| "#{match}\n#{text}" } end end |
#nested_class_template(relative_source, class_details, template_options = {}) ⇒ Object
A bit like the ‘template’ method, but wraps the rendered template output in a Ruby class definition, potentially nested in one or more module definitions. class_details should be a hash in the form returned by InvoicingGenerator::NameTools#extract_name_details, detailing information about the class and to which file it should be written.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/invoicing_generator/generator_extensions.rb', line 12 def nested_class_template(relative_source, class_details, = {}) # Render the relative_source template inside_template = render_file(source_path(relative_source), ) do |file| vars = [:assigns] || {} b = binding vars.each { |k,v| eval "#{k} = vars[:#{k}] || vars['#{k}']", b } # Render the source file with the temporary binding ERB.new(file.read, nil, '-').result(b) end # Prepare class and module definitions nesting = class_details[:class_nesting_array] index = -1 header = nesting.map{|mod| index += 1; (' ' * index) + "module #{mod}\n"}.join header << (' ' * nesting.size) + "class #{class_details[:class_name_base]}" header << " < #{class_details[:superclass]}" unless [nil, ''].include? class_details[:superclass] header << "\n" = (0..nesting.size).to_a.reverse.map{|n| (' ' * n) + "end\n"}.join indent = ' ' * (nesting.size + 1) # Write everything to file file(relative_source, class_details[:file_path_full], ) do header + inside_template.split("\n").map{|line| "#{indent}#{line}"}.join("\n") + "\n" + end end |