Skip to content

Commit

Permalink
new special cases for FlatHash.to_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHoste committed Sep 27, 2018
1 parent 2805092 commit e10c2d3
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/translation_io/flat_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def build_hash_with_flat(hash, key_string, value)
key_string = '[' + key_string
end

while key_string != ''
while key_string != '' && !current_object.is_a?(String) && !current_object.nil?
# Next is array
if key_string[0] == '['
array_pos = key_string.split(']', 2)[0]
Expand All @@ -54,7 +54,7 @@ def build_hash_with_flat(hash, key_string, value)
current_object[array_pos] = value if current_object.is_a?(Array)
end
# next is hash
elsif key_string[0] != '[' && (key_string.include?('.') or key_string.include?('['))
elsif key_string[0] != '[' && (key_string.include?('.') || key_string.include?('['))
splitted = key_string.split(/\.|\[/, 2)
new_key = splitted[0]
key_string = splitted[1]
Expand Down
85 changes: 81 additions & 4 deletions spec/translation/flat_hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,32 @@
}
end

it 'handles nil values' do
flat_hash = {
"key" => nil
}

hash = subject.to_hash(flat_hash)

hash.should == {
"key" => nil
}
end

it 'handles nil values with sublevel' do
flat_hash = {
"key.test" => nil
}

hash = subject.to_hash(flat_hash)

hash.should == {
"key" => {
"test" => nil
}
}
end

it 'handles joker square brackets in hash keys' do
flat_hash = {
'helpers.label.startup<@~<attachments_attributes>@~><@~<new_attachments>@~>.permissions' => 'Permissions',
Expand Down Expand Up @@ -485,7 +511,7 @@
subject.to_flat_hash(hash).should == flat_hash
end

it 'handle inconsistant values in hashs' do
it 'handles inconsistant values in hashs' do
flat_hash = {
"errors.messages.too_long" => "est trop long (pas plus de %{count} caractères)",
"errors.messages.too_long.one" => "est trop long (pas plus d'un caractère)",
Expand All @@ -503,7 +529,7 @@
}
end

it 'handle inconsistant values in hashs - 2' do
it 'handles inconsistant values in hashs - 2' do
flat_hash = {
"menus[0].a" => "Menu A",
"menus.b" => "Menu B",
Expand All @@ -524,7 +550,7 @@
}
end

it 'handle inconsistant values in hashs - 3' do
it 'handles inconsistant values in hashs - 3' do
flat_hash = {
"menus.a" => "Menu A",
"menus.a.test" => "test"
Expand All @@ -539,7 +565,7 @@
}
end

it 'handle inconsistant values in hashs - 4' do
it 'handles inconsistant values in hashs - 4' do
flat_hash = {
"title.edit" => "Modifier",
"title.new" => "Nouveau",
Expand All @@ -552,4 +578,55 @@
"title" => "",
}
end

it 'handles inconsistant values in hashs - 5' do
flat_hash = {
"services.renting.description" => 'Renting is great!',
"services.renting.description.price.header" => 'What is the price?',
}

hash = subject.to_hash(flat_hash)

hash.should == {
'services' => {
'renting' => {
'description' => "Renting is great!"
}
}
}
end

it "handles inconsistant values in hash - 6", :focus => true do
flat_hash = {
"services.renting.description" => 'Renting is great!',
"services.renting.description.price.header.test" => 'What is the price?',
}

hash = subject.to_hash(flat_hash)

hash.should == {
'services' => {
'renting' => {
'description' => "Renting is great!"
}
}
}
end

it "handles inconsistant values in hash - 7" do
flat_hash = {
"services.renting.description.price.header.test" => 'What is the price?',
"services.renting.description" => 'Renting is great!',
}

hash = subject.to_hash(flat_hash)

hash.should == {
'services' => {
'renting' => {
'description' => "Renting is great!"
}
}
}
end
end
26 changes: 24 additions & 2 deletions spec/translation/yaml_conversion_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
require 'spec_helper'

describe TranslationIO::YAMLConversion do
describe '#get_yaml_data_from_po_data' do
it 'returns correct YAML data' do
describe 'get_yaml_data_from_po_data with inconherent keys' do
it 'returns correct YAML data (1)' do
po_data = <<EOS
msgctxt "services.renting.description"
msgid "Renting is great!"
msgstr "Louer est super !"
msgctxt "services.renting.description.price.header"
msgid "What is the price?"
msgstr "Quel est le prix ?"
EOS

yaml_data = subject.get_yaml_data_from_po_data(po_data, :fr)

yaml_data.should == <<EOS
---
fr:
services:
renting:
description: Louer est super !
EOS
end

it 'returns correct YAML data (2)' do
po_data = <<EOS
msgctxt "hello"
msgid "Hello world"
Expand Down

0 comments on commit e10c2d3

Please sign in to comment.