class Multilang::MultilangTranslationKeeper

Attributes

attribute[R]
model[R]
translations[R]

Public Class Methods

new(model, attribute) click to toggle source
# File lib/multilang-hstore/translation_keeper.rb, line 8
def initialize(model, attribute)
  @model = model
  @attribute = attribute
  @translations = {}
  load!
end

Public Instance Methods

[](locale) click to toggle source
# File lib/multilang-hstore/translation_keeper.rb, line 42
def [](locale)
  read(locale)
end
[]=(locale, value) click to toggle source
# File lib/multilang-hstore/translation_keeper.rb, line 46
def []=(locale, value)
  write(locale, value)
  flush!
end
current_or_any_value() click to toggle source
# File lib/multilang-hstore/translation_keeper.rb, line 19
def current_or_any_value
  @translations.current_or_any_value
end
empty?() click to toggle source
# File lib/multilang-hstore/translation_keeper.rb, line 55
def empty?
  @translations.empty?
end
locales() click to toggle source
# File lib/multilang-hstore/translation_keeper.rb, line 51
def locales
  @translations.locales
end
to_s() click to toggle source
# File lib/multilang-hstore/translation_keeper.rb, line 23
def to_s
  raw_read(actual_locale)  
end
to_str(locale = nil) click to toggle source
# File lib/multilang-hstore/translation_keeper.rb, line 27
def to_str(locale = nil)
  locale ||= actual_locale
  raw_read(locale)
end
update(value) click to toggle source
# File lib/multilang-hstore/translation_keeper.rb, line 32
def update(value)
  if value.is_a?(Hash)
    clear
    value.each{|k, v| write(k, v)}
  elsif value.is_a?(String)
    write(actual_locale, value)
  end
  flush!
end
value(locale = nil) click to toggle source
# File lib/multilang-hstore/translation_keeper.rb, line 15
def value(locale = nil)
  @translations.value(locale)
end

Private Instance Methods

actual_locale() click to toggle source
# File lib/multilang-hstore/translation_keeper.rb, line 61
def actual_locale
  @translations.actual_locale
end
clear() click to toggle source
# File lib/multilang-hstore/translation_keeper.rb, line 77
def clear
  @translations.clear
end
flush!() click to toggle source
# File lib/multilang-hstore/translation_keeper.rb, line 126
def flush!
  @model.send("#{@attribute}_will_change!")
  @model[@attribute] = @translations
end
load!() click to toggle source
# File lib/multilang-hstore/translation_keeper.rb, line 81
def load!
  data = @model[@attribute]
  data = data.blank? ? nil : data
  @translations = data.is_a?(Hash) ? data : {}

  class << translations
    attr_accessor :multilang_keeper
    def to_s
      "#{current_or_any_value}"
    end

    def locales
      self.keys.map(&:to_sym)
    end

    def read(locale)
      MultilangTranslationProxy.new(self.multilang_keeper, locale)
    end

    def current_or_any_value
      v = value
      if v.empty?
        reduced_locales = locales - [actual_locale]
        reduced_locales.each do |locale|
          v = value(locale)
          return v unless v.empty?
        end
      else
        return v
      end
      return ''
    end

    def value(locale = nil)
      locale ||= actual_locale
      read(locale)
    end

    def actual_locale
      I18n.locale
    end
  end
  @translations.public_send("multilang_keeper=", self)
end
raw_read(locale) click to toggle source
# File lib/multilang-hstore/translation_keeper.rb, line 73
def raw_read(locale)
  @translations[locale.to_s] || ''
end
read(locale) click to toggle source
# File lib/multilang-hstore/translation_keeper.rb, line 69
def read(locale)
  @translations.read(locale)
end
write(locale, value) click to toggle source
# File lib/multilang-hstore/translation_keeper.rb, line 65
def write(locale, value)
  @translations[locale.to_s] = value
end