Skip to content

Commit

Permalink
Sort keys: spec and refactor #92
Browse files Browse the repository at this point in the history
  • Loading branch information
glebm committed Sep 22, 2014
1 parent 1f31763 commit b3c5586
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/i18n/tasks/command/options/trees.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def print_forest(forest, opt, version = :show_tree)
when 'keys'
puts forest.key_names(root: true)
when *enum_opt(:data_format)
puts i18n.data.adapter_dump forest, format
puts i18n.data.adapter_dump forest.to_hash(true), format
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/i18n/tasks/data/file_formats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def load_file(path)
def write_tree(path, tree)
::FileUtils.mkpath(File.dirname path)
::File.open(path, 'w') { |f|
f.write adapter_dump(tree.to_hash, self.class.adapter_name_for_path(path))
f.write adapter_dump(tree.to_hash(true), self.class.adapter_name_for_path(path))
}
end

Expand Down
7 changes: 3 additions & 4 deletions lib/i18n/tasks/data/tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,9 @@ def to_siblings
parent && parent.children || Siblings.new(nodes: [self])
end

def to_hash
@hash ||= begin
children_hash = (children || {}).map(&:to_hash).reduce(:deep_merge) || {}
children_hash = Hash[children_hash.sort { |(k1, _v1), (k2, _v2)| k1 <=> k2 }]
def to_hash(sort = false)
(@hash ||= {})[sort] ||= begin
children_hash = children ? children.to_hash(sort) : {}
if key.nil?
children_hash
elsif leaf?
Expand Down
10 changes: 8 additions & 2 deletions lib/i18n/tasks/data/tree/nodes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ def derive(new_attr = {})
self.class.new(attr)
end

def to_hash
@hash ||= map(&:to_hash).reduce(:deep_merge!) || {}
def to_hash(sort = false)
(@hash ||= {})[sort] ||= begin
if sort
self.sort { |a, b| a.key <=> b.key }
else
self
end.map { |node| node.to_hash(sort) }.reduce({}, :deep_merge!)
end
end

delegate :to_json, to: :to_hash
Expand Down
6 changes: 5 additions & 1 deletion lib/i18n/tasks/data/tree/traversal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ def levels(&block)
nodes = to_nodes
unless nodes.empty?
block.yield nodes
Nodes.new(nodes.children).levels(&block)
if nodes.children.size == 1
first.children
else
Nodes.new(nodes: nodes.children)
end.levels(&block)
end
self
end
Expand Down
15 changes: 15 additions & 0 deletions spec/i18n_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@
end

describe 'normalize' do
it 'sorts the keys' do
in_test_app_dir do
run_cmd :normalize, pattern_router: true
en_yml_data = i18n_task.data.reload['en'].select_keys { |_k, node|
node.data[:path] == 'config/locales/en.yml'
}
expect(en_yml_data).to be_present
en_yml_data.nodes { |nodes|
next unless nodes.children
keys = nodes.children.map(&:key)
expect(keys).to eq keys.sort
}
end
end

it 'moves keys to the corresponding files as per data.write' do
in_test_app_dir {
expect(File).to_not exist 'config/locales/devise.en.yml'
Expand Down

0 comments on commit b3c5586

Please sign in to comment.