class Translate::File

Attributes

path[RW]

Public Class Methods

deep_stringify_keys(hash) click to toggle source

Stringifying keys for prettier YAML

# File lib/translate/file.rb, line 23
def self.deep_stringify_keys(hash)
  hash.inject({}) { |result, (key, value)|
    value = deep_stringify_keys(value) if value.is_a? Hash
    result[(key.to_s rescue key) || key] = value
    result
  }
end
new(path) click to toggle source
# File lib/translate/file.rb, line 7
def initialize(path)
  self.path = path
end

Public Instance Methods

read() click to toggle source
# File lib/translate/file.rb, line 18
def read
  File.exists?(path) ? YAML::load(IO.read(path)) : {}
end
write(keys) click to toggle source
# File lib/translate/file.rb, line 11
def write(keys)
  FileUtils.mkdir_p File.dirname(path)
  File.open(path, "w") do |file|
    file.puts keys_to_yaml(Translate::File.deep_stringify_keys(keys))
  end
end

Private Instance Methods

keys_to_yaml(keys) click to toggle source
# File lib/translate/file.rb, line 32
def keys_to_yaml(keys)
  # Using ya2yaml, if available, for UTF8 support
  keys.respond_to?(:ya2yaml) ? keys.ya2yaml(:escape_as_utf8 => true) : keys.to_yaml
end