Module: Sequel::Plugins::Serialization

Defined in:


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 or yaml as the serialization format. If you use yaml, you should require yaml 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.

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.

# File 'lib/sequel/lib/sequel/plugins/serialization.rb', line 24

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

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

# File 'lib/sequel/lib/sequel/plugins/serialization.rb', line 28

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