config_volumizer 0.1.0 → 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,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 51fccd17634bc4c6605169c3f0aaa1552ed7e09b
4
- data.tar.gz: 7eedb6855853b5e3e643bfd95dc4286b4ef6a654
3
+ metadata.gz: 10a449433b7b4ca3479a4c362f276f9b74a07836
4
+ data.tar.gz: 7330a1c32aed1e002d9d8d748b2af31989815925
5
5
  SHA512:
6
- metadata.gz: a451d91280f236cbc6fc3442f1be856542d5042f7ba1113fb4996a78f11b44a4f121c342edcb76138eea9459dffb553c71638b65ad7bf5b64acbfd56285fce6c
7
- data.tar.gz: 493db1a62e270c7f0d01c4ca2545b85f9e38c33760ae6b3f4b24466210beff9dfda2625939bad0eed7dbdb058a869df008247017d97de59496058b326de97109
6
+ metadata.gz: f175ab8adcbc7da7d5da1b8af4c8d91838a4a07c03b9ad14fa2c40a6f8dcc6306b90928ce3fe29c34136c299a1bd6be60ca580f6582d56e8ca040d4903bbf6f1
7
+ data.tar.gz: 0d8b1f5b36cfef85ed2cae3a9c36857afa4f4b838271b3a36838306690b067c47936a1376f195f929aceca59ea12657337ac8b95e6043debc9d2faa6adfb706e
data/ChangeLog.md CHANGED
@@ -1,4 +1,5 @@
1
- ### 0.1.0 / 2015-04-03
1
+ ### 0.2.0 / 2015-04-03
2
2
 
3
3
  * Initial release:
4
-
4
+ - Implements parsing
5
+ - Implements generation
data/README.md CHANGED
@@ -46,9 +46,55 @@ ConfigVolumizer.parse(ENV, 'some')
46
46
 
47
47
  ## Features
48
48
 
49
- The gem basically smartly parses all keys within the passed in Hash that match the prefix specified.
49
+ ### Parsing
50
50
 
51
- It allows to nest either arrays or hashes inside eachother, basically any reasonable combination of hash/array notation should work.
51
+ You can parse a flattened config via `ConfigVolumizer.parse(ENV, 'some')`
52
+
53
+ For example if your ENV was:
54
+
55
+ ```
56
+ some.setting[0] = one
57
+ some.setting[1] = two
58
+ some.setting[2] = three
59
+ some.with.another = setting
60
+ ```
61
+
62
+ This would yield a {Hash} like the following:
63
+
64
+ ```yaml
65
+ some:
66
+ setting:
67
+ - one
68
+ - two
69
+ - three
70
+ with:
71
+ another: setting
72
+ ```
73
+
74
+ ### Generation
75
+
76
+ You can generate a flat list of configs (mostly as examples for your actual config) via:
77
+ `ConfigVolumizer.generate(data_hash)`
78
+
79
+ For example, given a hash simillar to:
80
+ ```yaml
81
+ some:
82
+ setting:
83
+ - one
84
+ - two
85
+ - three
86
+ with:
87
+ another: setting
88
+ ```
89
+
90
+ You would get back a hash looking like this:
91
+
92
+ ```yaml
93
+ "some.setting[0]": one
94
+ "some.setting[1]": two
95
+ "some.setting[2]": three
96
+ "some.with.another": setting
97
+ ```
52
98
 
53
99
  ## Install
54
100
 
@@ -0,0 +1,37 @@
1
+ module ConfigVolumizer
2
+
3
+ # Converts a {Hash} into a flattened {Hash} as a template for volumizing your configs
4
+ module Generator
5
+ class << self
6
+
7
+ # Generates a flattened config out of a data hash
8
+ #
9
+ # See Readme for an example
10
+ #
11
+ # @param [Hash] data
12
+ # @return [Hash]
13
+ def generate(data)
14
+ data.inject({}) do |result, (key, value)|
15
+ process_item(result, key, value)
16
+ result
17
+ end
18
+ end
19
+
20
+ def process_item(result, prefix, value)
21
+ case value
22
+ when Array
23
+ value.each_with_index do |item, index|
24
+ process_item(result, "#{prefix}[#{index}]", item)
25
+ end
26
+ when Hash
27
+ value.each do |key, item|
28
+ process_item(result, "#{prefix}.#{key}", item)
29
+ end
30
+ else
31
+ result[prefix] = value
32
+ end
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -6,6 +6,14 @@ module ConfigVolumizer
6
6
 
7
7
  class << self
8
8
 
9
+ # Parses keys within the {source} hash matching {base_name}
10
+ # returning a hash with all the matched data under a string key matching the {base_name}
11
+ #
12
+ # See Readme for an example
13
+ #
14
+ # @param [Hash] source
15
+ # @param [String] base_name
16
+ # @return [Hash]
9
17
  def parse(source, base_name)
10
18
  result = {}
11
19
  source.each do |key, value|
@@ -1,4 +1,3 @@
1
1
  module ConfigVolumizer
2
- # config_volumizer version
3
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
4
3
  end
@@ -1,12 +1,31 @@
1
1
  require 'config_volumizer/version'
2
2
  require 'config_volumizer/parser'
3
+ require 'config_volumizer/generator'
3
4
 
4
5
  module ConfigVolumizer
5
6
  class << self
6
7
 
8
+ # Parses keys within the {source} hash matching {base_name}
9
+ # returning a hash with all the matched data under a string key matching the {base_name}
10
+ #
11
+ # @see ConfigVolumizer::Parser.parse
12
+ #
13
+ # @param [Hash] source
14
+ # @param [String] base_name
15
+ # @return [Hash]
7
16
  def parse(source, base_name)
8
17
  Parser.parse(source, base_name)
9
18
  end
10
19
 
20
+ # Generates a flattened config out of a data hash
21
+ #
22
+ # @see ConfigVolumizer::Generator.generate
23
+ #
24
+ # @param [Hash] data
25
+ # @return [Hash]
26
+ def generate(data)
27
+ Generator.generate(data)
28
+ end
29
+
11
30
  end
12
31
  end
@@ -1,224 +1,225 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ConfigVolumizer do
4
- let(:key) { 'ex' }
5
- let(:result) { described_class.parse(input, key) }
6
-
7
- describe "1 level hash" do
8
- let(:input) { { "ex" => "1a" } }
9
- let(:expected_result) { { 'ex' => '1a' } }
10
-
11
- example do
12
- expect(result).to eq(expected_result)
13
- end
14
- end
15
-
16
- describe "2 level hash" do
17
- let(:key) { 'ex1' }
18
- let(:input) do
19
- {
20
- "ex1.bar" => "1a",
21
- "ex1.bar2" => "2a",
22
- }
23
- end
24
- let(:expected_result) do
25
- {
26
- 'ex1' => {
27
- 'bar' => '1a',
28
- 'bar2' => '2a',
29
- },
30
- }
31
- end
32
-
33
- example do
34
- expect(result).to eq(expected_result)
35
- end
36
- end
37
-
38
- describe "3 level hash" do
39
- let(:key) { 'ex3' }
40
- let(:input) do
41
- {
42
- "ex3.one.two" => "1a",
43
- "ex3.one.three" => "2a",
44
- "ex3.two.three" => "3a",
45
- }
46
- end
47
- let(:expected_result) do
48
- {
49
- 'ex3' => {
50
- 'one' => {
51
- 'two' => '1a',
52
- 'three' => '2a',
4
+ describe '.parse' do
5
+ let(:key) { 'ex' }
6
+ let(:result) { described_class.parse(input, key) }
7
+
8
+ describe "1 level hash" do
9
+ let(:input) { { "ex" => "1a" } }
10
+ let(:expected_result) { { 'ex' => '1a' } }
11
+
12
+ example do
13
+ expect(result).to eq(expected_result)
14
+ end
15
+ end
16
+
17
+ describe "2 level hash" do
18
+ let(:key) { 'ex1' }
19
+ let(:input) do
20
+ {
21
+ "ex1.bar" => "1a",
22
+ "ex1.bar2" => "2a",
23
+ }
24
+ end
25
+ let(:expected_result) do
26
+ {
27
+ 'ex1' => {
28
+ 'bar' => '1a',
29
+ 'bar2' => '2a',
53
30
  },
54
- 'two' => {
55
- 'three' => '3a',
56
- },
57
- },
58
- }
59
- end
60
-
61
- example do
62
- expect(result).to eq(expected_result)
63
- end
64
- end
65
-
66
- describe "array of values in hash" do
67
- let(:input) do
68
- {
69
- "ex.one[0]" => "1a",
70
- "ex.one[1]" => "2a",
71
- }
72
- end
73
- let(:expected_result) do
74
- {
75
- 'ex' => { 'one' => ['1a', '2a'] }
76
- }
77
- end
78
-
79
- example do
80
- expect(result).to eq(expected_result)
81
- end
82
- end
83
-
84
- describe "just array of values" do
85
- let(:input) do
86
- {
87
- "ex[0]" => "1a",
88
- "ex[1]" => "2a",
89
- }
90
- end
91
- let(:expected_result) do
92
- {
93
- 'ex' => ['1a', '2a']
94
- }
95
- end
96
-
97
- example do
98
- expect(result).to eq(expected_result)
99
- end
100
- end
101
-
102
- describe "array of hashes" do
103
- let(:input) do
104
- {
105
- "ex[0].foo" => "1a",
106
- "ex[0].bar" => "2a",
107
- "ex[1].foo" => "3a",
108
- }
109
- end
110
- let(:expected_result) do
111
- {
112
- 'ex' => [
113
- { 'foo' => '1a', 'bar' => '2a' },
114
- { 'foo' => '3a' },
115
- ]
116
- }
117
- end
118
-
119
- example do
120
- expect(result).to eq(expected_result)
121
- end
122
- end
123
-
124
- describe "array of hash in hash" do
125
- let(:key) { 'ex2' }
126
- let(:input) do
127
- {
128
- "ex2.one[0].foo" => "1a",
129
- "ex2.one[0].bar" => "2a",
130
- "ex2.one[1].foo" => "3a",
131
- }
132
- end
133
- let(:expected_result) do
134
- {
135
- 'ex2' => {
136
- 'one' => [
137
- {
138
- "foo" => '1a',
139
- "bar" => '2a',
31
+ }
32
+ end
33
+
34
+ example do
35
+ expect(result).to eq(expected_result)
36
+ end
37
+ end
38
+
39
+ describe "3 level hash" do
40
+ let(:key) { 'ex3' }
41
+ let(:input) do
42
+ {
43
+ "ex3.one.two" => "1a",
44
+ "ex3.one.three" => "2a",
45
+ "ex3.two.three" => "3a",
46
+ }
47
+ end
48
+ let(:expected_result) do
49
+ {
50
+ 'ex3' => {
51
+ 'one' => {
52
+ 'two' => '1a',
53
+ 'three' => '2a',
140
54
  },
141
- {
142
- "foo" => '3a',
143
- }
144
- ],
145
- },
146
- }
147
- end
148
-
149
- example do
150
- expect(result).to eq(expected_result)
151
- end
152
- end
153
-
154
- describe "empty hash value" do
155
- let(:input) { { "ex" => "{}", } }
156
- let(:expected_result) { { 'ex' => {} } }
157
-
158
- example do
159
- expect(result).to eq(expected_result)
160
- end
161
- end
162
-
163
- describe "empty array value" do
164
- let(:input) { { "ex" => "[]", } }
165
- let(:expected_result) { { 'ex' => [] } }
166
-
167
- example do
168
- expect(result).to eq(expected_result)
169
- end
170
- end
171
-
172
- describe "2d arrays" do
173
- let(:input) do
174
- {
175
- "ex[0][0]" => "1a",
176
- "ex[0][1]" => "2a",
177
- "ex[1][0]" => "3a",
178
- "ex[1][1]" => "4a",
179
- }
180
- end
181
- let(:expected_result) do
182
- {
183
- 'ex' => [
184
- ['1a', '2a'],
185
- ['3a', '4a']
186
- ]
187
- }
188
- end
189
-
190
- example do
191
- expect(result).to eq(expected_result)
192
- end
193
- end
194
-
195
- describe "3d arrays" do
196
- let(:input) do
197
- {
198
- 'ex[0][0][1]' => "1a",
199
- 'ex[0][1][0]' => "2a",
200
- 'ex[1][0][1]' => "3a",
201
- 'ex[1][1][0]' => "4a",
202
- }
203
- end
204
- let(:expected_result) do
205
- {
206
- 'ex' => [
207
- [
208
- [nil, '1a'],
209
- ['2a'],
210
- ],
211
- [
212
- [nil, '3a'],
213
- ['4a'],
55
+ 'two' => {
56
+ 'three' => '3a',
57
+ },
58
+ },
59
+ }
60
+ end
61
+
62
+ example do
63
+ expect(result).to eq(expected_result)
64
+ end
65
+ end
66
+
67
+ describe "array of values in hash" do
68
+ let(:input) do
69
+ {
70
+ "ex.one[0]" => "1a",
71
+ "ex.one[1]" => "2a",
72
+ }
73
+ end
74
+ let(:expected_result) do
75
+ {
76
+ 'ex' => { 'one' => ['1a', '2a'] }
77
+ }
78
+ end
79
+
80
+ example do
81
+ expect(result).to eq(expected_result)
82
+ end
83
+ end
84
+
85
+ describe "just array of values" do
86
+ let(:input) do
87
+ {
88
+ "ex[0]" => "1a",
89
+ "ex[1]" => "2a",
90
+ }
91
+ end
92
+ let(:expected_result) do
93
+ {
94
+ 'ex' => ['1a', '2a']
95
+ }
96
+ end
97
+
98
+ example do
99
+ expect(result).to eq(expected_result)
100
+ end
101
+ end
102
+
103
+ describe "array of hashes" do
104
+ let(:input) do
105
+ {
106
+ "ex[0].foo" => "1a",
107
+ "ex[0].bar" => "2a",
108
+ "ex[1].foo" => "3a",
109
+ }
110
+ end
111
+ let(:expected_result) do
112
+ {
113
+ 'ex' => [
114
+ { 'foo' => '1a', 'bar' => '2a' },
115
+ { 'foo' => '3a' },
214
116
  ]
215
- ]
216
- }
217
- end
117
+ }
118
+ end
119
+
120
+ example do
121
+ expect(result).to eq(expected_result)
122
+ end
123
+ end
124
+
125
+ describe "array of hash in hash" do
126
+ let(:key) { 'ex2' }
127
+ let(:input) do
128
+ {
129
+ "ex2.one[0].foo" => "1a",
130
+ "ex2.one[0].bar" => "2a",
131
+ "ex2.one[1].foo" => "3a",
132
+ }
133
+ end
134
+ let(:expected_result) do
135
+ {
136
+ 'ex2' => {
137
+ 'one' => [
138
+ {
139
+ "foo" => '1a',
140
+ "bar" => '2a',
141
+ },
142
+ {
143
+ "foo" => '3a',
144
+ }
145
+ ],
146
+ },
147
+ }
148
+ end
149
+
150
+ example do
151
+ expect(result).to eq(expected_result)
152
+ end
153
+ end
154
+
155
+ describe "empty hash value" do
156
+ let(:input) { { "ex" => "{}", } }
157
+ let(:expected_result) { { 'ex' => {} } }
158
+
159
+ example do
160
+ expect(result).to eq(expected_result)
161
+ end
162
+ end
163
+
164
+ describe "empty array value" do
165
+ let(:input) { { "ex" => "[]", } }
166
+ let(:expected_result) { { 'ex' => [] } }
167
+
168
+ example do
169
+ expect(result).to eq(expected_result)
170
+ end
171
+ end
172
+
173
+ describe "2d arrays" do
174
+ let(:input) do
175
+ {
176
+ "ex[0][0]" => "1a",
177
+ "ex[0][1]" => "2a",
178
+ "ex[1][0]" => "3a",
179
+ "ex[1][1]" => "4a",
180
+ }
181
+ end
182
+ let(:expected_result) do
183
+ {
184
+ 'ex' => [
185
+ ['1a', '2a'],
186
+ ['3a', '4a']
187
+ ]
188
+ }
189
+ end
190
+
191
+ example do
192
+ expect(result).to eq(expected_result)
193
+ end
194
+ end
195
+
196
+ describe "3d arrays" do
197
+ let(:input) do
198
+ {
199
+ 'ex[0][0][1]' => "1a",
200
+ 'ex[0][1][0]' => "2a",
201
+ 'ex[1][0][1]' => "3a",
202
+ 'ex[1][1][0]' => "4a",
203
+ }
204
+ end
205
+ let(:expected_result) do
206
+ {
207
+ 'ex' => [
208
+ [
209
+ [nil, '1a'],
210
+ ['2a'],
211
+ ],
212
+ [
213
+ [nil, '3a'],
214
+ ['4a'],
215
+ ]
216
+ ]
217
+ }
218
+ end
218
219
 
219
- example do
220
- expect(result).to eq(expected_result)
220
+ example do
221
+ expect(result).to eq(expected_result)
222
+ end
221
223
  end
222
224
  end
223
-
224
225
  end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe ConfigVolumizer do
4
+ describe '.generate' do
5
+ example do
6
+ data = {
7
+ "one" => "two",
8
+ "three" => {
9
+ "four" => "five",
10
+ "six" => [
11
+ "seven",
12
+ "eight",
13
+ ]
14
+ }
15
+ }
16
+ expected_data = {
17
+ "one" => "two",
18
+ "three.four" => "five",
19
+ "three.six[0]" => "seven",
20
+ "three.six[1]" => "eight",
21
+ }
22
+ expect(described_class.generate(data)).to eq(expected_data)
23
+ end
24
+ end
25
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_volumizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Banasik
@@ -28,9 +28,11 @@ files:
28
28
  - Rakefile
29
29
  - config_volumizer.gemspec
30
30
  - lib/config_volumizer.rb
31
+ - lib/config_volumizer/generator.rb
31
32
  - lib/config_volumizer/parser.rb
32
33
  - lib/config_volumizer/version.rb
33
34
  - spec/config_volumizer_spec.rb
35
+ - spec/generator_spec.rb
34
36
  - spec/spec_helper.rb
35
37
  homepage: https://rubygems.org/gems/config_volumizer
36
38
  licenses:
@@ -58,4 +60,5 @@ specification_version: 4
58
60
  summary: Adds volume to an otherwise flat config
59
61
  test_files:
60
62
  - spec/config_volumizer_spec.rb
63
+ - spec/generator_spec.rb
61
64
  - spec/spec_helper.rb