Class: Rage::Router::StaticNode

Inherits:
ParentNode show all
Defined in:
lib/rage/router/node.rb

Constant Summary

Constants inherited from Node

Node::PARAMETRIC, Node::STATIC, Node::WILDCARD

Instance Attribute Summary collapse

Attributes inherited from ParentNode

#static_children

Attributes inherited from Node

#handler_storage, #is_leaf_node, #kind

Instance Method Summary collapse

Methods inherited from ParentNode

#create_static_child, #find_static_matching_child

Methods inherited from Node

#add_route

Constructor Details

#initialize(prefix) ⇒ StaticNode

Returns a new instance of StaticNode.



71
72
73
74
75
76
77
78
79
# File 'lib/rage/router/node.rb', line 71

def initialize(prefix)
  super()
  @prefix = prefix
  @wildcard_child = nil
  @parametric_children = []
  @kind = Node::STATIC

  compile_prefix_match
end

Instance Attribute Details

#match_prefixObject (readonly)

Returns the value of attribute match_prefix.



69
70
71
# File 'lib/rage/router/node.rb', line 69

def match_prefix
  @match_prefix
end

#prefixObject (readonly)

Returns the value of attribute prefix.



69
70
71
# File 'lib/rage/router/node.rb', line 69

def prefix
  @prefix
end

Instance Method Details

#create_parametric_child(static_suffix, node_path) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rage/router/node.rb', line 81

def create_parametric_child(static_suffix, node_path)
  parametric_child = @parametric_children[0]

  if parametric_child
    parametric_child.node_paths.add(node_path)
    return parametric_child
  end

  parametric_child = ParametricNode.new(static_suffix, node_path)
  @parametric_children << parametric_child
  @parametric_children.sort! do |child1, child2|
    if child1.static_suffix.nil?
      1
    elsif child2.static_suffix.nil?
      -1
    elsif child2.static_suffix.end_with?(child1.static_suffix)
      1
    elsif child1.static_suffix.end_with?(child2.static_suffix)
      -1
    else
      0
    end
  end

  parametric_child
end

#create_wildcard_childObject



108
109
110
# File 'lib/rage/router/node.rb', line 108

def create_wildcard_child
  @wildcard_child ||= WildcardNode.new
end

#get_next_node(path, path_index, node_stack, params_count) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rage/router/node.rb', line 126

def get_next_node(path, path_index, node_stack, params_count)
  node = find_static_matching_child(path, path_index)
  parametric_brother_node_index = 0

  unless node
    return @wildcard_child if @parametric_children.empty?

    node = @parametric_children[0]
    parametric_brother_node_index = 1
  end

  if @wildcard_child
    node_stack << {
      params_count: params_count,
      brother_path_index: path_index,
      brother_node: @wildcard_child
    }
  end

  i = @parametric_children.length - 1
  while i >= parametric_brother_node_index
    node_stack << {
      params_count: params_count,
      brother_path_index: path_index,
      brother_node: @parametric_children[i]
    }
    i -= 1
  end

  node
end

#split(parent_node, length) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rage/router/node.rb', line 112

def split(parent_node, length)
  parent_prefix = @prefix[0, length]
  child_prefix = @prefix[length, @prefix.length - length]

  @prefix = child_prefix
  compile_prefix_match

  static_node = StaticNode.new(parent_prefix)
  static_node.static_children[child_prefix[0]] = self
  parent_node.static_children[parent_prefix[0]] = static_node

  static_node
end