Sha256: 3ed3ed498ce858ce1583943e7d2c89887ef160aa9d37e658ee83e7a95cf781f1

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

# this class stores metadata about terms in a locale
# specifically things like errors, notes, autotranslated
# it acts kind of like a hash where you give it a key and it returns a metadatum object
class Metadata
  def initialize(hash = {})
    @hash = hash
  end

  def [](key)
    if @hash[key].is_a?(Metadatum)
      @hash[key]
    else
      @hash[key] = Metadatum.new(@hash[key])
    end
  end

  def []=(key, value)
    raise("you may only assign a metadatum") unless value.is_a?(Metadatum)
    @hash[key] = value.dup
  end

  def delete(key)
    @hash.delete(key)
  end

  def to_h
    compacted_hash = {}
    @hash.keys.sort.each do |key|
      value = @hash[key].to_h
      compacted_hash[key] = value if value.present?
    end
    compacted_hash
  end

  def to_yaml
    to_h.to_yaml
  end

  class Metadatum
    attr_accessor :errors, :notes, :autotranslated

    def initialize(hash)
      safe_hash = hash || {}
      @errors = safe_hash['errors'] || []
      @notes = safe_hash['notes']
      @autotranslated = !!safe_hash['autotranslated']
    end

    def to_h
      hash = {}
      hash['errors'] = @errors unless @errors.empty?
      hash['notes'] = @notes unless @notes.blank?
      hash['autotranslated'] = @autotranslated if autotranslated
      hash
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
i18n-migrations-2.0.1 lib/i18n/migrations/metadata.rb
i18n-migrations-2.0.0 lib/i18n/migrations/metadata.rb