Module: Sequel::Plugins::Serialization

Defined in:
lib/sequel/plugins/serialization.rb

Overview

Sequel’s built in 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 deserialized the entry in @values and return it. The writer method will set the @deserialized_values entry. This plugin adds a before_save hook that serializes all @deserialized_values to @values.

You can use either marshal, yaml, or json as the serialization format. If you use yaml or json, you should require them by yourself.

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 'sequel'
require 'json'
class User < Sequel::Model
  plugin :serialization, :json, :permissions
  # or
  plugin :serialization
  serialize_attributes :marshal, :permissions, :attributes
end
user = User.create
user.permissions = { :global => 'read-only' }
user.save

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.apply(model, *args) ⇒ Object

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



38
39
40
# File 'lib/sequel/plugins/serialization.rb', line 38

def self.apply(model, *args)
  model.instance_eval{@serialization_map = {}}
end

.configure(model, format = nil, *columns) ⇒ Object



42
43
44
# File 'lib/sequel/plugins/serialization.rb', line 42

def self.configure(model, format=nil, *columns)
  model.serialize_attributes(format, *columns) unless columns.empty?
end