Sha256: a559908badf3604f9565b7743fec4be1953bb00ac9bf810e9eddbe2e318f64a5

Contents?: true

Size: 878 Bytes

Versions: 6

Compression:

Stored size: 878 Bytes

Contents

require 'yaml'
module MissingTranslation
  class YamlProcessor
    def self.denormalize_translations_hash(translations, parent_key='')
      ans = {}
      translations.each do |key, val|
        if val.is_a? Hash
          aux = denormalize_translations_hash(val, "#{parent_key}#{key}.")
          aux.each do |k, v|
            ans[k] = v
          end
        else
          ans["#{parent_key}#{key}"] = val
        end
      end
      ans
    end

    def self.dig_set(obj, keys, value)
      key = keys.first
      if keys.length == 1
        obj[key] = value
      else
        obj[key] = {} unless obj[key]
        dig_set(obj[key], keys.slice(1..-1), value)
      end
    end

    def self.normalize_translations_hash(hash)
      result = {}
      hash.keys.sort.each do |key|
        dig_set(result, key.split("."), hash[key])
      end
      result
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
missing_translation-0.4.4 lib/missing_translation/yaml_processor.rb
missing_translation-0.4.3 lib/missing_translation/yaml_processor.rb
missing_translation-0.4.2 lib/missing_translation/yaml_processor.rb
missing_translation-0.4.1 lib/missing_translation/yaml_processor.rb
missing_translation-0.4.0 lib/missing_translation/yaml_processor.rb
missing_translation-0.3.0 lib/missing_translation/yaml_processor.rb