Class: HttpRouter::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/http_router/node.rb

Direct Known Subclasses

ArbitraryNode, RequestNode, Root

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(router) ⇒ Node

Returns a new instance of Node.



6
7
8
9
# File 'lib/http_router/node.rb', line 6

def initialize(router)
  @router = router
  reset!
end

Instance Attribute Details

#arbitrary_nodeObject (readonly)

Returns the value of attribute arbitrary_node.



4
5
6
# File 'lib/http_router/node.rb', line 4

def arbitrary_node
  @arbitrary_node
end

#catchallObject

Returns the value of attribute catchall.



3
4
5
# File 'lib/http_router/node.rb', line 3

def catchall
  @catchall
end

#linearObject (readonly)

Returns the value of attribute linear.



4
5
6
# File 'lib/http_router/node.rb', line 4

def linear
  @linear
end

#lookupObject (readonly)

Returns the value of attribute lookup.



4
5
6
# File 'lib/http_router/node.rb', line 4

def lookup
  @lookup
end

#request_nodeObject (readonly)

Returns the value of attribute request_node.



4
5
6
# File 'lib/http_router/node.rb', line 4

def request_node
  @request_node
end

#valueObject

Returns the value of attribute value.



3
4
5
# File 'lib/http_router/node.rb', line 3

def value
  @value
end

#variableObject

Returns the value of attribute variable.



3
4
5
# File 'lib/http_router/node.rb', line 3

def variable
  @variable
end

Instance Method Details

#add(val) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/http_router/node.rb', line 15

def add(val)
  if val.respond_to?(:matches?)
    if val.matches_with
      add_to_linear(val)
    else
      @catchall ||= router.node
      @catchall.variable = val
      @catchall
    end
  elsif val.is_a?(Regexp)
    add_to_linear(val)
  else
    create_lookup
    @lookup[val] ||= router.node
  end
end

#add_arbitrary(procs) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/http_router/node.rb', line 57

def add_arbitrary(procs)
  target = self
  if procs && !procs.empty?
    @arbitrary_node ||= router.arbitrary_node
    @arbitrary_node.create_linear
    target = router.node
    @arbitrary_node.linear << [procs, target]
    if @value
      @arbitrary_node.catchall = router.node
      @arbitrary_node.catchall.value = @value
      @value = nil
    end
  elsif @arbitrary_node
    target = @arbitrary_node.catchall = router.node
  end
  target
end

#add_request_methods(options) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/http_router/node.rb', line 32

def add_request_methods(options)
  if !options.empty?
    generate_request_method_tree(options)
  elsif @request_node
    current_node = @request_node
    while current_node.request_method
      current_node = (current_node.catchall ||= router.request_node)
    end
    [current_node]
  else
    [self]
  end
end

#add_to_linear(val) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/http_router/node.rb', line 46

def add_to_linear(val)
  create_linear
  if @linear.assoc(val)
    @linear.assoc(val).last
  else
    new_node = router.node
    @linear << [val, new_node]
    new_node
  end
end

#reset!Object



11
12
13
# File 'lib/http_router/node.rb', line 11

def reset!
  @linear, @lookup, @catchall = nil, nil, nil
end