Sha256: e830ef7a499bb290730e34fcd6c5ea06f2873c7d058077690c25cba7baeb50fa

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require "modern/errors/setup_errors"

module Modern
  module Util
    class TrieNode
      attr_reader :parent
      attr_reader :path
      attr_accessor :value

      attr_reader :children

      def initialize(path = [])
        @path = path
        @children = {}
      end

      def add(key, value, raise_if_present: false)
        key = [key].flatten

        if key.empty?
          if @value
            raise Modern::Errors::RoutingError, "Existing value at #{path.inspect}: #{@value}" \
              if raise_if_present
          end

          @value = value
        else
          child_name = key.first
          @children[child_name] ||= TrieNode.new(path + [child_name])

          @children[child_name].add(key[1..-1], value, raise_if_present: raise_if_present)
        end
      end

      def [](child_name)
        @children[child_name] || @children[:templated]
      end

      def get(key = [])
        key = [key].flatten

        node = self
        until key.empty? || node.nil?
          node = node[key.shift]
        end

        node&.value
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
modern-0.5.0 lib/modern/util/trie_node.rb
modern-0.4.6 lib/modern/util/trie_node.rb
modern-0.4.5 lib/modern/util/trie_node.rb
modern-0.4.4 lib/modern/util/trie_node.rb
modern-0.4.3 lib/modern/util/trie_node.rb
modern-0.4.2 lib/modern/util/trie_node.rb