Class: ReeRoda::BuildRoutingTree

Inherits:
Object
  • Object
show all
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



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
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ree_lib/packages/ree_roda/package/ree_roda/services/build_routing_tree.rb', line 129

def call(routes)
  tree = nil

  routes.each do |route|
    splitted = route.path.split("/")
    parent_tree = tree

    splitted.each_with_index do |v, j|
      if tree.nil?
        tree = RoutingTree.new([v], j, :string)
        parent_tree = tree
        next
      end

      current = parent_tree.find_by_value(value: v, depth: j)

      if current
        parent_tree = current
        current.add_route(route) if j == (splitted.length - 1)
      else
        if !parent_tree.any_child_has_value?(v)
          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)
            param_child.add_route(route) if j == (splitted.length - 1)
            parent_tree = param_child

            next
          end

          new_tree = parent_tree.add_child(v, j, v.start_with?(":") ? :param : :string)
          parent_tree = new_tree
        end

        parent_tree.add_route(route) if j == (splitted.length - 1)
      end
    end
  end

  tree
end