class KeyTree::Tree
A tree of key-value lookup tables (hashes)
Attributes
default[R]
default_proc[R]
Public Class Methods
[](hash = {})
click to toggle source
KeyTree::Tree.new
(hash
)
Initialize a new KeyTree
from nested Hash:es
# File lib/key_tree/tree.rb, line 22 def self.[](hash = {}) new(hash) end
new(hash = {}, default = nil, &default_proc)
click to toggle source
# File lib/key_tree/tree.rb, line 26 def initialize(hash = {}, default = nil, &default_proc) @hash = hash.to_h.deep_key_pathify @default = default @default_proc = default_proc end
Public Instance Methods
Alias for: merge
Alias for: merge!
[](key_path)
click to toggle source
# File lib/key_tree/tree.rb, line 47 def [](key_path) fetch(key_path) do next default if default_proc.nil? default_proc.call(self, key_path) end end
delete(key_path)
click to toggle source
# File lib/key_tree/tree.rb, line 71 def delete(key_path) @hash.deep_delete(key_path.to_key_path) end
delete!(key_path)
click to toggle source
# File lib/key_tree/tree.rb, line 75 def delete!(key_path) delete(key_path) rescue KeyError key_path = key_path[0..-2] retry end
fetch(key_path, *args, &key_missing)
click to toggle source
# File lib/key_tree/tree.rb, line 55 def fetch(key_path, *args, &key_missing) @hash.deep_fetch(key_path.to_key_path, *args, &key_missing) end
include?(key_path)
click to toggle source
# File lib/key_tree/tree.rb, line 97 def include?(key_path) fetch(key_path) true rescue KeyError false end
Alias for: keys
keys → Array of KeyTree::Path
click to toggle source
Return all maximal key paths in a tree
# File lib/key_tree/tree.rb, line 90 def keys @hash.deep.each_with_object([]) do |(key_path, value), result| result << key_path.to_key_path unless value.is_a?(Hash) end end
Also aliased as: key_paths
merge(other) → Tree
click to toggle source
merge(other) { |key, lhs, rhs| } → Tree
Return a new tree by merging values from other
tree
# File lib/key_tree/tree.rb, line 140 def merge(other, &block) @hash.deep_merge(other.to_h, &block).to_key_tree end
Also aliased as: +
merge!(other) → self
click to toggle source
merge!(other) { |key, lhs, rhs| } → self
Merge values from other
tree into self
# File lib/key_tree/tree.rb, line 129 def merge!(other, &block) @hash.deep_merge!(other.to_h, &block) self end
Also aliased as: <<
prefix?(key_path)
click to toggle source
# File lib/key_tree/tree.rb, line 108 def prefix?(key_path) key_path.to_key_path.reduce(@hash) do |subtree, key| return false unless subtree.is_a?(Hash) return false unless subtree.key?(key) subtree[key] end true end
Also aliased as: has_prefix?
store(key_path, new_value)
click to toggle source
# File lib/key_tree/tree.rb, line 59 def store(key_path, new_value) @hash.deep_store(key_path.to_key_path, new_value) end
store!(key_path, new_value)
click to toggle source
# File lib/key_tree/tree.rb, line 63 def store!(key_path, new_value) store(key_path, new_value) rescue KeyError delete!(key_path) retry end
Also aliased as: []=
to_yaml → String
click to toggle source
Convert a Tree
to YAML, with string keys
# File lib/key_tree/tree.rb, line 43 def to_yaml to_h.deep_transform_keys(&:to_s).to_yaml end
value?(needle)
click to toggle source
# File lib/key_tree/tree.rb, line 119 def value?(needle) @hash.deep.lazy.any? { |(_, straw)| straw == needle } end
Also aliased as: has_value?
values_at(*key_paths)
click to toggle source
# File lib/key_tree/tree.rb, line 82 def values_at(*key_paths) key_paths.map { |key_path| self[key_path] } end