Class: Gin::Router::Route
- Inherits:
-
Object
- Object
- Gin::Router::Route
- Defined in:
- lib/gin/router.rb
Overview
Represents an HTTP path and path matcher, with inline params, and new path generation functionality.
r = Route.new "get", "/foo/:id.:format", [FooController, :show], :show_foo
r.to_path id: 123, format: "json"
#=> "/foo/123.json"
Constant Summary collapse
- SEP =
:nodoc:
"/"
- VAR_MATCHER =
:nodoc:
/:(\w+)/
- PARAM_MATCHER =
:nodoc:
"(.*?)"
Instance Attribute Summary collapse
-
#match_keys ⇒ Object
readonly
Array of path parts for tree-based matching.
-
#name ⇒ Object
readonly
Arbitrary name of the route.
-
#param_keys ⇒ Object
readonly
Parsed out path param key names.
-
#path ⇒ Object
readonly
Computed path String with wildcards.
-
#target ⇒ Object
readonly
Target of the route, in this case an Array with controller and action.
Instance Method Summary collapse
-
#===(other) ⇒ Object
Returns true if the argument matches the route_id.
-
#initialize(verb, path, target, name) ⇒ Route
constructor
A new instance of Route.
-
#to_path(params = {}) ⇒ Object
Render a route path by giving it inline (and other) params.
Constructor Details
#initialize(verb, path, target, name) ⇒ Route
Returns a new instance of Route.
140 141 142 143 144 |
# File 'lib/gin/router.rb', line 140 def initialize verb, path, target, name @target = target @name = name build verb, path end |
Instance Attribute Details
#match_keys ⇒ Object (readonly)
Array of path parts for tree-based matching.
124 125 126 |
# File 'lib/gin/router.rb', line 124 def match_keys @match_keys end |
#name ⇒ Object (readonly)
Arbitrary name of the route.
133 134 135 |
# File 'lib/gin/router.rb', line 133 def name @name end |
#param_keys ⇒ Object (readonly)
Parsed out path param key names.
121 122 123 |
# File 'lib/gin/router.rb', line 121 def param_keys @param_keys end |
#path ⇒ Object (readonly)
Computed path String with wildcards.
127 128 129 |
# File 'lib/gin/router.rb', line 127 def path @path end |
#target ⇒ Object (readonly)
Target of the route, in this case an Array with controller and action.
130 131 132 |
# File 'lib/gin/router.rb', line 130 def target @target end |
Instance Method Details
#===(other) ⇒ Object
Returns true if the argument matches the route_id. The route id is an array with verb and original path.
169 170 171 |
# File 'lib/gin/router.rb', line 169 def === other @route_id == other end |
#to_path(params = {}) ⇒ Object
Render a route path by giving it inline (and other) params.
route.to_path id: 123, format: "json", foo: "bar"
#=> "/foo/123.json?foo=bar"
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/gin/router.rb', line 152 def to_path params={} rendered_path = @path.dup rendered_path = rendered_path % @param_keys.map do |k| val = params.delete(k) || params.delete(k.to_sym) raise(PathArgumentError, "Missing param #{k}") unless val CGI.escape(val.to_s) end unless @param_keys.empty? rendered_path << "?#{Gin.build_query(params)}" unless params.empty? rendered_path end |