Class: HttpRouter::Node

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

Direct Known Subclasses

RequestNode, Root

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Node

Returns a new instance of Node.



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

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

Instance Attribute Details

#catchallObject

Returns the value of attribute catchall.



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

def catchall
  @catchall
end

#extension_nodeObject (readonly)

Returns the value of attribute extension_node.



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

def extension_node
  @extension_node
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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/http_router/node.rb', line 17

def add(val)
  if val.is_a?(Variable)
    if val.matches_with
      new_node = router.node
      create_linear
      @linear << [val, new_node]
      new_node
    else
      @catchall ||= router.node
      @catchall.variable = val
      @catchall
    end
  elsif val.is_a?(Regexp)
    create_linear
    @linear << [val, router.node]
    @linear.last.last
  else
    create_lookup
    @lookup[val] ||= router.node
  end
end

#add_extension(ext) ⇒ Object



39
40
41
42
# File 'lib/http_router/node.rb', line 39

def add_extension(ext)
  @extension_node ||= router.node
  @extension_node.add(ext)
end

#add_request_methods(options) ⇒ Object



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

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

#reset!Object



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

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