Class: Wayfarer::Routing::Router
- Inherits:
-
Object
- Object
- Wayfarer::Routing::Router
- Extended by:
- Forwardable
- Defined in:
- lib/wayfarer/routing/router.rb
Overview
Instance Attribute Summary collapse
-
#blacklist ⇒ Rule
readonly
-
#rule ⇒ Rule
readonly
Instance Method Summary collapse
-
#allows?(uri) ⇒ Boolean
Whether the URI is allowed.
-
#forbid(opts = {}, &proc) ⇒ Object
Adds a Rule to the blacklist.
-
#forbids?(uri) ⇒ Boolean
Whether the URI is matched by the blacklist rule.
-
#initialize ⇒ Router
constructor
A new instance of Router.
-
#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.
-
#routes?(uri) ⇒ Boolean
Whether a route matches the URI.
Constructor Details
Instance Attribute Details
#blacklist ⇒ Rule (readonly)
17 18 19 |
# File 'lib/wayfarer/routing/router.rb', line 17 def blacklist @blacklist end |
#rule ⇒ Rule (readonly)
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.
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.(opts) @blacklist.instance_eval(&proc) if block_given? @blacklist end |
#forbids?(uri) ⇒ Boolean
Whether the URI is matched by the blacklist rule.
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.
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
47 48 49 |
# File 'lib/wayfarer/routing/router.rb', line 47 def routes?(uri) !!route(uri) end |