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

Inherits:
Object
  • Object
show all
Defined in:
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

:api: private



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

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

:api: private



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

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