key_flatten 0.1.0 → 1.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 +18 -0
- data/README.md +14 -2
- data/lib/key_flatten.rb +26 -5
- data/lib/key_flatten/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b05cb3c2f7f68155ec66fe5335f32a8fd41a9c1
|
4
|
+
data.tar.gz: 634b3d5e73c7253ecb31d929be58cb7d0504a4f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef0ce0c974d9ea6ac34919f502519f53b80d21166c12d1b8370227e5b73b6df7bec6d9c6cfc5fcc94c475d935629ab231b1b03d8b97f8e2df3f02bd555dfddb3
|
7
|
+
data.tar.gz: 3c8b79f7ed8cde43883a46a028810cf4d86b4751a171b5f307b1d7f9d3e44daeb87d0b9aaa3c9bcf418b53c50a30046ece3907bd3e5edc9bf36a9147ffefabb0
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# Change Log
|
2
|
+
|
3
|
+
## [1.0.0] - 2017-03-06
|
4
|
+
|
5
|
+
### Added
|
6
|
+
|
7
|
+
- `KeyFlatten.key_unflatten` method
|
8
|
+
- `Hash#key_flatten` method for core extension
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
|
12
|
+
- The option `symbolize` is now `symbolize_keys`
|
13
|
+
|
14
|
+
## [0.1.0] - 2016-11-06
|
15
|
+
|
16
|
+
### Added
|
17
|
+
|
18
|
+
- Initial release
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# key_flatten
|
2
2
|
|
3
|
-
Flatten keys of `Hash`.
|
3
|
+
Flatten/unflatten keys of `Hash`.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -27,6 +27,9 @@ require 'key_flatten'
|
|
27
27
|
|
28
28
|
KeyFlatten.key_flatten({"foo" => {"bar" => "baz"}})
|
29
29
|
# => {"foo.bar" => "baz"}
|
30
|
+
|
31
|
+
KeyFlatten.key_unflatten({"foo.bar" => "baz"})
|
32
|
+
# => {"foo" => {"bar" => "baz"}}
|
30
33
|
```
|
31
34
|
|
32
35
|
### Monkey patching
|
@@ -36,6 +39,9 @@ require 'key_flatten/core_ext'
|
|
36
39
|
|
37
40
|
{"foo" => {"bar" => "baz"}}.key_flatten
|
38
41
|
# => {"foo.bar" => "baz"}
|
42
|
+
|
43
|
+
{"foo.bar" => "baz"}.key_unflatten
|
44
|
+
# => {"foo" => {"bar" => "baz"}}
|
39
45
|
```
|
40
46
|
|
41
47
|
### Specify delimiter
|
@@ -43,13 +49,19 @@ require 'key_flatten/core_ext'
|
|
43
49
|
```rb
|
44
50
|
{"foo" => {"bar" => "baz"}}.key_flatten(delimiter: "_")
|
45
51
|
# => {"foo_bar" => "baz"}
|
52
|
+
|
53
|
+
{"foo_bar" => "baz"}.key_unflatten(delimiter: "_")
|
54
|
+
# => {"foo" => {"bar" => "baz"}}
|
46
55
|
```
|
47
56
|
|
48
57
|
### Symbolize keys
|
49
58
|
|
50
59
|
```rb
|
51
|
-
{"foo" => {"bar" => "baz"}}.key_flatten(
|
60
|
+
{"foo" => {"bar" => "baz"}}.key_flatten(symbolize_keys: true)
|
52
61
|
# => {:"foo.bar" => "baz"}
|
62
|
+
|
63
|
+
{"foo.bar" => "baz"}.key_unflatten(symbolize_keys: true)
|
64
|
+
# => {foo: {bar: "baz"}}
|
53
65
|
```
|
54
66
|
|
55
67
|
## Development
|
data/lib/key_flatten.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require "key_flatten/version"
|
2
2
|
|
3
3
|
module KeyFlatten
|
4
|
-
def self.key_flatten(hash,
|
4
|
+
def self.key_flatten(hash, symbolize_keys: false, delimiter: '.', prefix: nil, result: {})
|
5
5
|
hash.each do |k, v|
|
6
6
|
flat_key = prefix.nil? ? k.to_s : "#{prefix}#{delimiter}#{k}"
|
7
|
-
flat_key =
|
7
|
+
flat_key = symbolize_keys ? flat_key.to_sym : flat_key.to_s
|
8
8
|
|
9
9
|
if v.is_a?(Hash)
|
10
|
-
self.key_flatten(v,
|
10
|
+
self.key_flatten(v, symbolize_keys: symbolize_keys, delimiter: delimiter, prefix: flat_key, result: result)
|
11
11
|
else
|
12
12
|
result[flat_key] = v
|
13
13
|
end
|
@@ -16,7 +16,28 @@ module KeyFlatten
|
|
16
16
|
result
|
17
17
|
end
|
18
18
|
|
19
|
-
def
|
20
|
-
|
19
|
+
def self.key_unflatten(hash, symbolize_keys: false, delimiter: '.', result: {})
|
20
|
+
hash.each do |k, v|
|
21
|
+
keys = k.to_s.split(delimiter)
|
22
|
+
last_key = symbolize_keys ? keys.pop.to_sym : keys.pop
|
23
|
+
|
24
|
+
h = keys.reduce(result) do |h, k|
|
25
|
+
k = k.to_sym if symbolize_keys
|
26
|
+
h[k] = {} unless h.key? k
|
27
|
+
h[k]
|
28
|
+
end
|
29
|
+
|
30
|
+
h[last_key] = v
|
31
|
+
end
|
32
|
+
|
33
|
+
result
|
34
|
+
end
|
35
|
+
|
36
|
+
def key_flatten(symbolize_keys: false, delimiter: '.', prefix: nil)
|
37
|
+
::KeyFlatten.key_flatten(self, symbolize_keys: symbolize_keys, delimiter: delimiter, prefix: prefix)
|
38
|
+
end
|
39
|
+
|
40
|
+
def key_unflatten(symbolize_keys: false, delimiter: '.')
|
41
|
+
::KeyFlatten.key_unflatten(self, symbolize_keys: symbolize_keys, delimiter: delimiter)
|
21
42
|
end
|
22
43
|
end
|
data/lib/key_flatten/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: key_flatten
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuya Takeyama
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-03-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- ".gitignore"
|
63
63
|
- ".rspec"
|
64
64
|
- ".travis.yml"
|
65
|
+
- CHANGELOG.md
|
65
66
|
- Gemfile
|
66
67
|
- LICENSE.txt
|
67
68
|
- README.md
|
@@ -92,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
93
|
version: '0'
|
93
94
|
requirements: []
|
94
95
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.
|
96
|
+
rubygems_version: 2.6.8
|
96
97
|
signing_key:
|
97
98
|
specification_version: 4
|
98
99
|
summary: Flatten keys of Hash
|