Sha256: 849be0a77ce10171513747c9a15e4f8ade4e9f19e8ad2b6bdc103aed6d58a8b7

Contents?: true

Size: 880 Bytes

Versions: 28

Compression:

Stored size: 880 Bytes

Contents

require 'fileutils'

module Translate
  class 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
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
refinerycms-i18n-5.0.1 lib/translate/file.rb
refinerycms-i18n-5.0.0 lib/translate/file.rb
refinerycms-i18n-4.0.2 lib/translate/file.rb
refinerycms-i18n-4.0.1 lib/translate/file.rb
refinerycms-i18n-3.0.2 lib/translate/file.rb
refinerycms-i18n-4.0.0 lib/translate/file.rb
refinerycms-i18n-3.0.1 lib/translate/file.rb
refinerycms-i18n-3.0.0 lib/translate/file.rb
refinerycms-i18n-2.1.0 lib/translate/file.rb
refinerycms-i18n-2.0.2 lib/translate/file.rb
refinerycms-i18n-2.0.0 lib/translate/file.rb
refinerycms-i18n-1.0.0 lib/translate/file.rb
refinerycms-i18n-0.9.9.16 lib/translate/file.rb
refinerycms-i18n-0.9.9.15 lib/translate/file.rb
refinerycms-i18n-0.9.9.14 lib/translate/file.rb
refinerycms-i18n-0.9.9.13 lib/translate/file.rb
refinerycms-i18n-0.9.9.12 lib/translate/file.rb
refinerycms-i18n-0.9.9.10 lib/translate/file.rb
refinerycms-i18n-0.9.9.9 lib/translate/file.rb
refinerycms-i18n-0.9.9.8 lib/translate/file.rb