Class: Mobility::Backend::Sequel::Serialized

Inherits:
Object
  • Object
show all
Includes:
Mobility::Backend
Defined in:
lib/mobility/backend/sequel/serialized.rb

Overview

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

Examples:

Define attribute with serialized backend

class Post < Sequel::Model
  include 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_before_mobility             # get serialized value
#=> "---\n:en: foo\n:ja: \"あああ\"\n"

See Also:

Defined Under Namespace

Modules: SerializationModificationDetectionFix Classes: QueryMethods

Instance Attribute Summary

Attributes included from Mobility::Backend

#attribute, #model, #options

Backend Accessors collapse

Backend Configuration collapse

Cache Methods collapse

Instance Method Summary collapse

Methods included from Mobility::Backend

included, #initialize, method_name

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



51
52
53
54
55
# File 'lib/mobility/backend/sequel/serialized.rb', line 51

def self.configure!(options)
  options[:format] ||= :yaml
  options[:format] = options[:format].downcase.to_sym
  raise ArgumentError, "Serialized backend only supports yaml or json formats." unless [:yaml, :json].include?(options[:format])
end

Instance Method Details

#new_cacheHash

Returns:

  • (Hash)


102
103
104
# File 'lib/mobility/backend/sequel/serialized.rb', line 102

def new_cache
  translations
end

#read(locale, **options) ⇒ Object

Returns Value of translation.

Parameters:

  • locale (Symbol)

    Locale to read

  • options (Hash)

Returns:

  • (Object)

    Value of translation



38
39
40
# File 'lib/mobility/backend/sequel/serialized.rb', line 38

def read(locale, **options)
  translations[locale]
end

#translationsHash

Returns deserialized column value

Returns:

  • (Hash)


89
90
91
92
93
94
95
96
97
98
# File 'lib/mobility/backend/sequel/serialized.rb', line 89

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

#write(locale, value, **options) ⇒ Object

Returns Value of translation.

Parameters:

  • locale (Symbol)

    Locale to read

  • options (Hash)

Returns:

  • (Object)

    Value of translation



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

def write(locale, value, **options)
  translations[locale] = value
end

#write_to_cache?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/mobility/backend/sequel/serialized.rb', line 107

def write_to_cache?
  true
end