class RoadForest::Application::Route

Extension of Webmachine’s Routes that allows for rendering url paths and parameter lists.

Attributes

name[RW]

Public Instance Methods

build_params(vars = nil) click to toggle source
# File lib/roadforest/application/route-adapter.rb, line 125
def build_params(vars = nil)
  vars ||= {}
  params = Application::Parameters.new
  path_set = Hash[path_spec.find_all{|segment| segment.is_a? Symbol}.map{|seg| [seg, true]}]
  vars.to_hash.each do |key, value|
    if(path_set.has_key?(key))
      params.path_info[key] = value
    elsif(key == '*')
      params.path_tokens = value
    else
      params.query_params[key] = value
    end
  end
  params
end
build_path(vars = nil) click to toggle source

Create a complete URL for this route, doing any necessary variable substitution. @param [Hash] vars values for the path variables @return [String] the valid URL for the route

# File lib/roadforest/application/route-adapter.rb, line 52
def build_path(vars = nil)
  vars ||= {}
  vars = vars.to_hash
  vars = vars.dup
  path_spec = resolve_path_spec(vars)
  if path_spec.any?{|segment| segment.is_a?(Symbol) or segment == "*"}
    raise "Cannot build path - missing vars: #{path_spec.inspect}"
  end
  path = "/" + path_spec.join("/")
  vars.delete('*')
  unless vars.empty?
    path += "?" + vars.map do |key,value|
      [key,value].join("=")
    end.join("&")
  end
  return path
end
build_pattern(vals = nil, extra_vars = nil) click to toggle source
# File lib/roadforest/application/route-adapter.rb, line 70
def build_pattern(vals = nil, extra_vars = nil)
  vals ||= {}
  extra_vars ||= []
  vals = vals.to_hash
  vals = vals.dup
  extra_vars -= path_spec.find_all{|segment| segment.is_a? Symbol}

  pattern_spec = resolve_path_spec(vals)

  pattern = pattern_spec.map do |segment|
    case segment
    when '*'
      "{/rest*}"
    when Symbol
      "{/#{segment}}"
    else
      "/" + segment
    end
  end.join("")

  vals.delete('*')
  unless vals.empty?
    pattern += "?" + vals.map do |key,value|
      [key,value].join("=")
    end.join("&")
  end
  unless extra_vars.empty?
    pattern += "{?#{extra_vars.join(",")}}"
  end
  return pattern
end
interface_class() click to toggle source
# File lib/roadforest/application/route-adapter.rb, line 117
def interface_class
  if resource.respond_to? :interface_class
    resource.interface_class
  else
    nil
  end
end
resolve_path_spec(vars) click to toggle source
# File lib/roadforest/application/route-adapter.rb, line 102
def resolve_path_spec(vars)
  path_spec.map do |segment|
    case segment
    when '*',Symbol
      if (string = vars.delete(segment)).nil?
        segment
      else
        string
      end
    when String
      segment
    end
  end
end