Class: RoadForest::Application::Route
- Inherits:
-
Webmachine::Dispatcher::Route
- Object
- Webmachine::Dispatcher::Route
- RoadForest::Application::Route
- Defined in:
- lib/roadforest/application/route-adapter.rb
Overview
Extension of Webmachine’s Routes that allows for rendering url paths and parameter lists.
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #build_params(vars = nil) ⇒ Object
-
#build_path(vars = nil) ⇒ String
Create a complete URL for this route, doing any necessary variable substitution.
- #build_pattern(vals = nil, extra_vars = nil) ⇒ Object
- #interface_class ⇒ Object
- #resolve_path_spec(vars) ⇒ Object
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
47 48 49 |
# File 'lib/roadforest/application/route-adapter.rb', line 47 def name @name end |
Instance Method Details
#build_params(vars = nil) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# 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) ⇒ String
Create a complete URL for this route, doing any necessary variable substitution.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# 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) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# 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 ⇒ Object
117 118 119 120 121 122 123 |
# 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) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# 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 |