hash_flatten 0.1.0 → 0.2.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
- SHA1:
3
- metadata.gz: 911f8f9a7d5972d022bac467c457e687b7466a3a
4
- data.tar.gz: 391ebb1619303004754ef581240111d8eab12cd5
2
+ SHA256:
3
+ metadata.gz: bfbdea8d024a61cf392f1dd1a1abb7e45bcb416e63afd1bdcaba7c180233e4a1
4
+ data.tar.gz: 035c49bfe889d938e459c99742bfec42c35dd00627a8a847af09470afca5017a
5
5
  SHA512:
6
- metadata.gz: ae05a8de2e941877ff0828dcd587ae67b7320151ebc3afef5b83a2fafbb3f516e1e153eb09994889ede3afca4ee4d53357374f07598ed62a6b035c764e23b3cb
7
- data.tar.gz: 212d4404ff0821f634ecfa9acaac824b6aae12a7cd909f600003b681c472eb306745c80f33608b238c332353e4b8ef966498d295c00e4b91cdde1c9db4a05b8c
6
+ metadata.gz: 63038064be7b3d3ccf9fc6e6e8b7404e03edfb6f3f44ebdaea677f1e5baafe73c5a653151e2aba52f73a82e28f61d2bb1f8764668d0ee8fddc22c1da0dff12fa
7
+ data.tar.gz: cffa3f4cad9de08cb045b6c5ef6551f05170e88ab2018ca8b6bf088c19942da7185805ee83f261f78b32988e6068b6e90d40bb3f539ee8938de0af65d0efe7dc
data/README.md CHANGED
@@ -1,17 +1,31 @@
1
1
  # HashFlatten
2
2
 
3
- Just one method i sometimes miss on Hash. It recursivelly flattens hash to be just one level deep.
3
+ Two methods i sometimes miss on Hash.
4
+
5
+ ## `#destructure`
4
6
 
5
7
  hash = { a: 'a',
6
8
  b: { a: 'b' },
7
9
  c: { b: { a: 'c' } } }
8
10
 
9
- hash.flatten =>
11
+ hash.destructure =>
10
12
 
11
13
  { 'a' => 'a',
12
14
  'b.a' => 'b',
13
15
  'c.b.a' => 'c' }
14
16
 
17
+ ## `#structure`
18
+
19
+ hash = { 'a' => 'a',
20
+ 'b.a' => 'b',
21
+ 'c.b.a' => 'c' }
22
+
23
+ hash.structure =>
24
+
25
+ { 'a' => 'a',
26
+ 'b' => { 'a' => 'b' },
27
+ 'c' => { 'b' => { 'a' => 'c' } } }
28
+
15
29
  ## Installation
16
30
 
17
31
  Add this line to your application's Gemfile:
@@ -36,7 +50,7 @@ class MyClass
36
50
  using HashFlatten
37
51
  end
38
52
  ```
39
- now you can call `#flatten` on hashes in `MyClass`.
53
+ now you can call `#destructure` od `#structure` on hashes in `MyClass`.
40
54
 
41
55
  Here is an excellent blog post on refinements if you didn't use them before: http://interblah.net/why-is-nobody-using-refinements
42
56
 
data/lib/hash_flatten.rb CHANGED
@@ -1,8 +1,8 @@
1
- require "hash_flatten/version"
1
+ require 'hash_flatten/version'
2
2
 
3
3
  module HashFlatten
4
4
  refine Hash do
5
- def flatten
5
+ def destructure
6
6
  flattened = each_with_object({}) do |(k, v), n|
7
7
  if v.is_a? Hash
8
8
  v.each do |k2, v2|
@@ -14,10 +14,31 @@ module HashFlatten
14
14
  end
15
15
 
16
16
  if flattened.any? { |_, v| v.is_a? Hash }
17
- flattened.flatten
17
+ flattened.destructure
18
18
  else
19
19
  flattened
20
20
  end
21
21
  end
22
+
23
+ def structure
24
+ structured = each_with_object({}) do |(k, v), n|
25
+ if k.include? '.'
26
+ keys = k.split('.')
27
+ new_key = keys.pop
28
+
29
+ n[keys.join('.')] = n.fetch(keys.join('.'), {}).merge({ new_key.to_s => v })
30
+ else
31
+ n[k.to_s] = v
32
+ end
33
+ end
34
+
35
+ structured
36
+
37
+ if structured.any? { |k, v| k.include? '.' }
38
+ structured.structure
39
+ else
40
+ structured
41
+ end
42
+ end
22
43
  end
23
44
  end
@@ -1,3 +1,3 @@
1
1
  module HashFlatten
2
- VERSION = "0.1.0"
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_flatten
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Premysl Donat
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-18 00:00:00.000000000 Z
11
+ date: 2018-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  version: '0'
92
92
  requirements: []
93
93
  rubyforge_project:
94
- rubygems_version: 2.6.8
94
+ rubygems_version: 2.7.3
95
95
  signing_key:
96
96
  specification_version: 4
97
97
  summary: Just one method i miss in std lib