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:



66
67
68
# File 'lib/wayfarer/routing/router.rb', line 66

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

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

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



52
53
54
55
56
# File 'lib/wayfarer/routing/router.rb', line 52

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:



60
61
62
# File 'lib/wayfarer/routing/router.rb', line 60

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.



35
36
37
38
39
40
41
42
43
# File 'lib/wayfarer/routing/router.rb', line 35

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)


47
48
49
# File 'lib/wayfarer/routing/router.rb', line 47

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