hm 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/hm.rb +36 -7
- data/lib/hm/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 85ac2c1b29c041f8981babe3497828dbd74008c8
|
4
|
+
data.tar.gz: 96f500ecd4ef021b715600546817df1527520f4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c4ca16a574f0ee40b33e1c7c2798143d1214afb454d3c151045e8777318d4e41f0cfbdbfc1f7ac8eafe5c365e630d78503bed91f6a25c6afb614f334858b203
|
7
|
+
data.tar.gz: 9123c53633102d932b602635afe18bf42e739ee4ab432a823ee909fd4126b6189497085077280673bfc97487e9880087a9561e1a54017c2e9ba286078dd04355
|
data/CHANGELOG.md
CHANGED
@@ -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
|
-
#
|
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 #
|
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.
|
259
|
-
Algo.
|
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
|
data/lib/hm/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2018-03-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: yard
|