structure_flatter 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +13 -5
- data/.gitignore +0 -0
- data/.rspec +0 -0
- data/Gemfile +0 -0
- data/LICENSE.txt +0 -0
- data/README.md +0 -0
- data/Rakefile +0 -0
- data/lib/structure_flatter.rb +2 -2
- data/lib/structure_flatter/array_hash_structure.rb +21 -16
- data/lib/structure_flatter/version.rb +1 -1
- data/spec/inputs/various_key_value_pairs.rb +0 -0
- data/spec/inputs/various_structures.rb +0 -0
- data/spec/spec_helper.rb +0 -0
- data/spec/structure_flatter/array_hash_structure_spec.rb +55 -52
- data/structure_flatter.gemspec +0 -0
- metadata +23 -23
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ODAwYzk3MzRhMGM2NzQzOGNhM2NiNWE5NjdjY2JjOWJiNzNjZGFiNw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NzA3YWE0ZTM1YzY3MDBjMGI4NWQyNDY2OTczMDFiMzZlMzIwZmI5Mg==
|
5
7
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NTVlYzdmNmYwNmY0ZDFhNTY0ZWFiZDFkOGE3Y2E4MTM2YzMyMDI5MmQxMmEz
|
10
|
+
MzM3YTQ1MDczYzA5N2YwMGY3ZTA1NGIyMTU4NDNmNmYyNmFhMjVhM2E2NTI5
|
11
|
+
ZTMxZWEwMWVjMGJjYjA0ZDc2MWQzNzA4N2IyNjJlNjlhMDc4YTc=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZTUwZjJmNjFkNzQxZWY4ZWU3YmIyZGQwZTY1Zjg1MzI2ZmQ3Y2Q3MmJkNzdi
|
14
|
+
ZjE1YjI5Yzg2NTNhNzNkNmY1ZGU4NGIyYWFhNWUzYTVlZjU1NmFjYWM1MGU2
|
15
|
+
YjQ5ZjE1MTU0Yjk4N2IyOWRiMDg5Y2E0MDRiM2Y0YTllYjE4YmE=
|
data/.gitignore
CHANGED
File without changes
|
data/.rspec
CHANGED
File without changes
|
data/Gemfile
CHANGED
File without changes
|
data/LICENSE.txt
CHANGED
File without changes
|
data/README.md
CHANGED
File without changes
|
data/Rakefile
CHANGED
File without changes
|
data/lib/structure_flatter.rb
CHANGED
@@ -2,25 +2,27 @@ require 'active_support/core_ext/string'
|
|
2
2
|
|
3
3
|
module StructureFlatter
|
4
4
|
class ArrayHashStructure
|
5
|
+
|
6
|
+
attr_reader :ahs
|
5
7
|
|
6
|
-
#
|
8
|
+
# Initializing element
|
7
9
|
# passed structure should be a simple or complex structure comprising Hashes and Arrays
|
8
10
|
def initialize(array_hash_structure = nil)
|
9
|
-
@
|
11
|
+
@ahs = array_hash_structure
|
10
12
|
end
|
11
13
|
|
12
|
-
#
|
13
|
-
def flatten_structure!(
|
14
|
-
case
|
14
|
+
# Flattening the given structure
|
15
|
+
def flatten_structure!(strt = ahs)
|
16
|
+
case strt
|
15
17
|
when Array
|
16
|
-
|
18
|
+
strt.each do |array_element|
|
17
19
|
flatten_structure!(array_element) if ((array_element.class == Array) || (array_element.class == Hash))
|
18
20
|
end
|
19
21
|
when Hash
|
20
|
-
|
22
|
+
strt.each do |hash_key, hash_value|
|
21
23
|
if hash_value.class == Array
|
22
24
|
# trying to flatten the value
|
23
|
-
|
25
|
+
flatten_key_value_pair!(hash_key, hash_value)
|
24
26
|
# recursive call
|
25
27
|
flatten_structure!(hash_value)
|
26
28
|
elsif hash_value.class == Hash
|
@@ -35,21 +37,23 @@ module StructureFlatter
|
|
35
37
|
else
|
36
38
|
# do nothing
|
37
39
|
end
|
38
|
-
|
40
|
+
strt
|
39
41
|
end
|
40
42
|
|
41
|
-
|
42
|
-
|
43
|
-
|
43
|
+
private
|
44
|
+
|
45
|
+
# Trying to flatten the given key value pair
|
46
|
+
def flatten_key_value_pair!(key, value)
|
47
|
+
if is_key_value_pair_flattable?(key, value)
|
44
48
|
value.each_index do |index|
|
45
49
|
value[index] = value[index].values.first
|
46
50
|
end
|
47
51
|
end
|
48
|
-
|
52
|
+
[key, value]
|
49
53
|
end
|
50
54
|
|
51
|
-
#
|
52
|
-
def
|
55
|
+
# Checking if given key value pair is flattable
|
56
|
+
def is_key_value_pair_flattable?(key, value)
|
53
57
|
# key should be plural
|
54
58
|
return false unless key.to_s.pluralize == key.to_s
|
55
59
|
# value should be Array
|
@@ -63,7 +67,8 @@ module StructureFlatter
|
|
63
67
|
# its key should be singular form of original key
|
64
68
|
return false unless (value_element.keys.first.to_s != key.to_s) && (value_element.keys[0].to_s.pluralize == key.to_s)
|
65
69
|
end
|
66
|
-
|
70
|
+
true
|
67
71
|
end
|
68
72
|
end
|
69
73
|
end
|
74
|
+
|
File without changes
|
File without changes
|
data/spec/spec_helper.rb
CHANGED
File without changes
|
@@ -5,57 +5,7 @@ module StructureFlatter
|
|
5
5
|
|
6
6
|
include VariousKeyValuePairs
|
7
7
|
include VariousStructures
|
8
|
-
|
9
|
-
describe '.is_root_flattable?' do
|
10
|
-
it 'returns true for simple flattable key-value pair' do
|
11
|
-
expect(ArrayHashStructure.is_key_value_pair_flattable?(*key_value_pair_1)).to be true
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'returns false for non-flattable nil value' do
|
15
|
-
array = [:key, nil]
|
16
|
-
expect(ArrayHashStructure.is_key_value_pair_flattable?(*array)).to be false
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'returns false for non-flattable empty array as value' do
|
20
|
-
array = [:key, []]
|
21
|
-
expect(ArrayHashStructure.is_key_value_pair_flattable?(*array)).to be false
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'returns false for non-flattable empty hash as value' do
|
25
|
-
array = [:key, {}]
|
26
|
-
expect(ArrayHashStructure.is_key_value_pair_flattable?(*array)).to be false
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'returns false for non-flattable key-value pair' do
|
30
|
-
expect(ArrayHashStructure.is_key_value_pair_flattable?(*key_value_pair_1_result)).to be false
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
describe '.flatten_root!' do
|
35
|
-
it 'flattens flattable key-value pair' do
|
36
|
-
expect(ArrayHashStructure.flatten_key_value_pair!(*key_value_pair_1)).to eq(key_value_pair_1_result)
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'does not affect non-flattable key-value pair' do
|
40
|
-
expect(ArrayHashStructure.flatten_key_value_pair!(*key_value_pair_1_result)).to eq(key_value_pair_1_result)
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'does not affect non-flattable key-value pair with nil value' do
|
44
|
-
array = [:key, nil]
|
45
|
-
expect(ArrayHashStructure.flatten_key_value_pair!(*array)).to eq(array)
|
46
|
-
end
|
47
|
-
|
48
|
-
it 'does not affect non-flattable key-value pair with empty array as value' do
|
49
|
-
array = [:key, []]
|
50
|
-
expect(ArrayHashStructure.flatten_key_value_pair!(*array)).to eq(array)
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'does not affect non-flattable key-value pair with empty hash as value' do
|
54
|
-
array = [:key, {}]
|
55
|
-
expect(ArrayHashStructure.flatten_key_value_pair!(*array)).to eq(array)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
8
|
+
|
59
9
|
describe '#flatten_structure!' do
|
60
10
|
it 'returns nil structure as it is' do
|
61
11
|
str = ArrayHashStructure.new(nil_structure).flatten_structure!
|
@@ -91,5 +41,58 @@ module StructureFlatter
|
|
91
41
|
end
|
92
42
|
end
|
93
43
|
|
44
|
+
# For developer
|
45
|
+
describe 'PRIVATE_INTERFACE' do
|
46
|
+
describe '#is_key_value_pair_flattable?' do
|
47
|
+
it 'returns true for simple flattable key-value pair' do
|
48
|
+
expect(ArrayHashStructure.new.send(:is_key_value_pair_flattable?, *key_value_pair_1)).to be true
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'returns false for non-flattable nil value' do
|
52
|
+
array = [:key, nil]
|
53
|
+
expect(ArrayHashStructure.new.send(:is_key_value_pair_flattable?, *array)).to be false
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'returns false for non-flattable empty array as value' do
|
57
|
+
array = [:key, []]
|
58
|
+
expect(ArrayHashStructure.new.send(:is_key_value_pair_flattable?, *array)).to be false
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'returns false for non-flattable empty hash as value' do
|
62
|
+
array = [:key, {}]
|
63
|
+
expect(ArrayHashStructure.new.send(:is_key_value_pair_flattable?, *array)).to be false
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'returns false for non-flattable key-value pair' do
|
67
|
+
expect(ArrayHashStructure.new.send(:is_key_value_pair_flattable?, *key_value_pair_1_result)).to be false
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '#flatten_key_value_pair!' do
|
72
|
+
it 'flattens flattable key-value pair' do
|
73
|
+
expect(ArrayHashStructure.new.send(:flatten_key_value_pair!, *key_value_pair_1)).to eq(key_value_pair_1_result)
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'does not affect non-flattable key-value pair' do
|
77
|
+
expect(ArrayHashStructure.new.send(:flatten_key_value_pair!, *key_value_pair_1_result)).to eq(key_value_pair_1_result)
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'does not affect non-flattable key-value pair with nil value' do
|
81
|
+
array = [:key, nil]
|
82
|
+
expect(ArrayHashStructure.new.send(:flatten_key_value_pair!, *array)).to eq(array)
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'does not affect non-flattable key-value pair with empty array as value' do
|
86
|
+
array = [:key, []]
|
87
|
+
expect(ArrayHashStructure.new.send(:flatten_key_value_pair!, *array)).to eq(array)
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'does not affect non-flattable key-value pair with empty hash as value' do
|
91
|
+
array = [:key, {}]
|
92
|
+
expect(ArrayHashStructure.new.send(:flatten_key_value_pair!, *array)).to eq(array)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
94
97
|
end
|
95
|
-
end
|
98
|
+
end
|
data/structure_flatter.gemspec
CHANGED
File without changes
|
metadata
CHANGED
@@ -1,125 +1,125 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: structure_flatter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Munish Goyal
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ! '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.3'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ! '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ! '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0.9'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ! '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0.9'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: awesome_print
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ! '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '1.2'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ! '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '1.2'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: 3.0.0
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ~>
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 3.0.0
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: autotest
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ! '>='
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ! '>='
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: rspec-autotest
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - ! '>='
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - ! '>='
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: simplecov
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- -
|
101
|
+
- - ! '>='
|
102
102
|
- !ruby/object:Gem::Version
|
103
103
|
version: '0'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- -
|
108
|
+
- - ! '>='
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: activesupport
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- -
|
115
|
+
- - ~>
|
116
116
|
- !ruby/object:Gem::Version
|
117
117
|
version: '4.0'
|
118
118
|
type: :runtime
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- -
|
122
|
+
- - ~>
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '4.0'
|
125
125
|
description: Flattens hash-array structures by removing redundant information.
|
@@ -129,9 +129,9 @@ executables: []
|
|
129
129
|
extensions: []
|
130
130
|
extra_rdoc_files: []
|
131
131
|
files:
|
132
|
-
-
|
133
|
-
-
|
134
|
-
-
|
132
|
+
- .autotest
|
133
|
+
- .gitignore
|
134
|
+
- .rspec
|
135
135
|
- Gemfile
|
136
136
|
- LICENSE.txt
|
137
137
|
- README.md
|
@@ -154,12 +154,12 @@ require_paths:
|
|
154
154
|
- lib
|
155
155
|
required_ruby_version: !ruby/object:Gem::Requirement
|
156
156
|
requirements:
|
157
|
-
- -
|
157
|
+
- - ! '>='
|
158
158
|
- !ruby/object:Gem::Version
|
159
159
|
version: '1.9'
|
160
160
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
161
161
|
requirements:
|
162
|
-
- -
|
162
|
+
- - ! '>='
|
163
163
|
- !ruby/object:Gem::Version
|
164
164
|
version: '0'
|
165
165
|
requirements: []
|