Class: Merb::Router::Route::Generator

Inherits:
Object
  • Object
show all
Defined in:
merb-core/lib/merb-core/dispatch/router/route.rb

Overview

The Generator class handles compiling the route down to a lambda that can generate the URL from a params hash and a default params hash.

Instance Method Summary collapse

Constructor Details

#initialize(segments, symbol_conditions, identifiers) ⇒ Generator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Generator



234
235
236
237
238
239
240
241
# File 'merb-core/lib/merb-core/dispatch/router/route.rb', line 234

def initialize(segments, symbol_conditions, identifiers)
  @segments          = segments
  @symbol_conditions = symbol_conditions
  @identifiers       = identifiers
  @stack             = []
  @opt_segment_count = 0
  @opt_segment_stack = [[]]
end

Instance Method Details

#compiledObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'merb-core/lib/merb-core/dispatch/router/route.rb', line 244

def compiled
  ruby  = ""
  ruby << "lambda do |params, defaults|\n"
  ruby << "  fragment     = params.delete(:anchor) || params.delete(:fragment)\n"
  ruby << "  query_params = params.dup\n"
  
  with(@segments) do
    ruby << "  include_defaults = true\n"
    ruby << "  return unless url = #{block_for_level}\n"
  end
  
  ruby << "  query_params.delete_if { |key, value| value.nil? }\n"
  ruby << "  unless query_params.empty?\n"
  ruby << '    url << "?#{Merb::Parse.params_to_query_string(query_params)}"' << "\n"
  ruby << "  end\n"
  ruby << '  url << "##{fragment}" if fragment' << "\n"
  ruby << "  url\n"
  ruby << "end\n"
  
  eval(ruby)
end