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 (Sybmol) 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:

  • Webmachine::Dispatcher::Route.{Dispatcher{Dispatcher#add_route}


29
30
31
32
# File 'lib/webmachine/dispatcher/route.rb', line 29

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

- (Class) resource (readonly)

Returns 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:



44
45
46
47
48
49
50
# File 'lib/webmachine/dispatcher/route.rb', line 44

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.

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/webmachine/dispatcher/route.rb', line 36

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