class Wayfarer::Routing::Router

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

Attributes

blacklist[R]

@!attribute [r] blacklist @return [Rule]

rule[R]

@!attribute [r] rule @return [Rule]

Public Class Methods

new() click to toggle source
# File lib/wayfarer/routing/router.rb, line 19
def initialize
  @rule = Rule.new
  @blacklist = Rule.new
end

Public Instance Methods

allows?(uri) click to toggle source

Whether the URI is allowed. @see forbid

# File lib/wayfarer/routing/router.rb, line 66
def allows?(uri)
  !forbids?(uri)
end
forbid(opts = {}, &proc) click to toggle source

Adds a {Rule} to the blacklist.

# 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) click to toggle source

Whether the URI is matched by the blacklist rule. @see forbid

# File lib/wayfarer/routing/router.rb, line 60
def forbids?(uri)
  @blacklist.matches?(uri)
end
route(uri) click to toggle source

Returns the associated instance method (action) of the first rule that matches a URI and the collected parameter hash from the rule chain. @return [[Boolean, Symbol, Hash]] if a matching rule exists. @return [false] if no matching rule exists or the URI is forbidden.

# 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) click to toggle source

Whether a route matches the URI. TODO: Test

# File lib/wayfarer/routing/router.rb, line 47
def routes?(uri)
  !!route(uri)
end