nugrant 2.1.1 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 36479d2a076b5dfd5f938969edcdc48f12ea9501
4
- data.tar.gz: 162d0feefe4e742beeef2561187335ef313d79cd
3
+ metadata.gz: 59d03f3a6d570c597abc17133e4d68782e455cc0
4
+ data.tar.gz: f0d3558fa09f660559cbdd283d84b2b845a16d69
5
5
  SHA512:
6
- metadata.gz: 0b5d3e769d89e6f527b39aa90696c19c1425a4058c08378dc89f6da33927599d1ce2566b883a012c185c6559b99575abe64863b2b9cf339752cbf72272284841
7
- data.tar.gz: 96f6cb2405b5ea6a8a501662f3c36e78d6d9285fbf4b28ba834fff8fa9e7f22ce878cf96c5b4d194fd565ccd6d00c7583c4e97b482409502c4c84c4f4df54b3e
6
+ metadata.gz: 7f85374f9ff849c110f185558d2594355c5636037472ec278697b544aa5ee0ed1c7c0d869cb43f42d6d78895388444407fe2037e1e7c4d33586deb545c132ccb
7
+ data.tar.gz: 22761919a1b509bef418fd2993641d7f2b7d43ae9807bd58bd2d278af43a632665812a28236717c3f7c53bbcfbc25cc1514380d73bb0ba7ab6d67f415d4d9db0
@@ -1,9 +1,17 @@
1
+ # 2.1.2 (January 12th, 2015)
2
+
3
+ * Fixed indifferent access inside arrays. Array elements of type `Hash`
4
+ are now converted to `Bag` instances (recursively). This fix the
5
+ indifferent access of `Bag` elements inside arrays.
6
+
7
+ Fixes [issue #27](https://github.com/maoueh/nugrant/issues/27).
8
+
1
9
  # 2.1.1 (December 2nd, 2014)
2
10
 
3
11
  * Permit numeric keys in bag. They are converted to symbol
4
12
  like others.
5
13
 
6
- Fixes [issue #26](https://github.com/maoueh/nugrant/issues/26) again.
14
+ Fixes [issue #26](https://github.com/maoueh/nugrant/issues/26).
7
15
 
8
16
  * Removed old code that was switching YAML engine to `syck` when
9
17
  it was available.
@@ -142,7 +142,7 @@ module Nugrant
142
142
 
143
143
  Hash[map do |key, value|
144
144
  key = use_string_key ? key.to_s() : key
145
- value = value.kind_of?(Bag) ? value.to_hash(options) : value
145
+ value = __convert_value_to_hash(value, options)
146
146
 
147
147
  [key, value]
148
148
  end]
@@ -178,11 +178,55 @@ module Nugrant
178
178
  # ensures at the same time a deeply preventive copy since a new
179
179
  # instance is created for this nested structure.
180
180
  #
181
+ # In addition, it also transforms array elements of type Hash into
182
+ # Bag instance also. This enable indifferent access inside arrays
183
+ # also.
184
+ #
181
185
  # @param value The value to convert to bag if necessary
182
186
  # @return The converted value
183
187
  #
184
188
  def __convert_value(value)
185
- value.kind_of?(Hash) ? Bag.new(value, @__config) : value
189
+ case
190
+ # Converts Hash to Bag instance
191
+ when value.kind_of?(Hash)
192
+ Bag.new(value, @__config)
193
+
194
+ # Converts Array elements to Bag instances if needed
195
+ when value.kind_of?(Array)
196
+ value.map(&self.method(:__convert_value))
197
+
198
+ # Keeps as-is other elements
199
+ else
200
+ value
201
+ end
202
+ end
203
+
204
+ ##
205
+ # This function does the reversion of value conversion to Bag
206
+ # instances. It transforms Bag instances to Hash (using to_hash)
207
+ # and array Bag elements to Hash (using to_hash).
208
+ #
209
+ # The options parameters are pass to the to_hash function
210
+ # when invoked.
211
+ #
212
+ # @param value The value to convert to hash
213
+ # @param options The options passed to the to_hash function
214
+ # @return The converted value
215
+ #
216
+ def __convert_value_to_hash(value, options = {})
217
+ case
218
+ # Converts Bag instance to Hash
219
+ when value.kind_of?(Bag)
220
+ value.to_hash(options)
221
+
222
+ # Converts Array elements to Hash instances if needed
223
+ when value.kind_of?(Array)
224
+ value.map { |value| __convert_value_to_hash(value, options) }
225
+
226
+ # Keeps as-is other elements
227
+ else
228
+ value
229
+ end
186
230
  end
187
231
 
188
232
  def __get(key)
@@ -1,3 +1,3 @@
1
1
  module Nugrant
2
- VERSION = "2.1.1"
2
+ VERSION = "2.1.2"
3
3
  end
@@ -5,39 +5,10 @@ require 'nugrant/helper/bag'
5
5
 
6
6
  module Nugrant
7
7
  class TestBag < ::Minitest::Test
8
- def create_bag(elements, options = {})
9
- return Bag.new(elements, options)
10
- end
11
-
12
- def assert_all_access_equal(expected, bag, key)
13
- assert_equal(expected, bag.method_missing(key.to_sym), "bag.#{key.to_sym}")
14
- assert_equal(expected, bag[key.to_s], "bag[#{key.to_s}]")
15
- assert_equal(expected, bag[key.to_sym], "bag[#{key.to_sym}]")
16
- end
17
-
18
- def assert_all_access_bag(expected, bag, key)
19
- assert_bag(expected, bag.method_missing(key.to_sym))
20
- assert_bag(expected, bag[key.to_s])
21
- assert_bag(expected, bag[key.to_sym])
22
- end
23
-
24
- def assert_bag(expected, bag)
25
- assert_kind_of(Bag, bag)
26
-
27
- expected.each do |key, expected_value|
28
- if not expected_value.kind_of?(Hash)
29
- assert_all_access_equal(expected_value, bag, key)
30
- next
31
- end
32
-
33
- assert_all_access_bag(expected_value, bag, key)
34
- end
35
- end
36
-
37
8
  def run_test_bag(parameters)
38
9
  bag = create_bag(parameters)
39
10
 
40
- assert_bag(parameters, bag)
11
+ assert_bag_equal(parameters, bag)
41
12
  end
42
13
 
43
14
  def test_bag()
@@ -101,18 +72,46 @@ module Nugrant
101
72
  def test_to_hash()
102
73
  hash = create_bag({}).to_hash()
103
74
 
104
- assert_kind_of(Hash, hash)
75
+ assert_is_hash_exclusive(hash)
105
76
  assert_equal({}, hash)
106
77
 
107
78
  hash = create_bag({"value" => {:one => "value", "two" => "value"}}).to_hash()
108
79
 
109
- assert_kind_of(Hash, hash)
110
- assert_kind_of(Hash, hash[:value])
80
+ assert_is_hash_exclusive(hash)
81
+ assert_is_hash_exclusive(hash[:value])
111
82
  assert_kind_of(String, hash[:value][:one])
112
83
  assert_kind_of(String, hash[:value][:two])
113
84
  assert_equal({:value => {:one => "value", :two => "value"}}, hash)
114
85
  end
115
86
 
87
+ def test_to_hash_array_with_bag()
88
+ hash = create_bag({:a => [{:b => "1", :c => [{"d" => "2"}]}]}).to_hash()
89
+
90
+ assert_is_hash_exclusive(hash)
91
+ assert_kind_of(Array, hash[:a])
92
+ assert_is_hash_exclusive(hash[:a][0])
93
+ assert_kind_of(String, hash[:a][0][:b])
94
+ assert_kind_of(Array, hash[:a][0][:c])
95
+ assert_is_hash_exclusive(hash[:a][0][:c][0])
96
+ assert_kind_of(String, hash[:a][0][:c][0][:d])
97
+
98
+ assert_equal({:a => [{:b => "1", :c => [{:d => "2"}]}]}, hash)
99
+ end
100
+
101
+ def test_to_hash_array_with_bag_string_key()
102
+ hash = create_bag({:a => [{:b => "1", :c => [{"d" => "2"}]}]}).to_hash(use_string_key: true)
103
+
104
+ assert_is_hash_exclusive(hash)
105
+ assert_kind_of(Array, hash["a"])
106
+ assert_is_hash_exclusive(hash["a"][0])
107
+ assert_kind_of(String, hash["a"][0]["b"])
108
+ assert_kind_of(Array, hash["a"][0]["c"])
109
+ assert_is_hash_exclusive(hash["a"][0]["c"][0])
110
+ assert_kind_of(String, hash["a"][0]["c"][0]["d"])
111
+
112
+ assert_equal({"a" => [{"b" => "1", "c" => [{"d" => "2"}]}]}, hash)
113
+ end
114
+
116
115
  def test_merge_array_replace()
117
116
  # Replace should be the default case
118
117
  bag1 = create_bag({"first" => [1, 2]})
@@ -168,16 +167,16 @@ module Nugrant
168
167
 
169
168
  assert_equal({:first => {:second => [3, 4]}, :third => "three"}, bag1.to_hash())
170
169
 
171
- assert_all_access_equal({:second => [3, 4]}, bag1, :first)
172
- assert_all_access_equal([3, 4], bag1["first"], :second)
170
+ assert_indifferent_access_equal({:second => [3, 4]}, bag1, :first)
171
+ assert_indifferent_access_equal([3, 4], bag1["first"], :second)
173
172
  end
174
173
 
175
174
  def test_set_a_slot_with_a_hash_keeps_indifferent_access
176
175
  bag1 = create_bag({})
177
176
  bag1["first"] = {:second => [1, 2]}
178
177
 
179
- assert_all_access_equal({:second => [1, 2]}, bag1, :first)
180
- assert_all_access_equal([1, 2], bag1["first"], :second)
178
+ assert_indifferent_access_equal({:second => [1, 2]}, bag1, :first)
179
+ assert_indifferent_access_equal([1, 2], bag1["first"], :second)
181
180
  end
182
181
 
183
182
  def test_nil_key()
@@ -217,7 +216,7 @@ module Nugrant
217
216
  def test_numeric_keys_converted_to_string
218
217
  bag1 = create_bag({1 => "value1"})
219
218
 
220
- assert_all_access_equal("value1", bag1, :'1')
219
+ assert_indifferent_access_equal("value1", bag1, :'1')
221
220
  end
222
221
 
223
222
  def test_custom_key_error_handler
@@ -243,7 +242,7 @@ module Nugrant
243
242
  "Some value"
244
243
  end)
245
244
 
246
- assert_all_access_equal("Some value", bag, :invalid_value)
245
+ assert_indifferent_access_equal("Some value", bag, :invalid_value)
247
246
  end
248
247
 
249
248
  def test_walk_bag
@@ -304,11 +303,11 @@ module Nugrant
304
303
 
305
304
  assert_equal(Nugrant::Bag, bag3.class)
306
305
 
307
- assert_all_access_equal("value1", bag3['first'], :level1)
308
- assert_all_access_equal("overriden2", bag3['first'], :level2)
309
- assert_all_access_equal("value3", bag3['second'], :level1)
310
- assert_all_access_equal("value4", bag3['second'], :level2)
311
- assert_all_access_equal("overriden5", bag3, :third)
306
+ assert_indifferent_access_equal("value1", bag3['first'], :level1)
307
+ assert_indifferent_access_equal("overriden2", bag3['first'], :level2)
308
+ assert_indifferent_access_equal("value3", bag3['second'], :level1)
309
+ assert_indifferent_access_equal("value4", bag3['second'], :level2)
310
+ assert_indifferent_access_equal("overriden5", bag3, :third)
312
311
  end
313
312
 
314
313
  def test_merge!
@@ -342,11 +341,11 @@ module Nugrant
342
341
 
343
342
  assert_equal(Nugrant::Bag, bag3.class)
344
343
 
345
- assert_all_access_equal("value1", bag3['first'], :level1)
346
- assert_all_access_equal("overriden2", bag3['first'], :level2)
347
- assert_all_access_equal("value3", bag3['second'], :level1)
348
- assert_all_access_equal("value4", bag3['second'], :level2)
349
- assert_all_access_equal("overriden5", bag3, :third)
344
+ assert_indifferent_access_equal("value1", bag3['first'], :level1)
345
+ assert_indifferent_access_equal("overriden2", bag3['first'], :level2)
346
+ assert_indifferent_access_equal("value3", bag3['second'], :level1)
347
+ assert_indifferent_access_equal("value4", bag3['second'], :level2)
348
+ assert_indifferent_access_equal("overriden5", bag3, :third)
350
349
  end
351
350
 
352
351
  def test_merge_hash()
@@ -389,5 +388,63 @@ module Nugrant
389
388
 
390
389
  assert_equal([1, 2, 3, 1, 2, 3], bag3['first'])
391
390
  end
391
+
392
+ def test_indifferent_access_in_array
393
+ bag = create_bag({:a => [{:b => 1, :c => [{"d" => 1}]}]})
394
+
395
+ assert_indifferent_access_equal(1, bag['a'][0]['c'][0], :d)
396
+ end
397
+
398
+ ## Helpers & Assertions
399
+
400
+ def create_bag(elements, options = {})
401
+ return Bag.new(elements, options)
402
+ end
403
+
404
+ def assert_is_hash_exclusive(input)
405
+ assert_equal(true, input.kind_of?(Hash), "#{input} should be a kind of Hash")
406
+ assert_equal(false, input.kind_of?(Bag), "#{input} should not be a kind of Bag")
407
+ end
408
+
409
+ def assert_indifferent_access_equal(expected, bag, key)
410
+ assert_symbol_access_equal(expected, bag, key)
411
+ assert_string_access_equal(expected, bag, key)
412
+ end
413
+
414
+ def assert_string_access_equal(expected, bag, key)
415
+ assert_equal(expected, bag[key.to_s], "bag[#{key.to_s}]")
416
+ end
417
+
418
+ def assert_symbol_access_equal(expected, bag, key)
419
+ assert_equal(expected, bag.method_missing(key.to_sym), "bag.#{key.to_sym}")
420
+ assert_equal(expected, bag[key.to_sym], "bag[#{key.to_sym}]")
421
+ end
422
+
423
+ def assert_indifferent_access_bag_equal(expected, bag, key)
424
+ assert_string_access_bag_equal(expected, bag, key)
425
+ assert_symbol_access_bag_equal(expected, bag, key)
426
+ end
427
+
428
+ def assert_string_access_bag_equal(expected, bag, key)
429
+ assert_bag_equal(expected, bag[key.to_s])
430
+ end
431
+
432
+ def assert_symbol_access_bag_equal(expected, bag, key)
433
+ assert_bag_equal(expected, bag.method_missing(key.to_sym))
434
+ assert_bag_equal(expected, bag[key.to_sym])
435
+ end
436
+
437
+ def assert_bag_equal(expected, bag)
438
+ assert_kind_of(Bag, bag)
439
+
440
+ expected.each do |key, expected_value|
441
+ if not expected_value.kind_of?(Hash)
442
+ assert_indifferent_access_equal(expected_value, bag, key)
443
+ next
444
+ end
445
+
446
+ assert_indifferent_access_bag_equal(expected_value, bag, key)
447
+ end
448
+ end
392
449
  end
393
450
  end
@@ -10,51 +10,6 @@ module Nugrant
10
10
  @@FORMATS = [:json, :yaml]
11
11
  @@INVALID_PATH = "impossible_file_path.yamljson.impossible"
12
12
 
13
- def create_parameters(format, current_filename, user_filename, system_filename, defaults = {}, options = {})
14
- extension = case format
15
- when :json
16
- "json"
17
- when :yml, :yaml
18
- "yml"
19
- else
20
- raise ArgumentError, "Format [#{format}] is currently not supported"
21
- end
22
-
23
- resource_path = File.expand_path("#{File.dirname(__FILE__)}/../../resources/#{format}")
24
-
25
- current_path = "#{resource_path}/#{current_filename}.#{extension}" if current_filename
26
- user_path = "#{resource_path}/#{user_filename}.#{extension}" if user_filename
27
- system_path = "#{resource_path}/#{system_filename}.#{extension}" if system_filename
28
-
29
- return Nugrant::Parameters.new(defaults, {
30
- :format => format,
31
- :current_path => current_path,
32
- :user_path => user_path,
33
- :system_path => system_path,
34
- :array_merge_strategy => options[:array_merge_strategy]
35
- })
36
- end
37
-
38
- def assert_all_access_equal(expected, parameters, key)
39
- assert_equal(expected, parameters.method_missing(key.to_sym), "parameters.#{key.to_s}")
40
- assert_equal(expected, parameters[key.to_s], "parameters[#{key.to_s}]")
41
- assert_equal(expected, parameters[key.to_sym], "parameters[#{key.to_sym}]")
42
- end
43
-
44
- def assert_level(parameters, results)
45
- results.each do |key, value|
46
- assert_all_access_equal(value, parameters, key)
47
- end
48
-
49
- assert_key_error(parameters, "0.0.0")
50
- end
51
-
52
- def assert_key_error(parameters, key)
53
- assert_raises(KeyError) do
54
- parameters[key]
55
- end
56
- end
57
-
58
13
  def test_params_level_1()
59
14
  formats.each do |format|
60
15
  parameters = create_parameters(format, "params_current_1", "params_user_1", "params_system_1")
@@ -425,6 +380,53 @@ module Nugrant
425
380
  assert_equal("value1", parameters.servers[:'1'])
426
381
  end
427
382
 
383
+ ## Helpers & Assertions
384
+
385
+ def create_parameters(format, current_filename, user_filename, system_filename, defaults = {}, options = {})
386
+ extension = case format
387
+ when :json
388
+ "json"
389
+ when :yml, :yaml
390
+ "yml"
391
+ else
392
+ raise ArgumentError, "Format [#{format}] is currently not supported"
393
+ end
394
+
395
+ resource_path = File.expand_path("#{File.dirname(__FILE__)}/../../resources/#{format}")
396
+
397
+ current_path = "#{resource_path}/#{current_filename}.#{extension}" if current_filename
398
+ user_path = "#{resource_path}/#{user_filename}.#{extension}" if user_filename
399
+ system_path = "#{resource_path}/#{system_filename}.#{extension}" if system_filename
400
+
401
+ return Nugrant::Parameters.new(defaults, {
402
+ :format => format,
403
+ :current_path => current_path,
404
+ :user_path => user_path,
405
+ :system_path => system_path,
406
+ :array_merge_strategy => options[:array_merge_strategy]
407
+ })
408
+ end
409
+
410
+ def assert_all_access_equal(expected, parameters, key)
411
+ assert_equal(expected, parameters.method_missing(key.to_sym), "parameters.#{key.to_s}")
412
+ assert_equal(expected, parameters[key.to_s], "parameters[#{key.to_s}]")
413
+ assert_equal(expected, parameters[key.to_sym], "parameters[#{key.to_sym}]")
414
+ end
415
+
416
+ def assert_level(parameters, results)
417
+ results.each do |key, value|
418
+ assert_all_access_equal(value, parameters, key)
419
+ end
420
+
421
+ assert_key_error(parameters, "0.0.0")
422
+ end
423
+
424
+ def assert_key_error(parameters, key)
425
+ assert_raises(KeyError) do
426
+ parameters[key]
427
+ end
428
+ end
429
+
428
430
  def formats()
429
431
  @@FORMATS
430
432
  end
@@ -0,0 +1,15 @@
1
+ Vagrant.configure("2") do |config|
2
+ config.user.defaults = {
3
+ nodes: [
4
+ {ip: '192.168.100.10'},
5
+ {ip: '192.168.100.11'}
6
+ ]
7
+ }
8
+
9
+ config.user.nodes.each_with_index do |node, index|
10
+ puts "Node ##{index}, string: #{node['ip']}"
11
+ puts "Node ##{index}, symbol: #{node[:ip]}"
12
+ puts "Node ##{index}, method: #{node.ip}"
13
+ puts ""
14
+ end
15
+ end
@@ -2,14 +2,28 @@ Vagrant.configure("2") do |config|
2
2
  config.user.defaults = {
3
3
  'local' => {
4
4
  'first' => "value1",
5
- 'second' => "value2"
5
+ },
6
+ 'array' => {
7
+ 'level1' => [1, 2]
8
+ },
9
+ 'application' => {
10
+ 'users' => {
11
+ "joe" => {
12
+ :full => "Joe Plumber"
13
+ },
14
+ "jane" => {
15
+ :full => "Jane Cook"
16
+ },
17
+ }
6
18
  }
7
19
  }
8
20
 
9
- config.user.local.each do |name, value|
10
- puts "Parameters #{name}: #{value}"
21
+ config.user.application.users.each do |key, data|
22
+ puts "Key #{key}: #{data}"
11
23
  end
12
24
 
13
- puts "User has local (symbol)" if config.user.has?(:local)
14
- puts "User has local (string)" if config.user.has?("local")
25
+ config.user.array_merge_strategy = :concat
26
+ config.user.auto_export = [:autoenv, :script]
27
+
28
+ config.vm.box = "opscode_centos-7.0_x86-64"
15
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nugrant
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthieu Vachon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-03 00:00:00.000000000 Z
11
+ date: 2015-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
@@ -104,6 +104,7 @@ files:
104
104
  - test/resources/json/params_user_nil_values.json
105
105
  - test/resources/json/params_windows_eol.json
106
106
  - test/resources/vagrantfiles/v2.auto_export
107
+ - test/resources/vagrantfiles/v2.bag_inside_array
107
108
  - test/resources/vagrantfiles/v2.defaults_mixed_string_symbols
108
109
  - test/resources/vagrantfiles/v2.defaults_null_values_in_vagrantuser
109
110
  - test/resources/vagrantfiles/v2.defaults_using_string