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)


53
54
55
56
# File 'lib/mobility/backends/sequel/serialized.rb', line 53

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



47
48
49
50
# File 'lib/mobility/backends/sequel/serialized.rb', line 47

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

.valid_keysObject



39
40
41
# File 'lib/mobility/backends/sequel/serialized.rb', line 39

def self.valid_keys
  super + [:format]
end

Instance Method Details

#translationsHash

Returns deserialized column value

Returns:



83
84
85
86
87
88
89
90
91
# File 'lib/mobility/backends/sequel/serialized.rb', line 83

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