Class: Langulator::Munger

Inherits:
Object
  • Object
show all
Defined in:
lib/langulator/munger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Munger

Returns a new instance of Munger.



5
6
7
8
9
# File 'lib/langulator/munger.rb', line 5

def initialize(options = {})
  @source_language = options[:language]
  @source_translations = options[:translations]
  @alternate_translations = options[:alternates]
end

Instance Attribute Details

#alternate_translationsObject (readonly)

Returns the value of attribute alternate_translations.



4
5
6
# File 'lib/langulator/munger.rb', line 4

def alternate_translations
  @alternate_translations
end

#source_languageObject (readonly)

Returns the value of attribute source_language.



4
5
6
# File 'lib/langulator/munger.rb', line 4

def source_language
  @source_language
end

#source_translationsObject (readonly)

Returns the value of attribute source_translations.



4
5
6
# File 'lib/langulator/munger.rb', line 4

def source_translations
  @source_translations
end

Instance Method Details

#insert(language, translations, dictionary) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/langulator/munger.rb', line 32

def insert(language, translations, dictionary)
  dictionary.dup.each do |key, value|
    if value.is_a?(Hash)
      insert(language, (translations || {})[key], value)
    else
      dictionary[language] = translations
    end
  end
  dictionary
end

#mungeObject



11
12
13
14
15
16
17
# File 'lib/langulator/munger.rb', line 11

def munge
  dictionary = transform(source_language, source_translations)
  alternate_translations.each do |language, translations|
    dictionary = insert(language, translations, dictionary)
  end
  dictionary
end

#transform(language, translations) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/langulator/munger.rb', line 19

def transform(language, translations)
  dictionary = {}
  translations.each do |key, value|
    dictionary[key] ||= {}
    if value.is_a?(Hash)
      dictionary[key] = transform(language, value)
    else
      dictionary[key][language] = value
    end
  end
  dictionary
end