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 CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 13e78a1830411d2771d2a41fc82b94f8939c19ca
4
- data.tar.gz: 744aa5b307c9289059decb60ae0587b01a19fae8
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ODAwYzk3MzRhMGM2NzQzOGNhM2NiNWE5NjdjY2JjOWJiNzNjZGFiNw==
5
+ data.tar.gz: !binary |-
6
+ NzA3YWE0ZTM1YzY3MDBjMGI4NWQyNDY2OTczMDFiMzZlMzIwZmI5Mg==
5
7
  SHA512:
6
- metadata.gz: 29e98e8abfc39f908e4c9f5bebd7219f7e4a07326f413e0da3a4dc583b1461533ee7a284dadf8db9f8ee27d6627def082ac356aab6e08c339fe781bfe2cff657
7
- data.tar.gz: bf7ab9a34bb15554c6b352edcc83013864815ec3f5a8c4894b9801dcba6d78af090b9e2e419b63976b7bc47bb7207869974a1e7accd0f83ba7344f690960ca97
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
File without changes
data/README.md CHANGED
File without changes
data/Rakefile CHANGED
File without changes
@@ -1,5 +1,5 @@
1
- require 'structure_flatter/version'
2
- require 'structure_flatter/array_hash_structure'
1
+ require_relative 'structure_flatter/version'
2
+ require_relative 'structure_flatter/array_hash_structure'
3
3
 
4
4
  module StructureFlatter
5
5
  # Your code goes here...
@@ -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
- # initializing element
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
- @array_hash_structure = array_hash_structure
11
+ @ahs = array_hash_structure
10
12
  end
11
13
 
12
- # it flattens the given structure
13
- def flatten_structure!(array_hash_structure = @array_hash_structure)
14
- case array_hash_structure
14
+ # Flattening the given structure
15
+ def flatten_structure!(strt = ahs)
16
+ case strt
15
17
  when Array
16
- array_hash_structure.each do |array_element|
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
- array_hash_structure.each do |hash_key, hash_value|
22
+ strt.each do |hash_key, hash_value|
21
23
  if hash_value.class == Array
22
24
  # trying to flatten the value
23
- ArrayHashStructure.flatten_key_value_pair!(hash_key, hash_value)
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
- return array_hash_structure
40
+ strt
39
41
  end
40
42
 
41
- # it tries to flatten the given key value pair
42
- def self.flatten_key_value_pair!(key, value)
43
- if self.is_key_value_pair_flattable?(key, value)
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
- return [key, value]
52
+ [key, value]
49
53
  end
50
54
 
51
- # check if given key value pair is flattable
52
- def self.is_key_value_pair_flattable?(key, value)
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
- return true
70
+ true
67
71
  end
68
72
  end
69
73
  end
74
+
@@ -1,3 +1,3 @@
1
1
  module StructureFlatter
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
File without changes
File without changes
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
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.1.1
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: 2014-08-05 00:00:00.000000000 Z
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
- - ".autotest"
133
- - ".gitignore"
134
- - ".rspec"
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: []