From b3c55860a1c12b0f7102d3754bac5f3b2adc59bc Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Mon, 22 Sep 2014 19:55:24 +0200 Subject: [PATCH] Sort keys: spec and refactor #92 --- lib/i18n/tasks/command/options/trees.rb | 2 +- lib/i18n/tasks/data/file_formats.rb | 2 +- lib/i18n/tasks/data/tree/node.rb | 7 +++---- lib/i18n/tasks/data/tree/nodes.rb | 10 ++++++++-- lib/i18n/tasks/data/tree/traversal.rb | 6 +++++- spec/i18n_tasks_spec.rb | 15 +++++++++++++++ 6 files changed, 33 insertions(+), 9 deletions(-) diff --git a/lib/i18n/tasks/command/options/trees.rb b/lib/i18n/tasks/command/options/trees.rb index b2ff23cc..8cb1319e 100644 --- a/lib/i18n/tasks/command/options/trees.rb +++ b/lib/i18n/tasks/command/options/trees.rb @@ -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 diff --git a/lib/i18n/tasks/data/file_formats.rb b/lib/i18n/tasks/data/file_formats.rb index e3f5839c..34c0d536 100644 --- a/lib/i18n/tasks/data/file_formats.rb +++ b/lib/i18n/tasks/data/file_formats.rb @@ -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 diff --git a/lib/i18n/tasks/data/tree/node.rb b/lib/i18n/tasks/data/tree/node.rb index 85163d0c..6f22f549 100644 --- a/lib/i18n/tasks/data/tree/node.rb +++ b/lib/i18n/tasks/data/tree/node.rb @@ -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? diff --git a/lib/i18n/tasks/data/tree/nodes.rb b/lib/i18n/tasks/data/tree/nodes.rb index 0c664a39..3d6c89bb 100644 --- a/lib/i18n/tasks/data/tree/nodes.rb +++ b/lib/i18n/tasks/data/tree/nodes.rb @@ -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 diff --git a/lib/i18n/tasks/data/tree/traversal.rb b/lib/i18n/tasks/data/tree/traversal.rb index 3a752623..b0e5e17b 100644 --- a/lib/i18n/tasks/data/tree/traversal.rb +++ b/lib/i18n/tasks/data/tree/traversal.rb @@ -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 diff --git a/spec/i18n_tasks_spec.rb b/spec/i18n_tasks_spec.rb index 6612d0e6..8f620084 100644 --- a/spec/i18n_tasks_spec.rb +++ b/spec/i18n_tasks_spec.rb @@ -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'