-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #263
- Loading branch information
Showing
4 changed files
with
36 additions
and
11 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
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 |
---|---|---|
|
@@ -44,15 +44,22 @@ def get(locale) | |
alias [] get | ||
|
||
# set locale tree | ||
# @param [String, Symbol] locale | ||
# @param [::I18n::Tasks::Data::Siblings] tree | ||
def set(locale, tree) | ||
locale = locale.to_s | ||
router.route locale, tree do |path, tree_slice| | ||
write_tree path, tree_slice | ||
removing_emptied_files locale, tree do |&block| | ||
router.route locale, tree do |path, tree_slice| | ||
tree_slice.key_names(root: true).each { |key| block.call(key, path) } | ||
This comment has been minimized.
Sorry, something went wrong.
vsakarov
|
||
write_tree path, tree_slice | ||
end | ||
end | ||
@trees.delete(locale) if @trees | ||
@available_locales = nil | ||
end | ||
|
||
alias []= set | ||
|
||
# @param [String] locale | ||
# @return [Array<String>] paths to files that are not normalized | ||
def non_normalized_paths(locale) | ||
|
@@ -62,7 +69,7 @@ def non_normalized_paths(locale) | |
end | ||
|
||
def write(forest) | ||
forest.each { |root| set(root.key, root) } | ||
forest.each { |root| set(root.key, root.to_siblings) } | ||
end | ||
|
||
def merge!(forest) | ||
|
@@ -80,15 +87,10 @@ def remove_by_key!(forest) | |
locale_data = get(locale) | ||
subtracted = locale_data.subtract_by_key(forest) | ||
set locale, subtracted | ||
(tree_paths(locale, forest) - tree_paths(locale, subtracted)).each do |path| | ||
FileUtils.remove_file(path) if File.exist?(path) | ||
end | ||
removed.merge! locale_data.subtract_by_key(subtracted) | ||
end | ||
end | ||
|
||
alias []= set | ||
|
||
# @return self | ||
def reload | ||
@trees = nil | ||
|
@@ -181,8 +183,23 @@ def filter_nil_keys!(path, data, suffix = []) | |
end | ||
end | ||
|
||
def removing_emptied_files(locale, tree, &block) | ||
@trees.delete(locale) if @trees | ||
paths_before = tree_paths(locale, get(locale)) | ||
new_paths = {} | ||
block.call do |key, path| # rubocop:disable Performance/RedundantBlockCall | ||
new_paths[key] = path | ||
end | ||
tree.keys(root: true) do |key, node| | ||
node.data[:path] = new_paths[key] | ||
end | ||
(paths_before - tree_paths(locale, tree)).each do |path| | ||
FileUtils.remove_file(path) if File.exist?(path) | ||
end | ||
end | ||
|
||
def tree_paths(locale, forest) | ||
Set.new(router.route(locale, forest).map { |path, _| path }) | ||
Set.new(forest[locale].leaves.map { |node| node.data[:path] }) | ||
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
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
Typo