module Sequel::Plugins::Serialization

  1. lib/sequel/plugins/serialization.rb

The serialization plugin allows you to keep serialized ruby objects in the database, while giving you deserialized objects when you call an accessor.

This plugin works by keeping the serialized value in the values, and adding a deserialized_values hash. The reader method for serialized columns will check the deserialized_values for the value, return it if present, or deserialize the entry in values and return it. The writer method will set the deserialized_values entry. This plugin adds a before_validation hook that serializes all deserialized_values to values.

You can specify the serialization format as a pair of serializer/deserializer callable objects. You can also specify the serialization format as a single symbol, if such a symbol has a registered serializer/deserializer pair in the plugin. By default, the plugin registers the :marshal, :yaml, and :json serialization formats. To register your own serialization formats, use Sequel::Plugins::Serialization.register_format. If you use yaml or json format, you need to require the libraries, Sequel does not do the requiring for you.

You can specify the columns to serialize when loading the plugin, or later using the serialize_attributes class method.

Because of how this plugin works, it must be used inside each model class that needs serialization, after any set_dataset method calls in that class. Otherwise, it is possible that the default column accessors will take precedence.

Example

# Require json if you plan to use it, as the plugin doesn't require it for you.
require 'json'

# Register custom serializer/deserializer pair, if desired
require 'sequel/plugins/serialization'
Sequel::Plugins::Serialization.register_format(:reverse, :reverse.to_proc, :reverse.to_proc)

class User < Sequel::Model
  # Built-in format support when loading the plugin
  plugin :serialization, :json, :permissions

  # Built-in format support after loading the plugin using serialize_attributes
  plugin :serialization
  serialize_attributes :marshal, :permissions

  # Use custom registered serialization format just like built-in format
  serialize_attributes :reverse, :password

  # Use a custom serializer/deserializer pair without registering
  serialize_attributes [:reverse.to_proc, :reverse.to_proc], :password
end
user = User.create
user.permissions = {global: 'read-only'}
user.save

Note that if you mutate serialized column values without reassigning them, those changes won’t be picked up by Model#save_changes or Model#update. Example:

user = User[1]
user.permissions[:global] = 'foo'
user.save_changes # Will not pick up changes to permissions

You can use the serialization_modification_detection plugin to pick up such changes.

Methods

Public Class

  1. apply
  2. configure
  3. register_format

Constants

REGISTERED_FORMATS = {}  

The default serializers supported by the serialization module. Use register_format to add serializers to this hash.

Public Class methods

apply(model, *args)

Set up the column readers to do deserialization and the column writers to save the value in deserialized_values.

[show source]
   # File lib/sequel/plugins/serialization.rb
77 def self.apply(model, *args)
78   model.instance_exec do
79     @deserialization_map = {}
80     @serialization_map = {}
81   end
82 end
configure(model, format=nil, *columns)

Automatically call serialize_attributes with the format and columns unless no columns were provided.

[show source]
   # File lib/sequel/plugins/serialization.rb
86 def self.configure(model, format=nil, *columns)
87   model.serialize_attributes(format, *columns) unless columns.empty?
88 end
register_format(format, serializer, deserializer)

Register a serializer/deserializer pair with a format symbol, to allow models to pick this format by name. Both serializer and deserializer should be callable objects.

[show source]
   # File lib/sequel/plugins/serialization.rb
93 def self.register_format(format, serializer, deserializer)
94   Sequel.synchronize{REGISTERED_FORMATS[format] = [serializer, deserializer].freeze}
95 end