Class: Usher::Util::Generators::URL

Inherits:
Generic
  • Object
show all
Defined in:
lib/usher/util/generate.rb

Defined Under Namespace

Classes: UrlParts

Instance Attribute Summary

Attributes inherited from Generic

#usher

Instance Method Summary collapse

Methods inherited from Generic

#generate_path_for_base_params

Constructor Details

#initializeURL

Returns a new instance of URL.



83
84
85
# File 'lib/usher/util/generate.rb', line 83

def initialize
  require File.join('usher', 'util', 'rack-mixins')
end

Instance Method Details

#build_module!Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/usher/util/generate.rb', line 136

def build_module!
  unless @generation_module
    @generation_module = Module.new
    @generation_module.module_eval <<-END_EVAL
      @@generator = nil
      def self.generator=(generator)
        @@generator = generator
      end
    END_EVAL
    @generation_module.generator = self

    @generation_module.module_eval <<-END_EVAL
      def respond_to?(method_name)
        if method_name =~ /^(.*?)_(path|url)$/
          @@generator.usher.named_routes.key?($1)
        else
          super
        end
      end
    END_EVAL


    usher.named_routes.each do |name, route|
      @generation_module.module_eval <<-END_EVAL
        def #{name}_url(options={})
          @@generator.generate_full('#{name}'.to_sym, request, options)
        end

        def #{name}_path(options={})
          @@generator.generate('#{name}'.to_sym, options)
        end
      END_EVAL
    end
  end
end

#generate(routing_lookup, params = nil) ⇒ Object

Generates a completed URL based on a route or set of optional params

set = Usher.new
route = set.add_named_route(:test_route, '/:controller/:action')
set.generator.generate(nil, {:controller => 'c', :action => 'a'}) == '/c/a' => true
set.generator.generate(:test_route, {:controller => 'c', :action => 'a'}) == '/c/a' => true
set.generator.generate(route.primary_path, {:controller => 'c', :action => 'a'}) == '/c/a' => true


101
102
103
# File 'lib/usher/util/generate.rb', line 101

def generate(routing_lookup, params = nil)
  generate_path(path_for_routing_lookup(routing_lookup, params), params)
end

#generate_base_url(params = nil) ⇒ Object



172
173
174
175
176
177
178
179
180
# File 'lib/usher/util/generate.rb', line 172

def generate_base_url(params = nil)
  if usher.parent_route
    usher.parent_route.router.generator.generate_path(usher.parent_route.paths.first, params, false)
  elsif params && params.key?(:default)
    params[:default].to_s
  else
    '/'
  end
end

#generate_extra_params(params, has_question_mark) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/usher/util/generate.rb', line 205

def generate_extra_params(params, has_question_mark)
  extra_params_result = ''

  params.each do |k,v|
    case v
    when Array
      v.each do |v_part|
        extra_params_result << (has_question_mark ? '&' : has_question_mark = true && '?') << Rack::Utils.escape("#{k.to_s}[]") << '=' << Rack::Utils.escape(v_part.to_s)
      end
    else
      extra_params_result << (has_question_mark ? '&' : has_question_mark = true && '?') << Rack::Utils.escape(k.to_s) << '=' << Rack::Utils.escape(v.to_s)
    end
  end
  extra_params_result
end

#generate_full(routing_lookup, request, params = nil) ⇒ Object



87
88
89
90
91
92
# File 'lib/usher/util/generate.rb', line 87

def generate_full(routing_lookup, request, params = nil)
  path = path_for_routing_lookup(routing_lookup, params)

  result = generate_start(path, request)          
  result << generate_path(path, params)
end

#generate_path(path, params = nil, generate_extra = true) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/usher/util/generate.rb', line 105

def generate_path(path, params = nil, generate_extra = true)
  params = Array(params) if params.is_a?(String)
  case params
  when nil
    params = path && path.route.default_values
  when Hash
    params = path.route.default_values.merge(params) if path && path.route.default_values
  when String, Array
    params = Array(params)
    given_size = params.size
    extra_params = params.last.is_a?(Hash) ? params.pop : nil
    params = Hash[*path.dynamic_parts.inject(path.route.default_values ? path.route.default_values.to_a : []){|a, dynamic_part| a.concat([dynamic_part.name, params.shift || raise(MissingParameterException.new("got #{given_size}, expected #{path.dynamic_parts.size} parameters"))]); a}]
    params.merge!(extra_params) if extra_params
  end
  
  result = Rack::Utils.uri_escape(generate_path_for_base_params(path, params))
  unless !generate_extra || params.nil? || params.empty?
    if usher.consider_destination_keys? && path.route.destination_keys
      params.delete_if{|k, v| path.route.destination_keys.include?(k)}
    end
    extra_params = generate_extra_params(params, result[??])
    result << extra_params
  end
  result
end

#generate_start(path, request) ⇒ Object



182
183
184
185
186
187
# File 'lib/usher/util/generate.rb', line 182

def generate_start(path, request)          
  url_parts = UrlParts.new(path, request)
  url = url_parts.url

  (url[-1] == ?/) ? url[0..-2] : url
end

#generation_moduleObject



131
132
133
134
# File 'lib/usher/util/generate.rb', line 131

def generation_module
  build_module!
  @generation_module
end

#path_for_routing_lookup(routing_lookup, params = {}) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/usher/util/generate.rb', line 189

def path_for_routing_lookup(routing_lookup, params = {})
  path = case routing_lookup
  when Symbol
    route = @usher.named_routes[routing_lookup]
    raise UnrecognizedException unless route
    route.find_matching_path(params || {})
  when Route
    routing_lookup.find_matching_path(params)
  when nil
    params.is_a?(Hash) ? usher.path_for_options(params) : raise
  when Route::Path
    routing_lookup
  end
end