Class: RailsI18nterface::Yamlfile
- Inherits:
-
Object
- Object
- RailsI18nterface::Yamlfile
- Includes:
- Utils
- Defined in:
- lib/rails-i18nterface/yamlfile.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(root_dir, locale) ⇒ Yamlfile
constructor
A new instance of Yamlfile.
- #read ⇒ Object
- #write(hash) ⇒ Object
- #write_to_file ⇒ Object
Methods included from Utils
#contains_key?, #deep_merge!, #deep_sort, #deep_stringify_keys, #keys_to_yaml, #remove_blanks, #set_nested, #to_deep_hash, #to_shallow_hash
Constructor Details
#initialize(root_dir, locale) ⇒ Yamlfile
Returns a new instance of Yamlfile.
12 13 14 15 16 |
# File 'lib/rails-i18nterface/yamlfile.rb', line 12 def initialize(root_dir, locale) @root_dir = root_dir @locale = locale @file_path = File.join(@root_dir, 'config', 'locales', "#{locale}.yml") end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/rails-i18nterface/yamlfile.rb', line 10 def path @path end |
Instance Method Details
#read ⇒ Object
25 26 27 |
# File 'lib/rails-i18nterface/yamlfile.rb', line 25 def read File.exists?(@file_path) ? YAML.load(IO.read(@file_path)) : {} end |
#write(hash) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/rails-i18nterface/yamlfile.rb', line 18 def write(hash) FileUtils.mkdir_p File.dirname(@file_path) File.open(@file_path, 'w') do |file| file.puts keys_to_yaml(hash) end end |
#write_to_file ⇒ Object
29 30 31 32 |
# File 'lib/rails-i18nterface/yamlfile.rb', line 29 def write_to_file keys = { @locale => I18n.backend.send(:translations)[@locale] } write remove_blanks(keys) end |