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
- MATCH_ALL =
When used in a path specification, will match all remaining segments
'*'.freeze
Instance Attribute Summary (collapse)
-
- (Array<Proc>) guards
readonly
The list of guard blocks used to define this route (see #initialize).
-
- (Array<String|Symbol>) path_spec
readonly
The list of path segments used to define this route (see #initialize).
-
- (Class) resource
readonly
The resource this route will dispatch to, a subclass of Resource.
Instance Method Summary (collapse)
-
- (Object) apply(request)
Decorates the request with information about the dispatch route, including path bindings.
-
- (Route) initialize(path_spec, *guards, resource, bindings = {}) {|req| ... }
constructor
Creates a new Route that will associate a pattern to a Resource.
-
- (Boolean) match?(request)
Determines whether the given request matches this route and should be dispatched to the #resource.
Methods included from Translation
Constructor Details
- (Route) initialize(path_spec, *guards, resource, bindings = {}) {|req| ... }
Creates a new Route that will associate a pattern to a Resource.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/webmachine/dispatcher/route.rb', line 57 def initialize(path_spec, *args) if args.last.is_a? Hash bindings = args.pop else bindings = {} end resource = args.pop guards = args guards << Proc.new if block_given? @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
- (Array<Proc>) guards (readonly)
The list of guard blocks used to define this route (see #initialize).
21 22 23 |
# File 'lib/webmachine/dispatcher/route.rb', line 21 def guards @guards end |
- (Array<String|Symbol>) path_spec (readonly)
The list of path segments used to define this route (see #initialize).
17 18 19 |
# File 'lib/webmachine/dispatcher/route.rb', line 17 def path_spec @path_spec end |
- (Class) resource (readonly)
The resource this route will dispatch to, a subclass of Resource
13 14 15 |
# File 'lib/webmachine/dispatcher/route.rb', line 13 def resource @resource end |
Instance Method Details
- (Object) apply(request)
Decorates the request with information about the dispatch route, including path bindings.
87 88 89 90 91 92 93 |
# File 'lib/webmachine/dispatcher/route.rb', line 87 def apply(request) request.disp_path = request.uri.path.match(/^\/(.*)/)[1] request.path_info = @bindings.dup tokens = request.disp_path.split('/') depth, trailing = bind(tokens, request.path_info) request.path_tokens = trailing || [] end |
- (Boolean) match?(request)
Determines whether the given request matches this route and should be dispatched to the #resource.
79 80 81 82 |
# File 'lib/webmachine/dispatcher/route.rb', line 79 def match?(request) tokens = request.uri.path.match(/^\/(.*)/)[1].split('/') bind(tokens, {}) && guards.all? { |guard| guard.call(request) } end |