Class: RailsI18nterface::Yamlfile

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rails-i18nterface/yamlfile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#pathObject (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

#readObject



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_fileObject



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