Module: Phrasing::Serializer

Defined in:
lib/phrasing/serializer.rb

Class Method Summary collapse

Class Method Details

.export_yaml(phrasing_phrases) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/phrasing/serializer.rb', line 37

def export_yaml(phrasing_phrases)
  hash = {}
  phrasing_phrases.each do |phrase|
    hash[phrase.locale] ||= {}
    hash[phrase.locale][phrase.key] = phrase.value
  end
  hash.to_yaml
end

.flatten_the_hash(hash) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/phrasing/serializer.rb', line 23

def flatten_the_hash(hash)
  new_hash = {}
  hash.each do |key, value|
    if value.is_a? Hash
      flatten_the_hash(value).each do |k,v|
        new_hash["#{key}.#{k}"] = v
      end
    else
      new_hash[key] = value
    end
  end
  new_hash
end

.import_yaml(yaml) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/phrasing/serializer.rb', line 5

def import_yaml(yaml)
  number_of_changes = 0
  hash = YAML.load(yaml)

  hash.each do |locale, data|
    flatten_the_hash(data).each do |key, value|
      phrase = PhrasingPhrase.where(key: key, locale: locale).first || PhrasingPhrase.new(key: key, locale: locale)
      if phrase.value != value
        phrase.value = value
        number_of_changes += 1
        phrase.save
      end
    end
  end

  number_of_changes
end