creative_rails_utilities 0.4.5 → 0.4.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +21 -0
- data/lib/creative_rails_utilities/hash.rb +34 -0
- data/lib/creative_rails_utilities/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: 0aeba2d7261337d8ead0a8589484c3810e09f77d
|
4
|
+
data.tar.gz: 75c51fa09a75031047752e8dd7d4cf2993a6dd69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bcc93b4c92d8ea744b797a8b873380aebeff10e70635d062cc3f4f8a3feb977944f3cc6af06994368129cec73a45db9a9780758be3b10ee3073135b4589388f5
|
7
|
+
data.tar.gz: 21dc974ec4b821762f23c6827c8b88a7dc1f6995aa854a6b8d89c4e1356ad91ed7e1a9c8de7c85849ace8744bef406bc8dccfe5e250986cfdba5c4239cd94467
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -77,6 +77,27 @@ some_hash.fast_sort_keys #=> some_sorted_hash
|
|
77
77
|
{}.populate_with_keys(min: 1, max: 3) #=> {1 => 0, 2 => 0, 3 => 0}
|
78
78
|
```
|
79
79
|
|
80
|
+
__.sum(h1, h2)__
|
81
|
+
```rb
|
82
|
+
# use this to recursively sum-merge two hashes that have strictly numeric and hash-like values
|
83
|
+
|
84
|
+
# Case 2 Sum arbitrary single depth hash
|
85
|
+
h1 = {1 => 2, 2 => 3}
|
86
|
+
h2 = {1 => 1, 3 => 2}
|
87
|
+
sum = {1 => 3, 2 => 3, 3 => 2}
|
88
|
+
|
89
|
+
# Case 2 Sum arbitrary two depth hash
|
90
|
+
h1 = {1 => {a: 1, b: 2}, 2 => {a: 1, b: 2} }
|
91
|
+
h2 = {1 => {a: 1, b: 1}, 3 => {a: 1, b: 2} }
|
92
|
+
sum = {1 => {a: 2, b: 3}, 2 => {a: 1, b: 2}, 3 => {a: 1, b: 2} }
|
93
|
+
|
94
|
+
# Case 3 Sum arbitrary N depth hash
|
95
|
+
h1 = {1 => 1, 2 => {a: 1, b: 2} }
|
96
|
+
h2 = {2 => {a: 1, b: 1}, 3 => {x: {y: 1}} }
|
97
|
+
sum = {1 => 1, 2 => {a: 2, b: 3}, 3 => {x: {y: 1}} }
|
98
|
+
|
99
|
+
```
|
100
|
+
|
80
101
|
##### Numeric
|
81
102
|
|
82
103
|
```ruby
|
@@ -36,4 +36,38 @@ class Hash
|
|
36
36
|
# return result.fast_sort_keys
|
37
37
|
# end
|
38
38
|
|
39
|
+
def self.sum(h1, h2)
|
40
|
+
# 1. take keys from both hashes
|
41
|
+
keys = (h1.keys + h2.keys).uniq
|
42
|
+
|
43
|
+
sum_hash = {}
|
44
|
+
|
45
|
+
# 2. loop over the keys, summing numerics and recursing into hash-like structures
|
46
|
+
keys.each do |key|
|
47
|
+
v1 = h1[key]
|
48
|
+
v2 = h2[key]
|
49
|
+
|
50
|
+
sum_value = if v1.respond_to?(:keys) && v2.respond_to?(:keys)
|
51
|
+
# O-1 recuring needed only if both are hash-like
|
52
|
+
sum(v1, v2)
|
53
|
+
elsif v1.present? && v2.blank?
|
54
|
+
# O-3a sum possibe because either is nil (one of the hashes does not have the key at all)
|
55
|
+
v1
|
56
|
+
elsif v1.blank? && v2.present?
|
57
|
+
# O-3b sum possibe because either is nil (one of the hashes does not have the key at all)
|
58
|
+
v2
|
59
|
+
elsif v1.respond_to?(:to_f) && v2.respond_to?(:to_f)
|
60
|
+
# O-2 sum possible because both numerics
|
61
|
+
v1 + v2
|
62
|
+
elsif v1.present? && v2.present? && (v1.respond_to?(:to_f) != v2.respond_to?(:to_f))
|
63
|
+
# 0-4 error because types mismatch
|
64
|
+
raise ArgumentError.new("Sorry, can not sum these hashes, there's value collision in '#{key}' keys")
|
65
|
+
end
|
66
|
+
|
67
|
+
sum_hash[key] = sum_value
|
68
|
+
end
|
69
|
+
|
70
|
+
return sum_hash
|
71
|
+
end
|
72
|
+
|
39
73
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: creative_rails_utilities
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Creative
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|