Sha256: 2f810a38afd3d6fbacb5b8ce2795378fc833e2a5ae02e9e9f8dc3aa94086d8a8

Contents?: true

Size: 827 Bytes

Versions: 10

Compression:

Stored size: 827 Bytes

Contents

class Translation < String
  attr_accessor :keys

  def initialize(*args)
    super
    @keys = {}
    @keys["default"] = "en"
  end

  def []=(lang, text)
    @keys[lang.to_s] = text
  end

  def [](lang)
    @keys[lang.to_s]
  end

  def languages
    langs = @keys.keys
    langs.delete("default")
    langs
  end

  def default_language=(lang)
    @keys["default"] = lang
    self.replace(@keys[lang.to_s])
  end

  def self.build(keys, default = "en")
    tr = self.new
    tr.keys = keys
    tr.default_language = default
    tr
  end

  def self.to_mongo(value)
    return value.keys if value.kind_of?(self)

    @keys
  end

  def self.from_mongo(value)
    return value if value.kind_of?(self)

    result = self.new
    result.keys = value
    result.default_language = value["default"] || "en"

    result
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
mongomapper_ext-0.5.2 lib/mongomapper_ext/types/translation.rb
mongomapper_ext-0.5.1 lib/mongomapper_ext/types/translation.rb
mongomapper_ext-0.5.0 lib/mongomapper_ext/types/translation.rb
mongomapper_ext-0.4.0 lib/mongomapper_ext/types/translation.rb
mongomapper_ext-0.3.0 lib/mongomapper_ext/types/translation.rb
mongomapper_ext-0.2.4 lib/mongomapper_ext/types/translation.rb
mongomapper_ext-0.2.3 lib/mongomapper_ext/types/translation.rb
mongomapper_ext-0.2.2 lib/mongomapper_ext/types/translation.rb
mongomapper_ext-0.2.1 lib/mongomapper_ext/types/translation.rb
mongomapper_ext-0.2.0 lib/mongomapper_ext/types/translation.rb