Class: Gin::Router::Route

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_keysObject (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

#nameObject (readonly)

Arbitrary name of the route.



133
134
135
# File 'lib/gin/router.rb', line 133

def name
  @name
end

#param_keysObject (readonly)

Parsed out path param key names.



121
122
123
# File 'lib/gin/router.rb', line 121

def param_keys
  @param_keys
end

#pathObject (readonly)

Computed path String with wildcards.



127
128
129
# File 'lib/gin/router.rb', line 127

def path
  @path
end

#targetObject (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