hm 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: 49f552a6aaf919dbd0d5909c5e88b6e16a949fe4
4
- data.tar.gz: 5d0b3b428620f7542e276ba0431ff9278e256643
3
+ metadata.gz: 546509d780fa20ac838d7cde7df190a5860df261
4
+ data.tar.gz: cd18bb35c606420e79056efebd703e5bd9c3962d
5
5
  SHA512:
6
- metadata.gz: 3f0e5d2db2a5bc0a0e5b39aa05aed137b235d4ec115739e063dc1bdde4eab15842a5220202e5eb4ffb4b42fdc4015cdacb0d97582ac79d4ac92a9f2422b4c881
7
- data.tar.gz: 7fd96ca72e8218a82663176b38549f3688e040f6efc74d923bd815006571aae1c83bacf20ff4bb3a0f3609aac0d1ceedf42421019c3f1dff7708f87807780b94
6
+ metadata.gz: 16c5155264b15632a58896c8e8acd5e26ed10b579bc813e66b4256ae03bfd7ee61a1fd2d702cef0aeda82a9011e193618c4ab00f98c270ff293564d81da82523
7
+ data.tar.gz: fa1a5b8e97a2a27bb9ab97f156f33d43746301d8b236488c8ea669d7b19c7cffebc80a132cd3a8fb636d159d1940240f2cbaa371cead7b558eb45956f7dd5e95
@@ -0,0 +1,11 @@
1
+ # Hm version history
2
+
3
+ ## 0.0.2 - Feb 24, 2018
4
+
5
+ * Some profiling and small optimizations;
6
+ * Support for Ruby versions before 2.3, when native #dig was introduced, thanks to
7
+ [@berniechiu](https://github.com/berniechiu).
8
+
9
+ ## 0.0.1 - Feb 17, 2018
10
+
11
+ Initial
data/README.md CHANGED
@@ -73,6 +73,7 @@ pp Hm.new(weather)
73
73
  invented on the road, but may not work for yours;
74
74
  * Most of the methods work on Arrays and Hashes, but not on `Struct` and `OpenStruct` (which are
75
75
  `dig`-able in Ruby), though, base `#dig` and `#dig!` should work on them too;
76
+ * `Hm(hash).dig(...)` works even on versions of Ruby before 2.3 (when native `#dig` was introduced);
76
77
  * API is subject to polish and change in future.
77
78
 
78
79
  ## Usage
data/lib/hm.rb CHANGED
@@ -475,7 +475,9 @@ class Hm
475
475
  end
476
476
  end
477
477
 
478
+ require_relative 'hm/version'
478
479
  require_relative 'hm/algo'
480
+ require_relative 'hm/dig'
479
481
 
480
482
  # Shortcut for {Hm}.new
481
483
  def Hm(hash) # rubocop:disable Naming/MethodName
@@ -24,31 +24,25 @@ class Hm
24
24
  end
25
25
  end
26
26
 
27
- def to_pairs(collection)
28
- case
29
- when collection.respond_to?(:each_pair)
30
- collection.each_pair.to_a
31
- when collection.respond_to?(:each)
32
- collection.each_with_index.to_a.map(&:reverse)
33
- else
34
- fail TypeError, "Can't dig/* in #{collection.class}"
35
- end
36
- end
37
-
38
- # Enumerates through entire collection with "current key/current values" at each point, even
27
+ # Enumerates through entire collection with "current key/current value" at each point, even
39
28
  # if elements are deleted in a process of enumeration
40
29
  def robust_enumerator(collection)
41
- return to_pairs(collection) if collection.is_a?(Hash)
42
-
43
- # Only Arrays need this kind of trickery
44
- Enumerator.new do |y|
45
- cur = collection.size
46
- until cur.zero?
47
- pairs = to_pairs(collection)
48
- pos = pairs.size - cur
49
- y << pairs[pos]
50
- cur -= 1
30
+ case collection
31
+ when Hash, Struct
32
+ collection.each_pair.to_a
33
+ when Array
34
+ Enumerator.new do |y|
35
+ cur = collection.size
36
+ until cur.zero?
37
+ pos = collection.size - cur
38
+ y << [pos, collection[pos]]
39
+ cur -= 1
40
+ end
51
41
  end
42
+ when ->(c) { c.respond_to?(:each_pair) }
43
+ collection.each_pair.to_a
44
+ else
45
+ fail TypeError, "Can't dig/* in #{collection.class}"
52
46
  end
53
47
  end
54
48
 
@@ -60,7 +54,7 @@ class Hm
60
54
  end
61
55
 
62
56
  def visit(what, rest, path = [], not_found: ->(*) {}, &found)
63
- what.respond_to?(:dig) or fail TypeError, "#{what.class} is not diggable"
57
+ Dig.diggable?(what) or fail TypeError, "#{what.class} is not diggable"
64
58
 
65
59
  key, *rst = rest
66
60
  if key == WILDCARD
@@ -73,7 +67,7 @@ class Hm
73
67
  def visit_all(what, path = [], &block)
74
68
  robust_enumerator(what).each do |key, val|
75
69
  yield(what, [*path, key], val)
76
- visit_all(val, [*path, key], &block) if val.respond_to?(:dig)
70
+ visit_all(val, [*path, key], &block) if Dig.diggable?(val)
77
71
  end
78
72
  end
79
73
 
@@ -87,7 +81,7 @@ class Hm
87
81
  end
88
82
 
89
83
  def visit_regular(what, key, rest, path, found:, not_found:) # rubocop:disable Metrics/ParameterLists
90
- internal = what.dig(key) or return not_found.(what, [*path, key], rest)
84
+ internal = Dig.dig(what, key) or return not_found.(what, [*path, key], rest)
91
85
  rest.empty? and return found.(what, [*path, key], internal)
92
86
  visit(internal, rest, [*path, key], not_found: not_found, &found)
93
87
  end
@@ -0,0 +1,19 @@
1
+ class Hm
2
+ # @private
3
+ module Dig
4
+ # TODO: Struct/OpenStruct are also diggable in Ruby core, can be added for future implementation
5
+ DIGGABLE_CLASSES = [Hash, Array].freeze
6
+
7
+ def self.dig(what, *keys)
8
+ return what.dig(*keys) if what.respond_to?(:dig)
9
+
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)
13
+ end
14
+
15
+ def self.diggable?(what)
16
+ DIGGABLE_CLASSES.include?(what.class)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ class Hm
2
+ MAJOR = 0
3
+ MINOR = 0
4
+ PATCH = 2
5
+ PRE = nil
6
+ VERSION = [MAJOR, MINOR, PATCH, PRE].compact.join('.')
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.1
4
+ version: 0.0.2
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-02-17 00:00:00.000000000 Z
11
+ date: 2018-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard
@@ -178,6 +178,20 @@ dependencies:
178
178
  - - ">="
179
179
  - !ruby/object:Gem::Version
180
180
  version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: ruby-prof
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
181
195
  description: |2
182
196
  Hm is a library for clean, idiomatic and chainable processing of complicated Ruby structures,
183
197
  typically unpacked from JSON. It provides smart dig and bury, keys replacement,
@@ -187,10 +201,13 @@ executables: []
187
201
  extensions: []
188
202
  extra_rdoc_files: []
189
203
  files:
204
+ - CHANGELOG.md
190
205
  - LICENSE.txt
191
206
  - README.md
192
207
  - lib/hm.rb
193
208
  - lib/hm/algo.rb
209
+ - lib/hm/dig.rb
210
+ - lib/hm/version.rb
194
211
  homepage: https://github.com/zverok/hm
195
212
  licenses:
196
213
  - MIT
@@ -203,7 +220,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
203
220
  requirements:
204
221
  - - ">="
205
222
  - !ruby/object:Gem::Version
206
- version: 2.3.0
223
+ version: 2.1.0
207
224
  required_rubygems_version: !ruby/object:Gem::Requirement
208
225
  requirements:
209
226
  - - ">="