Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
217 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Helpers | ||
|
||
def self.normalize_yaml(yaml) | ||
return '' if yaml.nil? | ||
return yaml if yaml.is_a? String | ||
return yaml.to_s if yaml.is_a? Numeric | ||
return yaml.to_s if !!yaml == yaml # if boolean | ||
return ":#{yaml.to_s}" if yaml.is_a? Symbol | ||
yaml = array_to_hash(yaml) if yaml.is_a? Array | ||
|
||
normalized = {} | ||
yaml.each do |key, value| | ||
normalized[key] = normalize_yaml(value) | ||
end | ||
normalized | ||
end | ||
|
||
def self.array_to_hash(array) | ||
hash = {} | ||
array.each_with_index { |val, i| hash[i.to_s] = val } | ||
hash | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
en: | ||
key1: 'value1' | ||
parent1: | ||
subkey1: 'value1' | ||
key2: 'value2' | ||
parent2: | ||
key3: 'value3' | ||
parent3: | ||
- one | ||
- :two | ||
- three | ||
- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
he: | ||
key1: 'value1' | ||
parent1: | ||
subkey1: 'value1' | ||
parent3: | ||
- 1 | ||
- heb two |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require_relative '../lib/helpers' | ||
|
||
describe Helpers do | ||
describe "#normalize_yaml" do | ||
it "converts symbols to strings (with ':' prefix)" do | ||
yaml = { 'key3' => :bob } | ||
|
||
Helpers.normalize_yaml(yaml).should == { | ||
'key3' => ':bob' | ||
} | ||
end | ||
|
||
it "converts nil to ''" do | ||
yaml = { 'key3' => nil } | ||
Helpers.normalize_yaml(yaml).should == { | ||
'key3' => '' | ||
} | ||
end | ||
|
||
it "converts numbers to string" do | ||
yaml = { 'key3' => 123 } | ||
Helpers.normalize_yaml(yaml).should == { | ||
'key3' => '123' | ||
} | ||
end | ||
|
||
it "converts booleans to string" do | ||
yaml = { 'key3' => true } | ||
Helpers.normalize_yaml(yaml).should == { | ||
'key3' => 'true' | ||
} | ||
end | ||
|
||
it "converts arrays to hashes" do | ||
yaml = { 'key1' => 'value1', | ||
'key2' => [ 'value2', nil, :bla ], | ||
'parent1' => { 'key3' => [ :bob ] } } | ||
|
||
Helpers.normalize_yaml(yaml).should == { | ||
'key1' => 'value1', | ||
'key2' => { '0' => 'value2', '1' => '', '2' => ':bla' }, | ||
'parent1' => { 'key3' => { '0' => ':bob' } } | ||
} | ||
end | ||
end | ||
|
||
describe "#array_to_hash" do | ||
it "returns hash with numeric indexes" do | ||
Helpers.array_to_hash(['a','b']).should == { '0' => 'a', '1' => 'b' } | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters