class Wayfarer::Routing::Rule

Tree nodes @private

Attributes

child_rules[R]
target_action[R]

Public Class Methods

new(opts = {}, &proc) click to toggle source
# File lib/wayfarer/routing/rule.rb, line 19
def initialize(opts = {}, &proc)
  @child_rules = []
  @target_action = nil

  build_child_rule_chain_from_options(opts)
  instance_eval(&proc) if block_given?
end

Public Instance Methods

build_child_rule_chain_from_options(opts) click to toggle source
# File lib/wayfarer/routing/rule.rb, line 27
def build_child_rule_chain_from_options(opts)
  @target_action = opts.delete(:to)
  opts.reduce(self) { |rule, (key, val)| rule.send(key, val) }
end
filetypes(*argv, &proc) click to toggle source
# File lib/wayfarer/routing/rule.rb, line 90
def filetypes(*argv, &proc)
  append_child_rule(FiletypesRule.new(*argv, &proc))
end
host(*argv, &proc) click to toggle source
# File lib/wayfarer/routing/rule.rb, line 70
def host(*argv, &proc)
  append_child_rule(HostRule.new(*argv, &proc))
end
if(*argv, &proc) click to toggle source
# File lib/wayfarer/routing/rule.rb, line 86
def if(*argv, &proc)
  append_child_rule(CustomRule.new(*argv, &proc))
end
invoke(uri) click to toggle source
# File lib/wayfarer/routing/rule.rb, line 37
def invoke(uri)
  rule_chain = matching_rule_chain(uri)

  if rule_chain.any?
    params = params_from_rule_chain(rule_chain, uri)
    action = action_from_rule_chain(rule_chain)

    [true, params, action]
  else
    false
  end
end
matches?(uri) click to toggle source
# File lib/wayfarer/routing/rule.rb, line 32
def matches?(uri)
  return false unless match!(uri)
  none? || any? { |child_rule| child_rule.matches?(uri) }
end
matching_rule_chain(uri, chain = []) click to toggle source

rubocop:disable Lint/AssignmentInCondition

# File lib/wayfarer/routing/rule.rb, line 51
def matching_rule_chain(uri, chain = [])
  if match!(uri) && none?
    chain << self
  elsif matching_child = detect { |child_rule| child_rule.matches?(uri) }
    matching_child.matching_rule_chain(uri, chain << self)
  else
    []
  end
end
params(*) click to toggle source

rubocop:enable Lint/AssignmentInCondition

# File lib/wayfarer/routing/rule.rb, line 62
def params(*)
  {}
end
path(*argv, &proc) click to toggle source
# File lib/wayfarer/routing/rule.rb, line 74
def path(*argv, &proc)
  append_child_rule(PathRule.new(*argv, &proc))
end
protocol(*argv, &proc) click to toggle source
# File lib/wayfarer/routing/rule.rb, line 82
def protocol(*argv, &proc)
  append_child_rule(ProtocolRule.new(*argv, &proc))
end
query(*argv, &proc) click to toggle source
# File lib/wayfarer/routing/rule.rb, line 78
def query(*argv, &proc)
  append_child_rule(QueryRule.new(*argv, &proc))
end
uri(*argv, &proc) click to toggle source
# File lib/wayfarer/routing/rule.rb, line 66
def uri(*argv, &proc)
  append_child_rule(URIRule.new(*argv, &proc))
end

Private Instance Methods

action_from_rule_chain(rule_chain) click to toggle source
# File lib/wayfarer/routing/rule.rb, line 109
def action_from_rule_chain(rule_chain)
  rule_chain.map(&:target_action).reverse.reject(&:nil?).first
end
append_child_rule(other) click to toggle source
# File lib/wayfarer/routing/rule.rb, line 96
def append_child_rule(other)
  @child_rules << other
  other
end
match!(*) click to toggle source
# File lib/wayfarer/routing/rule.rb, line 101
def match!(*)
  any?
end
params_from_rule_chain(rule_chain, uri) click to toggle source
# File lib/wayfarer/routing/rule.rb, line 105
def params_from_rule_chain(rule_chain, uri)
  rule_chain.reduce({}) { |hash, rule| hash.merge(rule.params(uri)) }
end