dothash 1.0.0 → 2.0.0
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 +6 -1
- data/lib/dothash/hash.rb +30 -5
- data/lib/dothash/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: 590537e3612fab0548b995256f5d5643bb389623
|
4
|
+
data.tar.gz: f2cf1b402a47d35de414b3a9924cfbc4f1937ea4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1b3492e36e382a228405b3a633b6c2324db71d01c7bfbb200671f743e6784b2d8a9f54781510c34a8652ba36b59448e3d1a1d4810273c5b8618f4ca241f36b3b
|
7
|
+
data.tar.gz: 2a4f3fd31dd6959c2e90dee1724eee0dc78f3b4c9f8a1ceba52deaf2be12b40c43008673faec6fe911de8ae6e09861d804b27fbe4ffcfea137fef55e4fbb7f97
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -24,13 +24,18 @@ Or install it yourself as:
|
|
24
24
|
```ruby
|
25
25
|
require "dothash"
|
26
26
|
hash = { x: { y: 1, z: { a1: 8, a2: 10 } }, v: [1, { y: 2, z: 3 }] }
|
27
|
-
dhash = Dothash::Hash.
|
27
|
+
dhash = Dothash::Hash.with_dots hash
|
28
28
|
|
29
29
|
puts dhash
|
30
30
|
# {"x.y"=>1, "x.z.a1"=>8, "x.z.a2"=>10, "v.0"=>1, "v.1.y"=>2, "v.1.z"=>3}
|
31
31
|
|
32
32
|
puts dh.class
|
33
33
|
# Hash
|
34
|
+
|
35
|
+
hash2 = Dothash::Hash.without_dots(dhash)
|
36
|
+
|
37
|
+
puts hash2
|
38
|
+
# {:x=>{:y=>1, :z=>{:a1=>8, :a2=>10}}, :v=>{:"0"=>1, :"1"=>{:y=>2, :z=>3}}}
|
34
39
|
```
|
35
40
|
|
36
41
|
## Versioning
|
data/lib/dothash/hash.rb
CHANGED
@@ -19,24 +19,49 @@
|
|
19
19
|
|
20
20
|
module Dothash
|
21
21
|
class Hash
|
22
|
-
def self.
|
22
|
+
def self.with_dots(hash, prefix = nil)
|
23
23
|
raise ArgumentError, "You should pass only Hash here" unless hash.is_a? ::Hash
|
24
24
|
hash.each_with_object({}) do |(key, value), memo|
|
25
25
|
new_key = [prefix, key].compact.join(".")
|
26
|
-
memo.merge!
|
26
|
+
memo.merge! with_dots_deeper(value, new_key)
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
private_class_method def self.
|
30
|
+
private_class_method def self.with_dots_deeper(value, new_key)
|
31
31
|
if value.is_a?(::Hash)
|
32
|
-
|
32
|
+
with_dots(value, new_key)
|
33
33
|
elsif value.is_a? ::Array
|
34
34
|
value.each_with_object({}).with_index do |(avalue, memo), index|
|
35
|
-
memo.merge!
|
35
|
+
memo.merge! with_dots({ index => avalue }, new_key)
|
36
36
|
end
|
37
37
|
else
|
38
38
|
{ new_key => value }
|
39
39
|
end
|
40
40
|
end
|
41
|
+
|
42
|
+
def self.without_dots(hash)
|
43
|
+
hash.each_with_object({}) do |(key, value), memo|
|
44
|
+
key_parts = key.to_s.split(".")
|
45
|
+
new_key = key_parts.shift.to_sym
|
46
|
+
if key_parts.empty?
|
47
|
+
memo[new_key] = value
|
48
|
+
else
|
49
|
+
deep_merge!(memo, new_key => without_dots(key_parts.join(".") => value))
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private_class_method def self.deep_merge!(first, second)
|
55
|
+
merger = proc do |_key, v1, v2|
|
56
|
+
if v1.is_a?(::Hash) && v2.is_a?(::Hash)
|
57
|
+
v1.merge(v2, &merger)
|
58
|
+
elsif v1.is_a?(::Array) && v2.is_a?(::Array)
|
59
|
+
v1 | v2
|
60
|
+
else
|
61
|
+
[:undefined, nil, :nil].include?(v2) ? v1 : v2
|
62
|
+
end
|
63
|
+
end
|
64
|
+
first.merge!(second.to_h, &merger)
|
65
|
+
end
|
41
66
|
end
|
42
67
|
end
|
data/lib/dothash/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dothash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Szymon Kopciewski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10-
|
11
|
+
date: 2016-10-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|