Class: Webmachine::Dispatcher::Route
- Inherits:
-
Object
- Object
- Webmachine::Dispatcher::Route
- Includes:
- Translation
- Defined in:
- lib/webmachine/dispatcher/route.rb
Overview
Pairs URIs with Resource classes in the Webmachine::Dispatcher. To create routes, use #add_route.
Constant Summary collapse
- MATCH_ALL =
When used in a path specification, will match all remaining segments
:*
- MATCH_ALL_STR =
String version of MATCH_ALL, deprecated. Use the symbol instead.
'*'.freeze
Instance Attribute Summary collapse
-
#guards ⇒ Array<Proc>
readonly
The list of guard blocks used to define this route (see #initialize).
-
#path_spec ⇒ Array<String|Symbol>
readonly
The list of path segments used to define this route (see #initialize).
-
#resource ⇒ Class
readonly
The resource this route will dispatch to, a subclass of Resource.
Class Method Summary collapse
-
.rfc3986_percent_decode(value) ⇒ Object
Decode a string using the scheme described in RFC 3986 2.1.
Instance Method Summary collapse
-
#apply(request) ⇒ Object
Decorates the request with information about the dispatch route, including path bindings.
-
#initialize(path_spec, *guards, resource, bindings = {}) {|req| ... } ⇒ Route
constructor
Creates a new Route that will associate a pattern to a Resource.
-
#match?(request) ⇒ Boolean
Determines whether the given request matches this route and should be dispatched to the #resource.
Methods included from Translation
Constructor Details
#initialize(path_spec, *guards, resource, bindings = {}) {|req| ... } ⇒ Route
Creates a new Route that will associate a pattern to a Resource.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/webmachine/dispatcher/route.rb', line 76 def initialize(path_spec, *args, &block) bindings = if args.last.is_a? Hash args.pop else {} end resource = args.pop guards = args guards << block if block warn t('match_all_symbol') if path_spec.include? MATCH_ALL_STR @path_spec = path_spec @guards = guards @resource = resource @bindings = bindings raise ArgumentError, t('not_resource_class', class: resource.name) unless resource < Resource end |
Instance Attribute Details
#guards ⇒ Array<Proc> (readonly)
Returns the list of guard blocks used to define this route (see #initialize).
22 23 24 |
# File 'lib/webmachine/dispatcher/route.rb', line 22 def guards @guards end |
#path_spec ⇒ Array<String|Symbol> (readonly)
Returns the list of path segments used to define this route (see #initialize).
18 19 20 |
# File 'lib/webmachine/dispatcher/route.rb', line 18 def path_spec @path_spec end |
#resource ⇒ Class (readonly)
Returns the resource this route will dispatch to, a subclass of Resource.
14 15 16 |
# File 'lib/webmachine/dispatcher/route.rb', line 14 def resource @resource end |
Class Method Details
.rfc3986_percent_decode(value) ⇒ Object
Decode a string using the scheme described in RFC 3986 2.1. Percent-Encoding (www.ietf.org/rfc/rfc3986.txt)
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/webmachine/dispatcher/route.rb', line 32 def self.rfc3986_percent_decode(value) s = StringScanner.new(value) result = '' until s.eos? encoded_val = s.scan(/%([0-9a-fA-F]){2}/) result << if encoded_val.nil? s.getch else [encoded_val[1..]].pack('H*') end end result end |
Instance Method Details
#apply(request) ⇒ Object
Decorates the request with information about the dispatch route, including path bindings.
108 109 110 111 112 113 114 |
# File 'lib/webmachine/dispatcher/route.rb', line 108 def apply(request) request.disp_path = request.routing_tokens.join(SLASH) request.path_info = @bindings.dup tokens = request.routing_tokens _depth, trailing = bind(tokens, request.path_info) request.path_tokens = trailing || [] end |
#match?(request) ⇒ Boolean
Determines whether the given request matches this route and should be dispatched to the #resource.
100 101 102 103 |
# File 'lib/webmachine/dispatcher/route.rb', line 100 def match?(request) tokens = request.routing_tokens bind(tokens, {}) && guards.all? { |guard| guard.call(request) } end |