Class: Doze::Router::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/doze/router/route.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#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

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



54
55
56
# File 'lib/doze/router/route.rb', line 54

def block
  @block
end

#nameObject (readonly)

Returns the value of attribute name.



54
55
56
# File 'lib/doze/router/route.rb', line 54

def name
  @name
end

#session_specificObject (readonly)

Returns the value of attribute session_specific.



54
55
56
# File 'lib/doze/router/route.rb', line 54

def session_specific
  @session_specific
end

#staticObject (readonly)

Returns the value of attribute static.



54
55
56
# File 'lib/doze/router/route.rb', line 54

def static
  @static
end

#target_route_setObject (readonly)

Returns the value of attribute target_route_set.



54
55
56
# File 'lib/doze/router/route.rb', line 54

def target_route_set
  @target_route_set
end

Instance Method Details

#call(router, vars = nil, session = nil, base_uri = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/doze/router/route.rb', line 63

def call(router, vars=nil, session=nil, base_uri=nil)
  return @target_instance if @target_instance
  base_uri ||= expand(vars, router.router_uri_prefix) if router
  args = [base_uri]
  args << vars if vars && !vars.empty?
  args << session if @session_specific
  args.unshift(router) unless @static
  @block.call(*args)
end

#expand(vars, prefix = nil) ⇒ Object



77
78
79
# File 'lib/doze/router/route.rb', line 77

def expand(vars, prefix=nil)
  template(prefix).expand(vars)
end

#match(path) ⇒ Object



73
74
75
# File 'lib/doze/router/route.rb', line 73

def match(path)
  @template.match_with_trailing(path)
end

#parameterized?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/doze/router/route.rb', line 85

def parameterized?
  !@template.variables.empty?
end

#partially_expand(vars, prefix = nil) ⇒ Object



81
82
83
# File 'lib/doze/router/route.rb', line 81

def partially_expand(vars, prefix=nil)
  template(prefix).partially_expand(vars)
end

#routes_uniquely_to_target?Boolean

Returns:

  • (Boolean)


56
# File 'lib/doze/router/route.rb', line 56

def routes_uniquely_to_target?; @routes_uniquely_to_target; end

#static?Boolean

Returns:

  • (Boolean)


57
# File 'lib/doze/router/route.rb', line 57

def static?; @static; end

#template(prefix = nil) ⇒ Object



59
60
61
# File 'lib/doze/router/route.rb', line 59

def template(prefix=nil)
  prefix ? @template.with_prefix(prefix) : @template
end