class Lenjador::Preprocessors::JSONPointerTrie

Constants

DEFAULT_CACHE_SIZE
SEPARATOR
WILDCARD

Public Class Methods

new(cache_size: DEFAULT_CACHE_SIZE, **) click to toggle source
# File lib/lenjador/preprocessors/json_pointer_trie.rb, line 12
def initialize(cache_size: DEFAULT_CACHE_SIZE, **)
  @root_node = {}
  @cache = LruRedux::Cache.new(cache_size)
end

Public Instance Methods

include?(path) click to toggle source
# File lib/lenjador/preprocessors/json_pointer_trie.rb, line 25
def include?(path)
  @cache.getset(path) { traverse_path(path) }
end
insert(pointer) click to toggle source
# File lib/lenjador/preprocessors/json_pointer_trie.rb, line 17
def insert(pointer)
  split_path(pointer).reduce(@root_node) do |tree, key|
    tree[key] ||= {}
  end

  self
end

Private Instance Methods

split_path(path) click to toggle source
# File lib/lenjador/preprocessors/json_pointer_trie.rb, line 37
def split_path(path)
  path.split(SEPARATOR).reject(&:empty?)
end
traverse_path(path) click to toggle source
# File lib/lenjador/preprocessors/json_pointer_trie.rb, line 31
def traverse_path(path)
  split_path(path).reduce(@root_node) do |node, key|
    node[key] || node[WILDCARD] || (break false)
  end
end