key_tree 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 535d58bfc5880880fe498d115be5d62a7cb4670661c17d46f4a44087f02c36a2
4
- data.tar.gz: 42896ddb6f71accd5a23504ae63e38abf242bf6f839df4b599d7e567c48fcb4f
3
+ metadata.gz: 69bc0aa979e024edf0ce7e8bf7fd091aa6976a99b436031be3d4d1e7eae94108
4
+ data.tar.gz: 14da39af72e5680703b911ddf7fa9b27bc8a614bbb95847ed79d01771d827307
5
5
  SHA512:
6
- metadata.gz: a0e0c52ca0cc4766b226d8f31efcf1e97aa154a8a67f132d5f4f2c0764bd2c45ff05e8a9686f25ed3bfcc87caa144960ffd9dfdcf083084e71d490d9418d2444
7
- data.tar.gz: 8cff399bdf835fb25fb45d0c50cf562e7f080ef9d5a88b7fd53c2a9c029b747fa1065bbf83455ffe1ef7a91eba5a91ef28b0ee2688eb06c942a7a4110a0c6eb7
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
- begin
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 = GIT_VERSION
10
+ spec.version = KeyTree::VERSION
16
11
  spec.authors = ['Calle Englund']
17
12
  spec.email = ['calle@discord.bofh.se']
18
13
 
@@ -41,11 +41,23 @@ module KeyTree # rubocop:disable Style/Documentation
41
41
  nil
42
42
  end
43
43
 
44
- def fetch(key)
45
- return tree_with_key(key).fetch(key) unless block_given?
46
-
47
- values = trees_with_key(key).map { |tree| tree.fetch(key) }
48
- values.reverse.reduce { |left, right| yield(key, left, right) }
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, *args, &key_missing)
26
- key_error = [KeyError, %(key path invalid: "#{key_path}")]
27
- result = key_path.reduce(self) do |hash, key|
28
- raise(*key_error) unless hash.is_a?(Hash)
29
- hash.fetch(key, *args, &key_missing)
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 result unless result.is_a?(Hash)
32
- raise(*key_error)
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.deep_transform_keys(&:to_sym)
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
- fetch(key_path) do
49
- next default_proc.call(self, key_path) unless default_proc.nil?
50
- default
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
- rescue KeyError
53
- default
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)
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KeyTree # rubocop:disable Style/Documentation
4
+ begin
5
+ require 'git-version-bump'
6
+ VERSION = GVB.version.freeze
7
+ rescue LoadError
8
+ VERSION = '0.0.0.UNDEF'
9
+ end
10
+ 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.6.0
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-30 00:00:00.000000000 Z
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