Class: Webmachine::Dispatcher::Route

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

Instance Method Summary collapse

Methods included from Translation

#t

Constructor Details

#initialize(path_spec, *guards, resource, bindings = {}) {|req| ... } ⇒ Route

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

Examples:

Standard route

Route.new([:*], MyResource)

Guarded route

Route.new ["/notes"],
  ->(request) { request.method == "POST" },
  Resources::Note
Route.new ["/notes"], Resources::NoteList
Route.new ["/notes", :id], Resources::Note
Route.new ["/notes"], Resources::Note do |req|
  req.query['foo']
end

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.

  • guards (Proc)

    optional guard blocks called with the request.

  • resource (Class)

    the Resource to dispatch to

  • bindings (Hash) (defaults to: {})

    additional information to add to Request#path_info when this route matches

Yields:

  • (req)

    an optional guard block

Yield Parameters:

  • req (Request)

    the request object

Raises:

  • (ArgumentError)

See Also:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/webmachine/dispatcher/route.rb', line 61

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?

  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

#guardsArray<Proc> (readonly)

Returns the list of guard blocks used to define this route (see #initialize).

Returns:

  • (Array<Proc>)

    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_specArray<String|Symbol> (readonly)

Returns 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).



18
19
20
# File 'lib/webmachine/dispatcher/route.rb', line 18

def path_spec
  @path_spec
end

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



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

def resource
  @resource
end

Instance Method Details

#apply(request) ⇒ Object

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

Parameters:

  • request (Request)

    the request object



93
94
95
96
97
98
99
# File 'lib/webmachine/dispatcher/route.rb', line 93

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.

Parameters:

  • request (Reqeust)

    the request object

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/webmachine/dispatcher/route.rb', line 85

def match?(request)
  tokens = request.routing_tokens
  bind(tokens, {}) && guards.all? { |guard| guard.call(request) }
end