key_tree 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +5 -0
- data/RELEASE_NOTES.md +35 -0
- data/lib/key_tree/forest.rb +6 -2
- data/lib/key_tree/path.rb +1 -1
- data/lib/key_tree/tree.rb +49 -2
- data/lib/key_tree.rb +4 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62d952100ee8c4be7d81a400178f58455618e239253b9cc1b40ab5667c95ab6b
|
4
|
+
data.tar.gz: 8c761dc94c958fd3be1b50b6536576cf4b9a808979c9bf01c877b1e23221bab3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 92e7da5bd0166fadaabb78713728975147dde76c81237e2d2e0b56ecd0a3c05744beacce87dad796b17b57ec55ccd83e1d53e1a4179a10e612e7917736301ce7
|
7
|
+
data.tar.gz: 740929794520b7085bc6b57bc75e559e165e4fc1ea1235db1da0743275bad34e336952a9d776bee7df7679bf4f3c39c517e175a070a395f53bea9ea9f4b90ac7
|
data/.rubocop.yml
CHANGED
data/RELEASE_NOTES.md
CHANGED
@@ -1,5 +1,40 @@
|
|
1
1
|
# Release Notes
|
2
2
|
|
3
|
+
## v0.5.1 – 2018-05-19
|
4
|
+
|
5
|
+
### New methods
|
6
|
+
|
7
|
+
* `KeyTree::Tree#default_key?(key)`
|
8
|
+
* `KeyTree::Tree#format(fmtstr)`
|
9
|
+
* `KeyTree::Tree#to_h(string_keys: false)`
|
10
|
+
* `KeyTree::Tree#to_json`
|
11
|
+
* `KeyTree::Tree#to_yaml`
|
12
|
+
|
13
|
+
### Bug fixes
|
14
|
+
|
15
|
+
#### Make forests aware of default values in trees
|
16
|
+
|
17
|
+
Ensure that forests pick up default values from a tree.
|
18
|
+
|
19
|
+
* ebd1cb06 Return trees and forests untouched
|
20
|
+
* 3451a430 Propagate default_proc in fetch
|
21
|
+
* e121a4c4 Consider trees to have a key if #default_key?
|
22
|
+
* 50bc56ec Detect if a default_proc yields a key value
|
23
|
+
|
24
|
+
### New features
|
25
|
+
|
26
|
+
#### Key tree content exporters
|
27
|
+
|
28
|
+
Support for exporting the contents of a key tree to Hash, JSON, and YAML.
|
29
|
+
Also includes a convenience string formatter, that fills format strings
|
30
|
+
with values from a `Tree`.
|
31
|
+
|
32
|
+
* 9b5f05f0 Make exported hash key format selectable
|
33
|
+
* e3434d7e Add custom format method
|
34
|
+
* fa6a9b16 Serialize the contents of a tree to json or yaml
|
35
|
+
* 3fc6466b Convert a tree back into nested hashes
|
36
|
+
* e5aecd8b Split symbols into key paths
|
37
|
+
|
3
38
|
## v0.5.0 – 2018-04-17
|
4
39
|
|
5
40
|
### Changed methods
|
data/lib/key_tree/forest.rb
CHANGED
@@ -37,12 +37,16 @@ module KeyTree
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def tree_with_key(key)
|
40
|
-
result = trees.detect
|
40
|
+
result = trees.detect do |tree|
|
41
|
+
tree.prefix?(key) || tree.default_key?(key)
|
42
|
+
end
|
41
43
|
result || raise(KeyError, "key not found: #{key}")
|
42
44
|
end
|
43
45
|
|
44
46
|
def trees_with_key(key)
|
45
|
-
result = trees.select
|
47
|
+
result = trees.select do |tree|
|
48
|
+
tree.prefix?(key) || tree.default_key?(key)
|
49
|
+
end
|
46
50
|
raise(KeyError, "key not found: #{key}") if result.empty?
|
47
51
|
result
|
48
52
|
end
|
data/lib/key_tree/path.rb
CHANGED
data/lib/key_tree/tree.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'key_tree/path'
|
2
2
|
require 'key_tree/meta_data'
|
3
|
+
|
3
4
|
module KeyTree
|
4
5
|
# A tree of key-value lookup tables (hashes)
|
5
6
|
class Tree < Hash
|
@@ -21,8 +22,9 @@ module KeyTree
|
|
21
22
|
super(Path[key_or_path])
|
22
23
|
end
|
23
24
|
|
24
|
-
def fetch(key_or_path, *args, &
|
25
|
-
|
25
|
+
def fetch(key_or_path, *args, &missing_key)
|
26
|
+
missing_key ||= default_proc
|
27
|
+
super(Path[key_or_path], *args, &missing_key)
|
26
28
|
end
|
27
29
|
|
28
30
|
def values_at(*keys)
|
@@ -46,6 +48,14 @@ module KeyTree
|
|
46
48
|
super(Path[key_or_path])
|
47
49
|
end
|
48
50
|
|
51
|
+
def default_key?(key_or_path)
|
52
|
+
return unless default_proc
|
53
|
+
default_proc.yield(self, Path[key_or_path])
|
54
|
+
true
|
55
|
+
rescue KeyError
|
56
|
+
false
|
57
|
+
end
|
58
|
+
|
49
59
|
def prefix?(key_or_path)
|
50
60
|
keys.any? { |key| key.prefix?(Path[key_or_path]) }
|
51
61
|
end
|
@@ -68,5 +78,42 @@ module KeyTree
|
|
68
78
|
dup.merge!(other, &merger)
|
69
79
|
end
|
70
80
|
alias + merge
|
81
|
+
|
82
|
+
# Format +fmtstr+ with values from the Tree
|
83
|
+
def format(fmtstr)
|
84
|
+
Kernel.format(fmtstr, Hash.new { |_, key| fetch(key) })
|
85
|
+
end
|
86
|
+
|
87
|
+
# Convert a Tree back to nested hashes.
|
88
|
+
#
|
89
|
+
# to_h => Hash, with symbol keys
|
90
|
+
# to_h(string_keys: true) => Hash, with string keys
|
91
|
+
def to_h(**kwargs)
|
92
|
+
to_hash_tree(**kwargs)
|
93
|
+
end
|
94
|
+
|
95
|
+
# Convert a Tree to JSON, with string keys
|
96
|
+
def to_json
|
97
|
+
to_hash_tree(string_keys: true).to_json
|
98
|
+
end
|
99
|
+
|
100
|
+
# Convert a Tree to YAML, with string keys
|
101
|
+
def to_yaml
|
102
|
+
to_hash_tree(string_keys: true).to_yaml
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
def to_hash_tree(key_pairs = self, string_keys: false)
|
108
|
+
hash = key_pairs.group_by do |path, _|
|
109
|
+
string_keys ? path.first.to_s : path.first
|
110
|
+
end
|
111
|
+
hash.transform_values do |next_level|
|
112
|
+
next_level.map! { |path, value| [path[1..-1], value] }
|
113
|
+
first_key, first_value = next_level.first
|
114
|
+
next first_value if first_key.nil? || first_key.empty?
|
115
|
+
to_hash_tree(next_level)
|
116
|
+
end
|
117
|
+
end
|
71
118
|
end
|
72
119
|
end
|
data/lib/key_tree.rb
CHANGED
@@ -15,10 +15,12 @@ require 'key_tree/loader'
|
|
15
15
|
module KeyTree
|
16
16
|
def self.[](contents = {})
|
17
17
|
case contents
|
18
|
+
when Tree, Forest
|
19
|
+
contents
|
18
20
|
when Hash
|
19
|
-
|
21
|
+
Tree[contents]
|
20
22
|
when Array
|
21
|
-
|
23
|
+
Forest[*contents]
|
22
24
|
else
|
23
25
|
raise ArgumentError, "can't load #{contents.class} into a KeyTree"
|
24
26
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: key_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Calle Englund
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: git-version-bump
|