data_list_converter 0.3.9 → 0.3.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/changelog.md +1 -0
- data/lib/data_list_converter/helper.rb +23 -0
- data/lib/data_list_converter/version.rb +1 -1
- data/test/helper_test.rb +11 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 85c9410c6b8c64a1af696f42485e098f43f432c2
|
4
|
+
data.tar.gz: 930b7bb74e9d441e183451d4329e639c06aa3cdf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b53cb6e80ac57b256b74100421b12d14c9e1f57bee172807bb0f6437efe27e2c2c8915705d52b0e1ff01855188a6e4a3ac274138f7623cbec648a187ba349631
|
7
|
+
data.tar.gz: c87df4a93536be36f7aec7d88c3c93325b39fe03ab09414d4d1533024569e6d550f46cfff724df48bd39612bd6b52727473acbe20b1fdd273b92194b4f9286fc
|
data/README.md
CHANGED
data/changelog.md
CHANGED
@@ -46,4 +46,27 @@ class DataListConverter
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
49
|
+
|
50
|
+
# flatten multi level item data into one level, example:
|
51
|
+
# {a: {b: 12}, c: {d: {e: 11}}}
|
52
|
+
# =>
|
53
|
+
# {:"a:b"=>12, :"c:d:e"=>11}
|
54
|
+
def self.flatten(data, sep=':')
|
55
|
+
out = {}
|
56
|
+
recursive_flatten(out, data, nil, sep)
|
57
|
+
out
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.recursive_flatten(out, data, header, sep)
|
61
|
+
data.each do |k, v|
|
62
|
+
k = header ? :"#{header}#{sep}#{k}" : k
|
63
|
+
case v
|
64
|
+
when Hash
|
65
|
+
recursive_flatten(out, v, k, sep)
|
66
|
+
else
|
67
|
+
out[k] = v
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
49
72
|
end
|
data/test/helper_test.rb
CHANGED
@@ -70,4 +70,15 @@ describe DataListConverter do
|
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
73
|
+
|
74
|
+
describe :flatten do
|
75
|
+
specify do
|
76
|
+
data = {a: {b: 12}, c: {d: {e: 11}}}
|
77
|
+
@c.flatten(data).must_equal({:"a:b"=>12, :"c:d:e"=>11})
|
78
|
+
|
79
|
+
# change sep
|
80
|
+
data = {a: {b: 12}, c: {d: {e: 11}}}
|
81
|
+
@c.flatten(data, '_').must_equal({:"a_b"=>12, :"c_d_e"=>11})
|
82
|
+
end
|
83
|
+
end
|
73
84
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_list_converter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- linjunhalida
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|