Method: Doze::Router::Route#initialize

Defined in:
lib/doze/router/route.rb

#initialize(template, options = {}, &block) ⇒ Route

Route options:

:name             :: override default name, which is based on stripping non-alphanumerics from the route template
:session_specific :: pass session as an extra last argument to the routing block
:static           :: route is not dependent on the parent router instance; parent router instance will not be
                     passed as first argument to routing block
:to (Router or Resource)
                  :: Route directly to this instance. Only for use where it is reachable by the
                     propagate_static_routes or where its uri is otherwise guaranteed to be the one routed to
:to (Class)       :: Pass routing arguments to the constructor of this class, instead of block.
                     Assumes :static => true by default.
                  :: Whenever :to => xyz routing is used, and the target is an AnchoredRouteSet (eg a Router instance or
                     a class that includes Router) remembers this as the target_route_set of the route.
:uniquely         :: for use with :to, indicates that this is the only, unique route to the given target.
                     where the target uri is statically inferable, allows propagate_static_routes to set the uri
                     on the target_route_set.
:uniquely_to      :: short-cut for :to => foo, :uniquely => true
&block            :: block which is passed (router, matched_uri, matched_vars_from_template, session), except:
                     * router is ommitted when :static
                     * matched_vars_from_template is ommitted when the template has no vars (!parameterized?)
                     * session is ommitted unless :session_specific set
                     not required where :to is used.


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/doze/router/route.rb', line 24

def initialize(template, options={}, &block)
  template = Doze::URITemplate.compile(template, options[:regexps] || {}) unless template.is_a?(Doze::URITemplate)
  @template = template
  @name = options[:name] || template.to_s.gsub(/[^a-z0-9]+/, '_').match(/^_?(.*?)_?$/)[1]
  @session_specific = options[:session_specific]
  @static = options[:static]

  if options[:uniquely_to]
    options[:uniquely] = true
    options[:to] = options[:uniquely_to]
  end

  target = options[:to]
  case target
  when Doze::Router, Doze::Resource
    @static = true
    @target_instance = target
  when Class
    @static = true if @static.nil?
    @block = block || target.method(:new)
  else
    @block = block or raise "You must specify :to or give a block"
  end

  if target.is_a?(Doze::Router::AnchoredRouteSet)
    @target_route_set = target
    @routes_uniquely_to_target = !options[:uniquely].nil?
  end
end