Parent

Files

Class/Module Index [+]

Quicksearch

Dragonfly::UrlMapper

Attributes

segments[R]
url_format[R]
url_regexp[R]

Public Class Methods

new(url_format, patterns={}) click to toggle source
# File lib/dragonfly/url_mapper.rb, line 13
def initialize(url_format, patterns={})
  @url_format = url_format
  raise BadUrlFormat, "bad url format #{url_format}" if url_format[/[\w_]:[\w_]/]
  init_segments(patterns)
  init_url_regexp
end

Public Instance Methods

params_for(path, query=nil) click to toggle source
# File lib/dragonfly/url_mapper.rb, line 22
def params_for(path, query=nil)
  if path and md = path.match(url_regexp)
    params = Rack::Utils.parse_query(query)
    params_in_url.each_with_index do |var, i|
      value = md[i+1][1..-1] if md[i+1]
      params[var] = value && Utils.uri_unescape(value)
    end
    params
  end
end
params_in_url() click to toggle source
# File lib/dragonfly/url_mapper.rb, line 33
def params_in_url
  @params_in_url ||= url_format.scan(/\:[\w_]+/).map{|f| f.tr(':','') }
end
url_for(params) click to toggle source
# File lib/dragonfly/url_mapper.rb, line 37
def url_for(params)
  params = params.dup
  url = url_format.dup
  segments.each do |seg|
    value = params[seg.param]
    value ? url.sub!(/:[\w_]+/, Utils.uri_escape_segment(value.to_s)) : url.sub!(/.:[\w_]+/, '')
    params.delete(seg.param)
  end
  url << "?#{Rack::Utils.build_query(params)}" if params.any?
  url
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.