Skip to content

Instantly share code, notes, and snippets.

@kryzhovnik
Created February 2, 2011 01:39
Show Gist options
  • Save kryzhovnik/807093 to your computer and use it in GitHub Desktop.
Save kryzhovnik/807093 to your computer and use it in GitHub Desktop.
требует установленного гема ya2yaml (gem install ya2yaml)
en:
name:
first: Andrey
last: Samsonov
email: '[email protected]'
github: 'https://github.com/kryzhovnik'
phone: '+7(927)695-8-444'
skype: 'andrey.samsonov'
country: Russia
city: Samara
birthday:
day: 16
month: apr
#!/usr/bin/env ruby
require 'yaml'
require 'ya2yaml'
class Hash
def deep_merge(other_hash)
dup.deep_merge!(other_hash)
end
def deep_merge!(other_hash)
other_hash.each_pair do |k,v|
tv = self[k]
self[k] = if v.is_a?(Hash)
tv.is_a?(Hash) ? tv.deep_merge(v) : self[k] = v
else
tv.nil? ? v : self[k]
end
end
self
end
end
if ARGV.size == 2 or ARGV.size == 3
ARGV[2] ||= ARGV[1]
complete_path, incomplete_path, result_path = ARGV[0..2]
complete_hash = YAML.load(File.open(complete_path))
incomplete_hash = YAML.load(File.open(incomplete_path))
# {:ru => { ... }}.first[1] == { ... } => true
incomplete_hash.first[1].deep_merge!(complete_hash.first[1])
File.open(result_path, 'w'){|f| f << incomplete_hash.ya2yaml}
else
puts <<END
Usage: lacales_merger COMPLETE_LOCALE_PATH INCOMPLETE_LOCALE_PATH [RESULT_PATH]
if RESULT_PATH is missing, results writes to INCOMPLETE_LOCALE_PATH file
Example:
./locales_merger.rb fixtures/en.yml fixtures/ru.yml fixtures/result.yml
END
end
ru:
name: Андрей Самсонов
city: Самара
country: Россия
birthday:
year: 1986
month: 'апрель'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment