Sha256: 8fef322e8836559fcd3141ea30bdef0a5cb6ed848f6ace0bde9ecc4999c4e2bd

Contents?: true

Size: 1.18 KB

Versions: 8

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

require "hanami/application/routing/resolver/node"

module Hanami
  class Application
    module Routing
      class Resolver
        # Endpoint resolver trie to register slices
        #
        # @api private
        # @since 2.0.0
        class Trie
          # @api private
          # @since 2.0.0
          def initialize
            @root = Node.new
          end

          # @api private
          # @since 2.0.0
          def add(path, name)
            node = @root
            for_each_segment(path) do |segment|
              node = node.put(segment)
            end

            node.leaf!(name)
          end

          # @api private
          # @since 2.0.0
          def find(path)
            node = @root

            for_each_segment(path) do |segment|
              break unless node

              node = node.get(segment)
            end

            return node.slice if node&.leaf?

            nil
          end

          private

          # @api private
          # @since 2.0.0
          def for_each_segment(path, &blk)
            _, *segments = path.split(/\//)
            segments.each(&blk)
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
hanami-2.0.0.alpha8 lib/hanami/application/routing/resolver/trie.rb
hanami-2.0.0.alpha7.1 lib/hanami/application/routing/resolver/trie.rb
hanami-2.0.0.alpha7 lib/hanami/application/routing/resolver/trie.rb
hanami-2.0.0.alpha6 lib/hanami/application/routing/resolver/trie.rb
hanami-2.0.0.alpha5 lib/hanami/application/routing/resolver/trie.rb
hanami-2.0.0.alpha4 lib/hanami/application/routing/resolver/trie.rb
hanami-2.0.0.alpha3 lib/hanami/application/routing/resolver/trie.rb
hanami-2.0.0.alpha2 lib/hanami/application/routing/resolver/trie.rb