hm 0.0.3 → 0.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 85ac2c1b29c041f8981babe3497828dbd74008c8
4
- data.tar.gz: 96f500ecd4ef021b715600546817df1527520f4d
3
+ metadata.gz: 961322aa7a75865b2a121c00cbbce0466e33b4c7
4
+ data.tar.gz: abbed53e1e18cf39ea449ada51c1faf93f16e940
5
5
  SHA512:
6
- metadata.gz: 3c4ca16a574f0ee40b33e1c7c2798143d1214afb454d3c151045e8777318d4e41f0cfbdbfc1f7ac8eafe5c365e630d78503bed91f6a25c6afb614f334858b203
7
- data.tar.gz: 9123c53633102d932b602635afe18bf42e739ee4ab432a823ee909fd4126b6189497085077280673bfc97487e9880087a9561e1a54017c2e9ba286078dd04355
6
+ metadata.gz: f88373127529d12668a8997e74673f897d082c67cce381d522c7c2dd694a85559d66e9eaef2230821b3611fd3c496f4df8bb53d0328f41b3b10ce4c5b217e3e7
7
+ data.tar.gz: 315cf2616d6028b3694b7f477dfc10a25f624ea3063bce6cd10eca08e2bf61aff206fc86e875e35e4fdd2eb1f065abb3e14fd65df03ae8b429b391ace3d4bd2a
@@ -48,6 +48,7 @@ class Hm
48
48
 
49
49
  def nest_hashes(value, *keys)
50
50
  return value if keys.empty?
51
+
51
52
  key = keys.shift
52
53
  val = keys.empty? ? value : nest_hashes(value, *keys)
53
54
  key.is_a?(Integer) ? [].tap { |arr| arr[key] = val } : {key => val}
@@ -81,7 +82,11 @@ class Hm
81
82
  end
82
83
 
83
84
  def visit_regular(what, key, rest, path, found:, not_found:) # rubocop:disable Metrics/ParameterLists
84
- internal = Dig.dig(what, key) or return not_found.(what, [*path, key], rest)
85
+ internal = Dig.dig(what, key)
86
+
87
+ # NB: NotFound is signified by special value, because `nil` can still be legitimate value in hash
88
+ return not_found.(what, [*path, key], rest) if internal == Dig::NotFound
89
+
85
90
  rest.empty? and return found.(what, [*path, key], internal)
86
91
  visit(internal, rest, [*path, key], not_found: not_found, &found)
87
92
  end
@@ -3,13 +3,29 @@ class Hm
3
3
  module Dig
4
4
  # TODO: Struct/OpenStruct are also diggable in Ruby core, can be added for future implementation
5
5
  DIGGABLE_CLASSES = [Hash, Array].freeze
6
+ NotFound = Object.new.freeze
6
7
 
7
- def self.dig(what, *keys)
8
- return what.dig(*keys) if what.respond_to?(:dig)
8
+ def self.dig(what, key, *keys)
9
+ # We want to return special value when key is not present, because
10
+ # 1. "at some point in path, we have a value, and this value is `nil`", and
11
+ # 2. "at some point in path, we don't have a value (key is absent)"
12
+ # ...should be different in Algo.visit
13
+ return NotFound unless key?(what, key)
9
14
 
10
- diggable?(what) or fail TypeError, "#{value.class} is not diggable"
11
- value = what[keys.shift]
12
- value.nil? || keys.empty? ? value : dig(value, *keys)
15
+ return what.dig(key, *keys) if what.respond_to?(:dig)
16
+
17
+ ensure_diggable?(what) or fail TypeError, "#{value.class} is not diggable"
18
+ value = what[key]
19
+ keys.empty? ? value : dig(value, *keys)
20
+ end
21
+
22
+ def self.key?(what, key)
23
+ case what
24
+ when Array
25
+ (0...what.size).cover?(key)
26
+ when Hash
27
+ what.key?(key)
28
+ end
13
29
  end
14
30
 
15
31
  def self.diggable?(what)
@@ -1,7 +1,7 @@
1
1
  class Hm
2
2
  MAJOR = 0
3
3
  MINOR = 0
4
- PATCH = 3
4
+ PATCH = 4
5
5
  PRE = nil
6
6
  VERSION = [MAJOR, MINOR, PATCH, PRE].compact.join('.')
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Shepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-08 00:00:00.000000000 Z
11
+ date: 2019-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard