Class: Webmachine::Dispatcher::Route

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

Instance Method Summary (collapse)

Constructor Details

- (Route) initialize(path_spec, resource, bindings = {})

Creates a new Route that will associate a pattern to a Resource.

Parameters:

  • path_spec (Array<String|Symbol>)
    a list of path segments (String) and identifiers (Symbol) to bind. Strings will be simply matched for equality. Symbols in the path spec will be extracted into Request#path_info for use inside your Resource. The special segment MATCH_ALL will match all remaining segments.
  • resource (Class)
    the Resource to dispatch to
  • bindings (Hash) (defaults to: {})
    additional information to add to Request#path_info when this route matches

Raises:

  • (ArgumentError)

See Also:



33
34
35
36
# File 'lib/webmachine/dispatcher/route.rb', line 33

def initialize(path_spec, resource, bindings={})
  @path_spec, @resource, @bindings = path_spec, resource, bindings
  raise ArgumentError, t('not_resource_class', :class => resource.name) unless resource < Resource
end

Instance Attribute Details

- (Array<String|Symbol>) path_spec (readonly)

The list of path segments used to define this route (see #initialize).

Returns:

  • (Array<String|Symbol>)
    the list of path segments used to define this route (see #initialize).


15
16
17
# File 'lib/webmachine/dispatcher/route.rb', line 15

def path_spec
  @path_spec
end

- (Class) resource (readonly)

The resource this route will dispatch to, a subclass of Resource

Returns:

  • (Class)
    the resource this route will dispatch to, a subclass of Resource


11
12
13
# File 'lib/webmachine/dispatcher/route.rb', line 11

def resource
  @resource
end

Instance Method Details

- (Object) apply(request)

Decorates the request with information about the dispatch route, including path bindings.

Parameters:

  • request (Request)
    the request object


49
50
51
52
53
54
55
# File 'lib/webmachine/dispatcher/route.rb', line 49

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.

Parameters:

  • request (Reqeust)
    the request object

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/webmachine/dispatcher/route.rb', line 41

def match?(request)
  tokens = request.uri.path.match(/^\/(.*)/)[1].split('/')
  bind(tokens, {})
end