diff --git a/generators/lib/hash/hash.rb b/generators/lib/hash/hash.rb index aac71f8fbe609f404f7e6e6aa7f633cb7d9a7e95..c19ae8865da206c0456f6a507cd134138876e71f 100644 --- a/generators/lib/hash/hash.rb +++ b/generators/lib/hash/hash.rb @@ -33,13 +33,14 @@ class ::Hash # * If only a is omited, a == 1. # * If b is omited, only existing keys are modified (no keys are created). Otherwise, PREFIX-[a] to PREFIX-[b] entries are created (if missing). # Example: - # {"foo-1": {a: 0}, "foo-2": {a: 0}, "foo-3": {a: 0}, "foo-[2-]": {b: 1}}.expand_angle_brackets() + # {"foo-1": {a: 0}, "foo-2": {a: 0}, "foo-3": {a: 0}, "foo-[2-]": {b: 1}}.expand_square_brackets() # -> {"foo-1": {a: 0}, "foo-2": {a: 0, b:1}, "foo-3": {a: 0, b: 0}} - def expand_angle_brackets() + def expand_square_brackets() dup = self.clone # because can't add a new key into hash during iteration # Looking up for PREFIX-[a-b] keys dup.each { |key_ab, value_ab| + prefix, a, b = key_ab.to_s.scan(/^(.*)-\[(\d*)-(\d*)\]$/).first next if not a and not b # not found a != "" ? a = a.to_i : a = 1 @@ -76,7 +77,7 @@ class ::Hash # Do it recursivly self.each { |key, value| if value.is_a?(Hash) - value.expand_angle_brackets() + value.expand_square_brackets() end } end diff --git a/generators/lib/input_loader.rb b/generators/lib/input_loader.rb index 6dcfc0b0ae00ceae129152f08a7c16ca3aefd4d8..b81b0210eb73caef67ac3ecd5f0fdf8a49cadfda 100755 --- a/generators/lib/input_loader.rb +++ b/generators/lib/input_loader.rb @@ -30,7 +30,7 @@ def load_yaml_file_hierarchy(directory) # Expand the hash. Done at each iteration for enforcing priorities between duplicate entries: # ie. keys to be expanded have lowest priority on existing entries but higher priority on the entries found in the next files - global_hash.expand_angle_brackets() + global_hash.expand_square_brackets() } diff --git a/generators/tests/input_loader_tests.rb b/generators/tests/input_loader_tests.rb index 9109417b0879c117966d548a3b025c3f78818dc9..5e63d79b804fd5e391a54d3aa7c8cf92b1259b62 100755 --- a/generators/tests/input_loader_tests.rb +++ b/generators/tests/input_loader_tests.rb @@ -12,8 +12,8 @@ end # Helper test routine. Test if the hashes are equal after having been expanded. def assert_equal_expanded_hash(hash1, hash2) - expanded_hash1 = hash1.clone.expand_angle_brackets() - expanded_hash2 = hash2.clone.expand_angle_brackets() + expanded_hash1 = hash1.clone.expand_square_brackets() + expanded_hash2 = hash2.clone.expand_square_brackets() assert_equal_hash(expanded_hash1, expanded_hash2) end @@ -53,7 +53,7 @@ class TestInputLoader < Test::Unit::TestCase end # Test the example given in documentation - def test__expand_angle_brackets__doc_example + def test__expand_square_brackets__doc_example hash = { "foo-1": {a: 0}, "foo-2": {a: 0}, @@ -71,12 +71,12 @@ class TestInputLoader < Test::Unit::TestCase end # The 'a' parameter - def test__expand_angle_brackets__a_values + def test__expand_square_brackets__a_values assert_equal_expanded_hash({"foo-[-3]": 0}, {"foo-1": 0, "foo-2": 0, "foo-3": 0}) # Default 'a' value is 1 assert_equal_expanded_hash({"foo-[2-3]": 0}, {"foo-2": 0, "foo-3": 0}) # Simply check if the value of 'a' is taken into account end - def test__expand_angle_brackets__create_keys + def test__expand_square_brackets__create_keys # # If 'b' is given, create missing keys. Also, the keys must be of the same type (Symbol or String). # @@ -105,7 +105,7 @@ class TestInputLoader < Test::Unit::TestCase end - def test__expand_angle_brackets__keep_existing_keys + def test__expand_square_brackets__keep_existing_keys # # Do not modify existing keys # @@ -117,7 +117,7 @@ class TestInputLoader < Test::Unit::TestCase end # Some tests with nil values (=> nil values are created, existing nil values are overriden) - def test__expand_angle_brackets__nil + def test__expand_square_brackets__nil assert_equal_expanded_hash({"foo-[2-3]": nil}, {"foo-2": nil, "foo-3": nil}) # PREFIX-[a-b] is nil assert_equal_expanded_hash({"foo-[2-3]": 0, "foo-2": nil}, {"foo-2": 0, "foo-3": 0}) # PREFIX-x is nil assert_equal_expanded_hash({"foo-[2-]": nil, "foo-3": 0}, {"foo-3": 0}) # PREFIX-[a-] is nil