Class: Wayfarer::Routing::Rule

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

Overview

Tree nodes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &proc) ⇒ Rule

Returns a new instance of Rule.



19
20
21
22
23
24
25
# 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

Instance Attribute Details

#child_rulesObject (readonly)

Returns the value of attribute child_rules.



14
15
16
# File 'lib/wayfarer/routing/rule.rb', line 14

def child_rules
  @child_rules
end

#target_actionObject (readonly)

Returns the value of attribute target_action.



15
16
17
# File 'lib/wayfarer/routing/rule.rb', line 15

def target_action
  @target_action
end

Instance Method Details

#build_child_rule_chain_from_options(opts) ⇒ Object



27
28
29
30
# 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) ⇒ Object



90
91
92
# File 'lib/wayfarer/routing/rule.rb', line 90

def filetypes(*argv, &proc)
  append_child_rule(FiletypesRule.new(*argv, &proc))
end

#host(*argv, &proc) ⇒ Object



70
71
72
# File 'lib/wayfarer/routing/rule.rb', line 70

def host(*argv, &proc)
  append_child_rule(HostRule.new(*argv, &proc))
end

#if(*argv, &proc) ⇒ Object



86
87
88
# File 'lib/wayfarer/routing/rule.rb', line 86

def if(*argv, &proc)
  append_child_rule(CustomRule.new(*argv, &proc))
end

#invoke(uri) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# 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 = []) ⇒ Object

rubocop:disable Lint/AssignmentInCondition



51
52
53
54
55
56
57
58
59
# 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

#paramsObject

rubocop:enable Lint/AssignmentInCondition



62
63
64
# File 'lib/wayfarer/routing/rule.rb', line 62

def params(*)
  {}
end

#path(*argv, &proc) ⇒ Object



74
75
76
# File 'lib/wayfarer/routing/rule.rb', line 74

def path(*argv, &proc)
  append_child_rule(PathRule.new(*argv, &proc))
end

#protocol(*argv, &proc) ⇒ Object



82
83
84
# File 'lib/wayfarer/routing/rule.rb', line 82

def protocol(*argv, &proc)
  append_child_rule(ProtocolRule.new(*argv, &proc))
end

#query(*argv, &proc) ⇒ Object



78
79
80
# File 'lib/wayfarer/routing/rule.rb', line 78

def query(*argv, &proc)
  append_child_rule(QueryRule.new(*argv, &proc))
end

#uri(*argv, &proc) ⇒ Object



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

def uri(*argv, &proc)
  append_child_rule(URIRule.new(*argv, &proc))
end