Class: EmberRoutes::Generator
- Inherits:
-
Object
- Object
- EmberRoutes::Generator
- Defined in:
- lib/ember_routes/generator.rb
Class Method Summary collapse
- .args_to_hash(*args) ⇒ Object
- .format_path(path, path_params, parameters) ⇒ Object
- .underscore(camel_cased_word) ⇒ Object
Instance Method Summary collapse
- #current_method ⇒ Object
- #current_path ⇒ Object
- #generate ⇒ Object
-
#initialize(config, context) ⇒ Generator
constructor
A new instance of Generator.
- #visit(route) ⇒ Object
Constructor Details
#initialize(config, context) ⇒ Generator
Returns a new instance of Generator.
26 27 28 29 30 |
# File 'lib/ember_routes/generator.rb', line 26 def initialize(config, context) @config = config @context = context @stack = [] end |
Class Method Details
.args_to_hash(*args) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/ember_routes/generator.rb', line 4 def self.args_to_hash(*args) parameters = {} if args.any? if args.is_a?(Array) parameters = args.reduce({}) { |carry, elem| carry.merge(elem) } else parameters = args end end parameters end |
.format_path(path, path_params, parameters) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/ember_routes/generator.rb', line 16 def self.format_path(path, path_params, parameters) path_params.each do |param_name| val = parameters[param_name.to_sym] raise "#{path} missing #{param_name}" unless val path = path.gsub(":"+param_name, val.to_s) parameters.delete(param_name.to_sym) end [path, parameters] end |
.underscore(camel_cased_word) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ember_routes/generator.rb', line 75 def self.underscore(camel_cased_word) camel_cased_word.to_s .gsub(/^\//, '') .gsub(/\/$/, '') .gsub(/\//, '_') .gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2') .gsub(/([a-z\d])([A-Z])/,'\1_\2') .tr("-", "_") .downcase end |
Instance Method Details
#current_method ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/ember_routes/generator.rb', line 62 def current_method if @stack.length > 1 name = @stack[1..-1].map(&:name).join('_') else name = @stack.first.name end @config.prefix + name + "_path" end |
#current_path ⇒ Object
71 72 73 |
# File 'lib/ember_routes/generator.rb', line 71 def current_path @stack.map(&:path).join('').gsub(/\/\//,'/') end |
#generate ⇒ Object
32 33 34 35 |
# File 'lib/ember_routes/generator.rb', line 32 def generate @stack = [] visit @config.root end |
#visit(route) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ember_routes/generator.rb', line 37 def visit(route) @stack.push route path = @config.base_url + current_path path_params = path.scan(/:(\w+)/).map { |arr| arr[0] } helper = Proc.new { |*args| formatted_path = path if (args.length) formatted_path, parameters = Generator.format_path(path, path_params, Generator.args_to_hash(*args)) end if parameters.any? formatted_path += "?" + URI.encode_www_form(parameters) end formatted_path } _method = current_method define_helper = Proc.new { define_method _method, helper } @context.class_eval &define_helper route.children.each do |child| visit(child) end @stack.pop end |