key_tree 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/RELEASE_NOTES.md +25 -0
- data/key_tree.gemspec +2 -7
- data/lib/key_tree/forest.rb +17 -5
- data/lib/key_tree/refine/deep_hash.rb +25 -7
- data/lib/key_tree/tree.rb +11 -6
- data/lib/key_tree/version.rb +10 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69bc0aa979e024edf0ce7e8bf7fd091aa6976a99b436031be3d4d1e7eae94108
|
4
|
+
data.tar.gz: 14da39af72e5680703b911ddf7fa9b27bc8a614bbb95847ed79d01771d827307
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 606f5b5907e53c97570439a2eaa2d5b1c4085300bb1b4855a31ee72fe3ffdc39a69d88f74a439dcd285e51e101bd159f1baacd641536b3c1ceed3ed3f0557232
|
7
|
+
data.tar.gz: 9e9a5f2b2ce987786be86670e003f0cc9d7fa44d33e90acf6db8d835d38aa2bf635027f909245bae092d22502ce67e150d03907c5ad9281689532dcfdf5fcaca
|
data/RELEASE_NOTES.md
CHANGED
@@ -1,5 +1,30 @@
|
|
1
1
|
# Release Notes
|
2
2
|
|
3
|
+
## v0.6.1 – 2018-05-31
|
4
|
+
|
5
|
+
### New methods
|
6
|
+
|
7
|
+
* `KeyTree::Tree#fetch_default(key, default) { block }`
|
8
|
+
|
9
|
+
* Using `KeyTree::Refine::DeepHash`
|
10
|
+
* `Hash#deep_key_pathify`
|
11
|
+
|
12
|
+
### Bug fixes
|
13
|
+
|
14
|
+
Default processing for forests was broken.
|
15
|
+
|
16
|
+
* 7ff646ff Fix Forest#fetch default vs KeyError processing
|
17
|
+
* 62f6fd73 Add default aware fetch method
|
18
|
+
* 00f0b163 Manage deep_fetch KeyError generation on our own
|
19
|
+
|
20
|
+
#### Handle incoming key pathish keys
|
21
|
+
|
22
|
+
When a `Hash` had keys that looked like key paths, they were not correctly
|
23
|
+
converted to nested hashes with symbol keys.
|
24
|
+
|
25
|
+
* c23df1dd Change key transform for incoming hashes
|
26
|
+
* 09b526c8 Add new hash refinement for key_path-ification
|
27
|
+
|
3
28
|
## v0.6.0 – 2018-05-30
|
4
29
|
|
5
30
|
### Major changes
|
data/key_tree.gemspec
CHANGED
@@ -3,16 +3,11 @@
|
|
3
3
|
lib = File.expand_path('lib', __dir__)
|
4
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
5
|
|
6
|
-
|
7
|
-
require 'git-version-bump'
|
8
|
-
GIT_VERSION = GVB.version.freeze
|
9
|
-
rescue LoadError
|
10
|
-
GIT_VERSION = '0.0.0.UNDEF'
|
11
|
-
end
|
6
|
+
require 'key_tree/version'
|
12
7
|
|
13
8
|
Gem::Specification.new do |spec|
|
14
9
|
spec.name = 'key_tree'
|
15
|
-
spec.version =
|
10
|
+
spec.version = KeyTree::VERSION
|
16
11
|
spec.authors = ['Calle Englund']
|
17
12
|
spec.email = ['calle@discord.bofh.se']
|
18
13
|
|
data/lib/key_tree/forest.rb
CHANGED
@@ -41,11 +41,23 @@ module KeyTree # rubocop:disable Style/Documentation
|
|
41
41
|
nil
|
42
42
|
end
|
43
43
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
44
|
+
# Fetch a value from a forest
|
45
|
+
#
|
46
|
+
# :call-seq:
|
47
|
+
# fetch(key) => value
|
48
|
+
# fetch(key, default) => value
|
49
|
+
# fetch(key) { |key| } => value
|
50
|
+
#
|
51
|
+
# The first form raises a +KeyError+ unless +key+ has a value.
|
52
|
+
def fetch(key, *default)
|
53
|
+
trees.lazy.each do |tree|
|
54
|
+
catch do |ball|
|
55
|
+
return tree.fetch(key) { throw ball }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
return yield(key) if block_given?
|
59
|
+
return default.first unless default.empty?
|
60
|
+
raise KeyError, %(key not found: "#{key}")
|
49
61
|
end
|
50
62
|
|
51
63
|
def key?(key)
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative '../path'
|
4
|
+
|
3
5
|
module KeyTree
|
4
6
|
module Refine
|
5
7
|
# Refinements to Hash for deep_ methods, for traversing nested structures
|
@@ -22,14 +24,17 @@ module KeyTree
|
|
22
24
|
# deep_fetch(key_path) => value
|
23
25
|
# deep_fetch(key_path, default) => value || default
|
24
26
|
# deep_fetch(key_path) { |key_path| block } => value || block
|
25
|
-
def deep_fetch(key_path, *
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
def deep_fetch(key_path, *default)
|
28
|
+
catch do |ball|
|
29
|
+
result = key_path.reduce(self) do |hash, key|
|
30
|
+
throw ball unless hash.is_a?(Hash)
|
31
|
+
hash.fetch(key) { throw ball }
|
32
|
+
end
|
33
|
+
return result unless result.is_a?(Hash)
|
30
34
|
end
|
31
|
-
return
|
32
|
-
|
35
|
+
return yield(key_path) if block_given?
|
36
|
+
return default.first unless default.empty?
|
37
|
+
raise KeyError, %(key path invalid: "#{key_path}")
|
33
38
|
end
|
34
39
|
|
35
40
|
# Store a new value in a nested hash structure, expanding it
|
@@ -115,6 +120,19 @@ module KeyTree
|
|
115
120
|
end
|
116
121
|
end
|
117
122
|
|
123
|
+
# Comvert any keys containing a +.+ in a hash structure
|
124
|
+
# to nested hashes.
|
125
|
+
#
|
126
|
+
# :call-seq:
|
127
|
+
# deep_key_pathify => Hash
|
128
|
+
def deep_key_pathify
|
129
|
+
each_with_object({}) do |(key, value), result|
|
130
|
+
key_path = Path[key]
|
131
|
+
value = value.deep_key_pathify if value.is_a?(Hash)
|
132
|
+
result.deep_store(key_path, value)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
118
136
|
def deep_enumerator(yielder, prefix = [])
|
119
137
|
each do |key, value|
|
120
138
|
key_path = prefix + [key]
|
data/lib/key_tree/tree.rb
CHANGED
@@ -24,7 +24,7 @@ module KeyTree # rubocop:disable Style/Documentation
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def initialize(hash = {}, default = nil, &default_proc)
|
27
|
-
@hash = hash.to_h.
|
27
|
+
@hash = hash.to_h.deep_key_pathify
|
28
28
|
@default = default
|
29
29
|
@default_proc = default_proc
|
30
30
|
end
|
@@ -45,12 +45,17 @@ module KeyTree # rubocop:disable Style/Documentation
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def [](key_path)
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
fetch_default(key_path, default)
|
49
|
+
end
|
50
|
+
|
51
|
+
def fetch_default(key_path, *default)
|
52
|
+
catch do |ball|
|
53
|
+
return fetch(key_path) { throw ball }
|
51
54
|
end
|
52
|
-
|
53
|
-
|
55
|
+
return default_proc.call(self, key_path) unless default_proc.nil?
|
56
|
+
return yield(key_path) if block_given?
|
57
|
+
return default.first unless default.empty?
|
58
|
+
raise KeyError, %(key not found: "#{key_path}")
|
54
59
|
end
|
55
60
|
|
56
61
|
def fetch(key_path, *args, &key_missing)
|
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.6.
|
4
|
+
version: 0.6.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-05-
|
11
|
+
date: 2018-05-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -123,6 +123,7 @@ files:
|
|
123
123
|
- lib/key_tree/refine/deep_hash.rb
|
124
124
|
- lib/key_tree/refinements.rb
|
125
125
|
- lib/key_tree/tree.rb
|
126
|
+
- lib/key_tree/version.rb
|
126
127
|
homepage: https://github.com/notcalle/ruby-keytree
|
127
128
|
licenses:
|
128
129
|
- MIT
|