hm 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/lib/hm.rb +36 -7
  4. data/lib/hm/version.rb +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 546509d780fa20ac838d7cde7df190a5860df261
4
- data.tar.gz: cd18bb35c606420e79056efebd703e5bd9c3962d
3
+ metadata.gz: 85ac2c1b29c041f8981babe3497828dbd74008c8
4
+ data.tar.gz: 96f500ecd4ef021b715600546817df1527520f4d
5
5
  SHA512:
6
- metadata.gz: 16c5155264b15632a58896c8e8acd5e26ed10b579bc813e66b4256ae03bfd7ee61a1fd2d702cef0aeda82a9011e193618c4ab00f98c270ff293564d81da82523
7
- data.tar.gz: fa1a5b8e97a2a27bb9ab97f156f33d43746301d8b236488c8ea669d7b19c7cffebc80a132cd3a8fb636d159d1940240f2cbaa371cead7b558eb45956f7dd5e95
6
+ metadata.gz: 3c4ca16a574f0ee40b33e1c7c2798143d1214afb454d3c151045e8777318d4e41f0cfbdbfc1f7ac8eafe5c365e630d78503bed91f6a25c6afb614f334858b203
7
+ data.tar.gz: 9123c53633102d932b602635afe18bf42e739ee4ab432a823ee909fd4126b6189497085077280673bfc97487e9880087a9561e1a54017c2e9ba286078dd04355
@@ -1,5 +1,10 @@
1
1
  # Hm version history
2
2
 
3
+ ## 0.0.3 - Mar 8, 2018
4
+
5
+ * `Hm#partition(list, of, keys)` to receive two hashes: with matching, and not matching keys;
6
+ * `Hm#transform_values` now work without any keys/pathes passed (just transforming all "terminal" values).
7
+
3
8
  ## 0.0.2 - Feb 24, 2018
4
9
 
5
10
  * Some profiling and small optimizations;
data/lib/hm.rb CHANGED
@@ -238,25 +238,30 @@ class Hm
238
238
  # Performs specified transformations on values of input sequence, limited only by specified
239
239
  # pathes.
240
240
  #
241
- # @note
242
- # Unlike {#transform_keys}, this method does nothing when no pathes are passed (e.g. not runs
243
- # transformation on each value), because the semantic would be unclear. In our `:order` example,
244
- # list of all items is a value _too_, at `:items` key, so should it be also transformed?
241
+ # If no `pathes` are passed, all "terminal" values (e.g. not diggable) are yielded and transformed.
245
242
  #
246
243
  # @example
247
244
  # order = {items: [{title: "Beef", price: 18.0}, {title: "Potato", price: 8.2}], total: 26.2}
248
245
  # Hm(order).transform_values(%i[items * price], :total, &:to_s).to_h
249
246
  # # => {:items=>[{:title=>"Beef", :price=>"18.0"}, {:title=>"Potato", :price=>"8.2"}], :total=>"26.2"}
247
+ # Hm(order).transform_values(&:to_s).to_h
248
+ # # => {:items=>[{:title=>"Beef", :price=>"18.0"}, {:title=>"Potato", :price=>"8.2"}], :total=>"26.2"}
250
249
  #
251
- # @see #transform_values
250
+ # @see #transform_keys
252
251
  # @see #transform
253
252
  # @param pathes [Array] List of pathes (each being singular key, or array of keys, including
254
253
  # `:*` wildcard) to look at.
255
254
  # @yieldparam value [Array] Current value to process.
256
255
  # @return [self]
257
256
  def transform_values(*pathes)
258
- pathes.each do |path|
259
- Algo.visit(@hash, path) { |at, pth, val| at[pth.last] = yield(val) }
257
+ if pathes.empty?
258
+ Algo.visit_all(@hash) do |at, pth, val|
259
+ at[pth.last] = yield(val) unless Dig.diggable?(val)
260
+ end
261
+ else
262
+ pathes.each do |path|
263
+ Algo.visit(@hash, path) { |at, pth, val| at[pth.last] = yield(val) }
264
+ end
260
265
  end
261
266
  self
262
267
  end
@@ -434,6 +439,30 @@ class Hm
434
439
  self
435
440
  end
436
441
 
442
+ # Split hash into two: the one with the substructure matching `pathes`, and the with thos that do
443
+ # not.
444
+ #
445
+ # @example
446
+ # order = {items: [{title: "Beef", price: 18.0}, {title: "Potato", price: 8.2}], total: 26.2}
447
+ # Hm(order).partition(%i[items * price], :total)
448
+ # # => [
449
+ # # {:items=>[{:price=>18.0}, {:price=>8.2}], :total=>26.2},
450
+ # # {:items=>[{:title=>"Beef"}, {:title=>"Potato"}]}
451
+ # # ]
452
+ #
453
+ # @param pathes [Array] List of pathes (each being singular key, or array of keys, including
454
+ # `:*` wildcard) to look at.
455
+ # @yieldparam value [Array] Current value to process.
456
+ # @return [Array<Hash>] Two hashes
457
+ def partition(*pathes)
458
+ # FIXME: this implementation is naive, it performs 2 additional deep copies and 2 full cycles of
459
+ # visiting instead of just splitting existing data in one pass. It works, though
460
+ [
461
+ Hm(@hash).slice(*pathes).to_h,
462
+ Hm(@hash).except(*pathes).to_h
463
+ ]
464
+ end
465
+
437
466
  # Returns the result of all the processings inside the `Hm` object.
438
467
  #
439
468
  # Note, that you can pass an Array as a top-level structure to `Hm`, and in this case `to_h` will
@@ -1,7 +1,7 @@
1
1
  class Hm
2
2
  MAJOR = 0
3
3
  MINOR = 0
4
- PATCH = 2
4
+ PATCH = 3
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.2
4
+ version: 0.0.3
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-24 00:00:00.000000000 Z
11
+ date: 2018-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard