class TwitterCldr::Utils::FileSystemTrie

Constants

VALUE_FILE

Attributes

path_root[R]

Public Class Methods

new(path_root, root = Node.new) click to toggle source
# File lib/twitter_cldr/utils/file_system_trie.rb, line 17
def initialize(path_root, root = Node.new)
  @path_root = path_root
  @root = root
end

Public Instance Methods

add(key, value) click to toggle source
# File lib/twitter_cldr/utils/file_system_trie.rb, line 26
def add(key, value)
  store(key, value, false)
end
empty?() click to toggle source
# File lib/twitter_cldr/utils/file_system_trie.rb, line 22
def empty?
  !@root.has_children?
end
get(key) click to toggle source
# File lib/twitter_cldr/utils/file_system_trie.rb, line 34
def get(key)
  node = get_node(key)
  node && node.value
end
get_node(key) click to toggle source
# File lib/twitter_cldr/utils/file_system_trie.rb, line 39
def get_node(key)
  traverse(key) do |node, key_element|
    return unless node
    node.child(key_element)
  end
end
set(key, value) click to toggle source
# File lib/twitter_cldr/utils/file_system_trie.rb, line 30
def set(key, value)
  store(key, value)
end

Private Instance Methods

fill_in_path(current_path, key_element, parent) click to toggle source
# File lib/twitter_cldr/utils/file_system_trie.rb, line 85
def fill_in_path(current_path, key_element, parent)
  if File.exist?(current_path)
    unless parent.child(key_element)
      parent.set_child(key_element, Node.new)
    end
  end
end
fill_in_value(current_path, key_element, parent) click to toggle source
# File lib/twitter_cldr/utils/file_system_trie.rb, line 93
def fill_in_value(current_path, key_element, parent)
  value_file = File.join(current_path, VALUE_FILE)
  child = parent.child(key_element)

  if File.exist?(value_file) && child && !child.value
    parent.child(key_element).value = ::Marshal.load(
      File.read(value_file)
    )
  end
end
mkdir(path) click to toggle source
# File lib/twitter_cldr/utils/file_system_trie.rb, line 104
def mkdir(path)
  FileUtils.mkdir_p(path) unless File.exist?(path)
end
store(key, value, override = true) click to toggle source
# File lib/twitter_cldr/utils/file_system_trie.rb, line 51
def store(key, value, override = true)
  final = store_p(key)

  if final.value.nil? || override
    final.value = value

    path = File.join(path_root, *key, VALUE_FILE)
    File.write(path, Marshal.dump(value))
  end
end
store_p(key) click to toggle source
# File lib/twitter_cldr/utils/file_system_trie.rb, line 62
def store_p(key)
  current_path = path_root

  traverse(key) do |node, key_element|
    current_path = File.join(current_path, key_element)
    mkdir(current_path)
    node.child(key_element) || node.set_child(key_element, Node.new)
  end
end
traverse(key) { |node, key_element| ... } click to toggle source
# File lib/twitter_cldr/utils/file_system_trie.rb, line 72
def traverse(key)
  current_path = path_root

  key.inject(@root) do |node, key_element|
    next unless node
    next unless key_element
    current_path = File.join(current_path, key_element)
    fill_in_path(current_path, key_element, node)
    fill_in_value(current_path, key_element, node)
    yield node, key_element if block_given?
  end
end