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

Inherits:
Object
  • Object
show all
Includes:
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

Backend Configuration collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mobility::Backends::Sequel

included

Class Method Details

.build_op(attr, _locale) ⇒ Object

Raises:

  • (ArgumentError)

49
50
51
52
# File 'lib/mobility/backends/sequel/serialized.rb', line 49

def self.build_op(attr, _locale)
  raise ArgumentError,
    "You cannot query on mobility attributes translated with the Serialized backend (#{attr})."
end

.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


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

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

Instance Method Details

#translationsHash

Returns deserialized column value

Returns:

  • (Hash)

79
80
81
82
83
84
85
86
87
# File 'lib/mobility/backends/sequel/serialized.rb', line 79

def translations
  if model.deserialized_values.has_key?(column_name)
    model.deserialized_values[column_name]
  elsif model.frozen?
    deserialize_value(serialized_value)
  else
    model.deserialized_values[column_name] = deserialize_value(serialized_value)
  end
end