Class: ReeRoda::BuildRoutingTree
- Inherits:
-
Object
- Object
- ReeRoda::BuildRoutingTree
- Includes:
- Ree::FnDSL
- Defined in:
- lib/ree_lib/packages/ree_roda/package/ree_roda/services/build_routing_tree.rb
Defined Under Namespace
Classes: RoutingTree
Instance Method Summary collapse
Instance Method Details
#call(routes) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/ree_lib/packages/ree_roda/package/ree_roda/services/build_routing_tree.rb', line 174 def call(routes) trees = [] routes.each do |route| splitted = route.path.split("/").reject(&:empty?) matched_tree = nil root_part = splitted[0] # find tree for root segment trees.each do |tree| if tree.values.include?(root_part) matched_tree = tree break end end if matched_tree.nil? # create new tree for root matched_tree = RoutingTree.new([root_part], 0, :string) trees << matched_tree end parent_tree = matched_tree # process other parts splitted[1..-1].each_with_index do |v, i| current = parent_tree.find_by_value(value: v, depth: i+1) if current parent_tree = current else # check if we can add it to existing param node if parent_tree.children.any? { |c| c.type == :param } && v.start_with?(":") param_child = parent_tree.children.find { |c| c.type == :param } param_child.values << v if !param_child.values.include?(v) parent_tree = param_child else new_tree = parent_tree.add_child(v, i+1, v.start_with?(":") ? :param : :string) parent_tree = new_tree end end end parent_tree.add_route(route) end trees end |