hash_flatten 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/README.md +17 -3
- data/lib/hash_flatten.rb +24 -3
- data/lib/hash_flatten/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bfbdea8d024a61cf392f1dd1a1abb7e45bcb416e63afd1bdcaba7c180233e4a1
|
4
|
+
data.tar.gz: 035c49bfe889d938e459c99742bfec42c35dd00627a8a847af09470afca5017a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 63038064be7b3d3ccf9fc6e6e8b7404e03edfb6f3f44ebdaea677f1e5baafe73c5a653151e2aba52f73a82e28f61d2bb1f8764668d0ee8fddc22c1da0dff12fa
|
7
|
+
data.tar.gz: cffa3f4cad9de08cb045b6c5ef6551f05170e88ab2018ca8b6bf088c19942da7185805ee83f261f78b32988e6068b6e90d40bb3f539ee8938de0af65d0efe7dc
|
data/README.md
CHANGED
@@ -1,17 +1,31 @@
|
|
1
1
|
# HashFlatten
|
2
2
|
|
3
|
-
|
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.
|
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 `#
|
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
|
1
|
+
require 'hash_flatten/version'
|
2
2
|
|
3
3
|
module HashFlatten
|
4
4
|
refine Hash do
|
5
|
-
def
|
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.
|
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
|
data/lib/hash_flatten/version.rb
CHANGED
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.
|
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:
|
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.
|
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
|