Sha256: 414c1253552e8dfa8645a27faf3527445c1d4835b0340b42f1b725025ad55b71

Contents?: true

Size: 866 Bytes

Versions: 9

Compression:

Stored size: 866 Bytes

Contents

require 'fileutils'

class Translate::File
  attr_accessor :path
  
  def initialize(path)
    self.path = path
  end
  
  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
  
  def read
    File.exists?(path) ? YAML::load(IO.read(path)) : {}
  end

  # Stringifying keys for prettier YAML
  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
  
  private
  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    
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
translate-rails3-plus-0.0.11 lib/translate/file.rb
translate-rails3-plus-0.0.9 lib/translate/file.rb
translate-rails3-plus-0.0.8 lib/translate/file.rb
translate-rails3-plus-0.0.7 lib/translate/file.rb
translate-rails3-plus-0.0.6 lib/translate/file.rb
translate-rails3-plus-0.0.5 lib/translate/file.rb
translate-rails3-plus-0.0.4 lib/translate/file.rb
translate-rails3-plus-0.0.3 lib/translate/file.rb
translate-rails3-plus-0.0.1 lib/translate/file.rb