Class: Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n/migrations/metadata.rb

Overview

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

Defined Under Namespace

Classes: Metadatum

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Metadata

Returns a new instance of Metadata.



5
6
7
# File 'lib/i18n/migrations/metadata.rb', line 5

def initialize(hash = {})
  @hash = hash
end

Instance Method Details

#[](key) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/i18n/migrations/metadata.rb', line 9

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

#[]=(key, value) ⇒ Object



17
18
19
20
# File 'lib/i18n/migrations/metadata.rb', line 17

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

#delete(key) ⇒ Object



22
23
24
25
26
# File 'lib/i18n/migrations/metadata.rb', line 22

def delete(key)
  metadatum = self[key]
  @hash.delete(key)
  metadatum
end

#to_hObject



28
29
30
31
32
33
34
35
# File 'lib/i18n/migrations/metadata.rb', line 28

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

#to_yamlObject



37
38
39
# File 'lib/i18n/migrations/metadata.rb', line 37

def to_yaml
  to_h.to_yaml
end