hash_flatten 0.3.0 → 1.0.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 +4 -4
- data/.travis.yml +1 -2
- data/Gemfile +1 -3
- data/README.md +54 -27
- data/hash_flatten.gemspec +3 -4
- data/lib/hash_flatten.rb +15 -34
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83fff9dffa4f78c3437e291b31519ff7035a2665
|
4
|
+
data.tar.gz: 01c61a4e9a3c2877c9593064bb55ecf616bbc415
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e607f013258e812f6106a008d0fe5075ac66dd862b35dd3bbfb1151b8c2a930cfcba3dec2970cc2a6c48e4200fa30504667ce7399c61dce0d65c87dd2964d9ea
|
7
|
+
data.tar.gz: 64b47cfbcef60c76936051c5ae8afb2eb15778fb7f9317851e2a57ab26dab44603235f94fea6edd7b0a0c7229bcefe02b7952bba3854990aab5993342f495a72
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,32 +1,24 @@
|
|
1
|
-
# HashFlatten
|
1
|
+
# HashFlatten [](https://travis-ci.org/Masa331/hash_flatten)
|
2
2
|
|
3
|
-
|
3
|
+
Flatten nested Hash to one level depth with keys joined with a dot and vice versa.
|
4
4
|
|
5
|
-
|
5
|
+
Content:
|
6
|
+
1. [About](#about)
|
7
|
+
2. [How to](#how-to)
|
8
|
+
1. [Installation](#installation)
|
9
|
+
2. [Usage](#usage)
|
10
|
+
3. [squish_levels](#squish_levels)
|
11
|
+
4. [stretch_to_levels](#stretch_to_levels)
|
12
|
+
5. [Notes](#notes)
|
13
|
+
3. [License](#license)
|
6
14
|
|
7
|
-
|
8
|
-
b: { a: 'b' },
|
9
|
-
c: { b: { a: 'c' } } }
|
10
|
-
|
11
|
-
hash.destructure =>
|
12
|
-
|
13
|
-
{ 'a' => 'a',
|
14
|
-
'b.a' => 'b',
|
15
|
-
'c.b.a' => 'c' }
|
16
|
-
|
17
|
-
## `#structure`
|
18
|
-
|
19
|
-
hash = { 'a' => 'a',
|
20
|
-
'b.a' => 'b',
|
21
|
-
'c.b.a' => 'c' }
|
15
|
+
## About
|
22
16
|
|
23
|
-
hash.structure
|
17
|
+
This gem includes Refinement which adds `#squish_levels` and `#stretch_to_levels` to Hash. With them you can transform this hash: `{ a: { b: 'b', c: 'c' } }` to this: `{ 'a.b' => 'b', 'a.c' => 'c' }` and vice versa. I use these to transform Rails locales to somehow readable csv for external translators or showing XML structure(transformed to a hash) in a table.
|
24
18
|
|
25
|
-
|
26
|
-
'b' => { 'a' => 'b' },
|
27
|
-
'c' => { 'b' => { 'a' => 'c' } } }
|
19
|
+
## How to
|
28
20
|
|
29
|
-
|
21
|
+
### Installation
|
30
22
|
|
31
23
|
Add this line to your application's Gemfile:
|
32
24
|
|
@@ -42,17 +34,52 @@ Or install it yourself as:
|
|
42
34
|
|
43
35
|
$ gem install hash_flatten
|
44
36
|
|
45
|
-
|
37
|
+
### Usage
|
46
38
|
|
47
|
-
Use as
|
39
|
+
Use as a Refinement:
|
48
40
|
```
|
49
41
|
class MyClass
|
50
42
|
using HashFlatten
|
51
43
|
end
|
52
44
|
```
|
53
|
-
now you can call `#
|
45
|
+
now you can call `#squish_levels` and `#stretch_to_levels` on hashes in `MyClass`(lexically).
|
46
|
+
|
47
|
+
Here is an excellent blog post on Refinements if you didn't use them before: http://interblah.net/why-is-nobody-using-refinements
|
48
|
+
|
49
|
+
### `#squish_levels`
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
hash = { a: 'a',
|
53
|
+
b: { a: 'b' },
|
54
|
+
c: { b: { a: 'c' } } }
|
55
|
+
|
56
|
+
hash.squish_levels =>
|
57
|
+
|
58
|
+
{ 'a' => 'a',
|
59
|
+
'b.a' => 'b',
|
60
|
+
'c.b.a' => 'c' }
|
61
|
+
```
|
62
|
+
|
63
|
+
### `#stretch_to_levels`
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
hash = { 'a' => 'a',
|
67
|
+
'b.a' => 'b',
|
68
|
+
'c.b.a' => 'c' }
|
69
|
+
|
70
|
+
hash.stretch_to_levels =>
|
71
|
+
|
72
|
+
{ 'a' => 'a',
|
73
|
+
'b' => { 'a' => 'b' },
|
74
|
+
'c' => { 'b' => { 'a' => 'c' } } }
|
75
|
+
```
|
76
|
+
|
77
|
+
### Notes
|
78
|
+
|
79
|
+
Some things to know:
|
54
80
|
|
55
|
-
|
81
|
+
* whenever you call `#squish_levels` or `#stretch_to_levels` returned hash has keys transformed to strings
|
82
|
+
* squishes or stretches only sub Hashes, other hash-like structure aren't touched(`#to_h` isn't called on values..)
|
56
83
|
|
57
84
|
## License
|
58
85
|
|
data/hash_flatten.gemspec
CHANGED
@@ -4,19 +4,18 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = 'hash_flatten'
|
7
|
-
spec.version = '0.
|
7
|
+
spec.version = '1.0.0'
|
8
8
|
spec.authors = ['Premysl Donat']
|
9
9
|
spec.email = ['pdonat@seznam.cz']
|
10
10
|
|
11
|
-
spec.summary = '
|
12
|
-
spec.description =
|
11
|
+
spec.summary = 'Flatten nested Hash to one level depth with keys joined with a dot.'
|
12
|
+
spec.description = 'A refinement which includes a method for flattening nested Hash to one level depth with keys joined with a dot. Also includes a method for reverse operation.'
|
13
13
|
spec.homepage = 'https://github.com/Masa331/hash_flatten'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
17
|
f.match(%r{^(test|spec|features)/})
|
18
18
|
end
|
19
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
19
|
spec.require_paths = ['lib']
|
21
20
|
|
22
21
|
spec.add_development_dependency 'bundler'
|
data/lib/hash_flatten.rb
CHANGED
@@ -1,48 +1,29 @@
|
|
1
1
|
module HashFlatten
|
2
2
|
refine Hash do
|
3
|
-
def
|
4
|
-
|
5
|
-
if
|
6
|
-
|
7
|
-
n["#{k}.#{k2}"] = v2
|
8
|
-
end
|
3
|
+
def squish_levels
|
4
|
+
each_with_object({}) do |(key, value), squished|
|
5
|
+
if value.is_a? Hash
|
6
|
+
value.squish_levels.each { |sub_key, sub_value| squished.store("#{key}.#{sub_key}", sub_value) }
|
9
7
|
else
|
10
|
-
|
8
|
+
squished.store(key.to_s, value)
|
11
9
|
end
|
12
10
|
end
|
13
|
-
|
14
|
-
if flattened.any? { |_, v| v.is_a? Hash }
|
15
|
-
flattened.destructure
|
16
|
-
else
|
17
|
-
flattened
|
18
|
-
end
|
19
11
|
end
|
20
12
|
|
21
|
-
def
|
22
|
-
|
13
|
+
def stretch_to_levels
|
14
|
+
each_with_object({}) do |(key, value), stretched|
|
15
|
+
key_parts = key.to_s.split('.')
|
23
16
|
|
24
|
-
|
25
|
-
|
26
|
-
|
17
|
+
if key_parts.size > 1
|
18
|
+
level_key = key_parts.shift
|
19
|
+
tail = key_parts.join('.')
|
27
20
|
|
28
|
-
|
29
|
-
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def decompose_keys
|
34
|
-
each_with_object({}) do |(k, v), new_hash|
|
35
|
-
key_parts = k.split('.')
|
36
|
-
top_level_key = key_parts.shift
|
37
|
-
tail = key_parts.join('.')
|
21
|
+
existing_content = stretched.fetch(level_key, {})
|
22
|
+
new_content = existing_content.merge({ tail => value }).stretch_to_levels
|
38
23
|
|
39
|
-
|
40
|
-
new_hash[top_level_key] = v
|
24
|
+
stretched.store(level_key, new_content)
|
41
25
|
else
|
42
|
-
|
43
|
-
new_value = existing_value.merge({ tail => v })
|
44
|
-
|
45
|
-
new_hash[top_level_key] = new_value
|
26
|
+
stretched.store(key.to_s, value)
|
46
27
|
end
|
47
28
|
end
|
48
29
|
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.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Premysl Donat
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,7 +66,8 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
-
description:
|
69
|
+
description: A refinement which includes a method for flattening nested Hash to one
|
70
|
+
level depth with keys joined with a dot. Also includes a method for reverse operation.
|
70
71
|
email:
|
71
72
|
- pdonat@seznam.cz
|
72
73
|
executables: []
|
@@ -107,5 +108,5 @@ rubyforge_project:
|
|
107
108
|
rubygems_version: 2.6.14
|
108
109
|
signing_key:
|
109
110
|
specification_version: 4
|
110
|
-
summary:
|
111
|
+
summary: Flatten nested Hash to one level depth with keys joined with a dot.
|
111
112
|
test_files: []
|