Sha256: 131b8be629296d73982a8efd5737bc5b80439210af20063ae60d86f823983ca1

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

require "hanami/router/leaf"

module Hanami
  class Router
    # Trie node
    #
    # @api private
    # @since 2.0.0
    class Node
      # @api private
      # @since 2.0.0
      attr_reader :to

      # @api private
      # @since 2.0.0
      def initialize
        @variable = nil
        @fixed = nil
        @leaves = nil
      end

      # @api private
      # @since 2.0.0
      def put(segment)
        if variable?(segment)
          @variable ||= self.class.new
        else
          @fixed ||= {}
          @fixed[segment] ||= self.class.new
        end
      end

      # @api private
      # @since 2.0.0
      def get(segment)
        @fixed&.fetch(segment, nil) || @variable
      end

      # @api private
      # @since 2.0.0
      def leaf!(route, to, constraints)
        @leaves ||= []
        @leaves << Leaf.new(route, to, constraints)
      end

      # @api private
      # @since 2.2.0
      def match(path)
        @leaves&.find { |leaf| leaf.match(path) }
      end

      private

      # @api private
      # @since 2.0.0
      def variable?(segment)
        Router::ROUTE_VARIABLE_MATCHER.match?(segment)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hanami-router-2.2.0 lib/hanami/router/node.rb