Class: Langulator::Munger
- Inherits:
-
Object
- Object
- Langulator::Munger
- Defined in:
- lib/langulator/munger.rb
Instance Attribute Summary collapse
-
#alternate_translations ⇒ Object
readonly
Returns the value of attribute alternate_translations.
-
#source_language ⇒ Object
readonly
Returns the value of attribute source_language.
-
#source_translations ⇒ Object
readonly
Returns the value of attribute source_translations.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Munger
constructor
A new instance of Munger.
- #insert(language, translations, dictionary) ⇒ Object
- #munge ⇒ Object
- #transform(language, translations) ⇒ Object
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( = {}) @source_language = [:language] @source_translations = [:translations] @alternate_translations = [:alternates] end |
Instance Attribute Details
#alternate_translations ⇒ Object (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_language ⇒ Object (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_translations ⇒ Object (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 |
#munge ⇒ Object
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 |