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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f9370d6174074e9e2dd6eb90097dc555664fa58c
4
- data.tar.gz: 744f5ce43630e70f08456cf8c75e5a11818dee3c
3
+ metadata.gz: 590537e3612fab0548b995256f5d5643bb389623
4
+ data.tar.gz: f2cf1b402a47d35de414b3a9924cfbc4f1937ea4
5
5
  SHA512:
6
- metadata.gz: ff629422c02c99ebf91b4ad719d83200f710e4df5bf65d701cc3e6b6a7e201f8bbcd8a0549ed7dfd5afbe6b0caecb74da06c6e8bdea33e4394331470b255b021
7
- data.tar.gz: b1552126c0ab4a38e5f7b30fd4039a06730f22155b7d600787e0fd47334594414b416996774ad4f3a381f182fcd6f461197fdcdf2d14d6446169b99774d7a564
6
+ metadata.gz: 1b3492e36e382a228405b3a633b6c2324db71d01c7bfbb200671f743e6784b2d8a9f54781510c34a8652ba36b59448e3d1a1d4810273c5b8618f4ca241f36b3b
7
+ data.tar.gz: 2a4f3fd31dd6959c2e90dee1724eee0dc78f3b4c9f8a1ceba52deaf2be12b40c43008673faec6fe911de8ae6e09861d804b27fbe4ffcfea137fef55e4fbb7f97
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ === 2.0.0 (2016.10.05)
2
+
3
+ * add conversion from opposite direction
4
+
1
5
  === 1.0.0 (2016.10.04)
2
6
 
3
7
  * publication of the code
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.convert 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.convert(hash, prefix = nil)
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! go_deep(value, new_key)
26
+ memo.merge! with_dots_deeper(value, new_key)
27
27
  end
28
28
  end
29
29
 
30
- private_class_method def self.go_deep(value, new_key)
30
+ private_class_method def self.with_dots_deeper(value, new_key)
31
31
  if value.is_a?(::Hash)
32
- convert(value, new_key)
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! convert({ index => avalue }, new_key)
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
@@ -18,5 +18,5 @@
18
18
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
19
 
20
20
  module Dothash
21
- VERSION = "1.0.0".freeze
21
+ VERSION = "2.0.0".freeze
22
22
  end
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: 1.0.0
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-04 00:00:00.000000000 Z
11
+ date: 2016-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake