Class: Wayfarer::Routing::Router

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/wayfarer/routing/router.rb

Overview

A Router maps URIs onto a Job's instance methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.


19
20
21
22
# File 'lib/wayfarer/routing/router.rb', line 19

def initialize
  @rule = Rule.new
  @blacklist = Rule.new
end

Instance Attribute Details

#blacklistRule (readonly)

Returns:


17
18
19
# File 'lib/wayfarer/routing/router.rb', line 17

def blacklist
  @blacklist
end

#ruleRule (readonly)

Returns:


13
14
15
# File 'lib/wayfarer/routing/router.rb', line 13

def rule
  @rule
end

Instance Method Details

#allows?(uri) ⇒ Boolean

Whether the URI is allowed.

Returns:

  • (Boolean)

See Also:


69
70
71
# File 'lib/wayfarer/routing/router.rb', line 69

def allows?(uri)
  !forbids?(uri)
end

#forbid(opts = {}, &proc) ⇒ Object

Adds a Wayfarer::Routing::Rule to the blacklist.


55
56
57
58
59
# File 'lib/wayfarer/routing/router.rb', line 55

def forbid(opts = {}, &proc)
  @blacklist.build_child_rule_chain_from_options(opts)
  @blacklist.instance_eval(&proc) if block_given?
  @blacklist
end

#forbids?(uri) ⇒ Boolean

Whether the URI is matched by the blacklist rule.

Returns:

  • (Boolean)

See Also:


63
64
65
# File 'lib/wayfarer/routing/router.rb', line 63

def forbids?(uri)
  @blacklist.matches?(uri)
end

#route(uri) ⇒ [Boolean, Symbol, Hash], false

Returns the associated instance method (action) of the first rule that matches a URI and the collected parameter hash from the rule chain.

Returns:

  • ([Boolean, Symbol, Hash])

    if a matching rule exists.

  • (false)

    if no matching rule exists or the URI is forbidden.


38
39
40
41
42
43
44
45
46
# File 'lib/wayfarer/routing/router.rb', line 38

def route(uri)
  return false if forbids?(uri)

  # TODO: Use structs instead
  is_matching, params, action = @rule.invoke(uri)
  return action, params if is_matching && params

  false
end

#routes?(uri) ⇒ Boolean

Whether a route matches the URI. TODO: Test

Returns:

  • (Boolean)

50
51
52
# File 'lib/wayfarer/routing/router.rb', line 50

def routes?(uri)
  !!route(uri)
end