Class: Mobility::Backends::Sequel::Serialized

Inherits:
Object
  • Object
show all
Includes:
HashValued, Mobility::Backends::Sequel
Defined in:
lib/mobility/backends/sequel/serialized.rb

Overview

Implements Mobility::Backends::Serialized backend for Sequel models, using the Sequel serialization plugin.

Examples:

Define attribute with serialized backend

class Post < Sequel::Model
  extend Mobility
  translates :title, backend: :serialized, format: :yaml
end

Read and write attribute translations

post = Post.create(title: "foo")
post.title
#=> "foo"
Mobility.locale = :ja
post.title = "あああ"
post.save
post.deserialized_values[:title]       # get deserialized value
#=> {:en=>"foo", :ja=>"あああ"}
post.title(super: true)                # get serialized value
#=> "---\n:en: foo\n:ja: \"あああ\"\n"

See Also:

Defined Under Namespace

Modules: SerializationModificationDetectionFix Classes: QueryMethods

Backend Configuration collapse

Instance Method Summary collapse

Methods included from HashValued

#each_locale, #read, #write

Methods included from Mobility::Backends::Sequel

included, #setup_query_methods

Class Method Details

.configure(options) ⇒ Object

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • format (Symbol) — default: :yaml

    Serialization format

Raises:

  • (ArgumentError)

    if a format other than :yaml or :json is passed in



44
45
46
# File 'lib/mobility/backends/sequel/serialized.rb', line 44

def self.configure(options)
  Serialized.configure(options)
end

Instance Method Details

#translationsHash

Returns deserialized column value

Returns:

  • (Hash)


75
76
77
78
79
80
81
82
83
84
# File 'lib/mobility/backends/sequel/serialized.rb', line 75

def translations
  attribute_ = attribute.to_sym
  if model.deserialized_values.has_key?(attribute_)
    model.deserialized_values[attribute_]
  elsif model.frozen?
    deserialize_value(attribute_, serialized_value)
  else
    model.deserialized_values[attribute_] = deserialize_value(attribute_, serialized_value)
  end
end