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

:nodoc:



133
134
135
136
137
138
139
140
# File 'lib/merb-core/dispatch/router/route.rb', line 133

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/merb-core/dispatch/router/route.rb', line 142

def compiled
  ruby  = ""
  ruby << "lambda do |params, defaults|\n"
  ruby << "  fragment     = 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::Request.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