Class: KeyTree::Tree

Inherits:
Hash
  • Object
show all
Includes:
MetaData
Defined in:
lib/key_tree/tree.rb

Overview

A tree of key-value lookup tables (hashes)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MetaData

#meta_data, #with_meta_data

Class Method Details

.[](hash = {}) ⇒ Object

KeyTree::Tree.new(hash)

Initialize a new KeyTree from nested Hash:es



13
14
15
16
17
18
19
# File 'lib/key_tree/tree.rb', line 13

def self.[](hash = {})
  keytree = Tree.new
  hash.each do |key, value|
    keytree[key] = value
  end
  keytree
end

Instance Method Details

#[](key_or_path) ⇒ Object



21
22
23
# File 'lib/key_tree/tree.rb', line 21

def [](key_or_path)
  super(Path[key_or_path])
end

#[]=(key_or_path, new_value) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/key_tree/tree.rb', line 34

def []=(key_or_path, new_value)
  path = Path[key_or_path]

  delete_if { |key, _| path.conflict?(key) }

  case new_value
  when Hash
    new_value.each { |suffix, value| self[path + suffix] = value }
  else
    super(path, new_value)
  end
end

#conflict?(key_or_path) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/key_tree/tree.rb', line 63

def conflict?(key_or_path)
  keys.any? { |key| key.conflict?(Path[key_or_path]) }
end

#default_key?(key_or_path) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
# File 'lib/key_tree/tree.rb', line 51

def default_key?(key_or_path)
  return unless default_proc
  default_proc.yield(self, Path[key_or_path])
  true
rescue KeyError
  false
end

#fetch(key_or_path, *args, &missing_key) ⇒ Object



25
26
27
28
# File 'lib/key_tree/tree.rb', line 25

def fetch(key_or_path, *args, &missing_key)
  missing_key ||= default_proc
  super(Path[key_or_path], *args, &missing_key)
end

#format(fmtstr) ⇒ Object

Format fmtstr with values from the Tree



83
84
85
# File 'lib/key_tree/tree.rb', line 83

def format(fmtstr)
  Kernel.format(fmtstr, Hash.new { |_, key| fetch(key) })
end

#key?(key_or_path) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/key_tree/tree.rb', line 47

def key?(key_or_path)
  super(Path[key_or_path])
end

#merge(other, &merger) ⇒ Object Also known as: +



77
78
79
# File 'lib/key_tree/tree.rb', line 77

def merge(other, &merger)
  dup.merge!(other, &merger)
end

#merge!(other, &merger) ⇒ Object Also known as: <<

The merging of trees needs some extra consideration; due to the nature of key paths, prefix conflicts must be deleted



70
71
72
73
74
# File 'lib/key_tree/tree.rb', line 70

def merge!(other, &merger)
  other = Tree[other] unless other.is_a?(Tree)
  delete_if { |key, _| other.conflict?(key) }
  super
end

#prefix?(key_or_path) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/key_tree/tree.rb', line 59

def prefix?(key_or_path)
  keys.any? { |key| key.prefix?(Path[key_or_path]) }
end

#to_h(**kwargs) ⇒ Object

Convert a Tree back to nested hashes.

to_h => Hash, with symbol keys to_h(string_keys: true) => Hash, with string keys



91
92
93
# File 'lib/key_tree/tree.rb', line 91

def to_h(**kwargs)
  to_hash_tree(**kwargs)
end

#to_jsonObject

Convert a Tree to JSON, with string keys



96
97
98
# File 'lib/key_tree/tree.rb', line 96

def to_json
  to_hash_tree(string_keys: true).to_json
end

#to_yamlObject

Convert a Tree to YAML, with string keys



101
102
103
# File 'lib/key_tree/tree.rb', line 101

def to_yaml
  to_hash_tree(string_keys: true).to_yaml
end

#values_at(*keys) ⇒ Object



30
31
32
# File 'lib/key_tree/tree.rb', line 30

def values_at(*keys)
  super(keys.map { |key_or_path| Path[key_or_path] })
end