Module: SerialTranslator::ClassMethods
- Defined in:
- lib/serial_translator.rb
Instance Attribute Summary collapse
-
#serial_translator_attributes ⇒ Object
readonly
Returns the value of attribute serial_translator_attributes.
Instance Method Summary collapse
Instance Attribute Details
#serial_translator_attributes ⇒ Object (readonly)
Returns the value of attribute serial_translator_attributes.
15 16 17 |
# File 'lib/serial_translator.rb', line 15 def serial_translator_attributes @serial_translator_attributes end |
Instance Method Details
#serial_translator_for(*attributes) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/serial_translator.rb', line 17 def serial_translator_for(*attributes) @serial_translator_attributes = attributes attributes.each do |attribute| serialize :"#{attribute}_translations", Hash # Define the normal getter, that respects the # current translation locale define_method attribute do |locale = current_translation_locale| translations = translations_for(attribute) result = translations[locale] if translations[locale].present? result ||= translations.values.detect(&:present?) if translation_fallback? result end define_method :"#{attribute}?" do |locale = current_translation_locale| __send__(attribute.to_sym, locale).present? end # Define the normal setter, that respects the # current translation locale define_method "#{attribute}=" do |value| __send__(:"#{attribute}_translations_will_change!") translations = translations_for(attribute) if value.present? translations[current_translation_locale] = value else translations.delete(current_translation_locale) end __send__(:"#{attribute}_translations=", translations) end # Define getters for each specific available locale I18n.available_locales.each do |available_locale| define_method "#{attribute}_#{available_locale.to_s.underscore}" do begin old_locale = I18n.locale I18n.locale = available_locale __send__(attribute) ensure I18n.locale = old_locale end end end # Define setters for each specific available locale I18n.available_locales.each do |available_locale| define_method "#{attribute}_#{available_locale.to_s.underscore}=" do |value| begin old_locale = I18n.locale I18n.locale = available_locale __send__(:"#{attribute}=", value) ensure I18n.locale = old_locale end end end end end |