Module | Sequel::Plugins::Serialization
In: | lib/sequel/plugins/serialization.rb

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.

### Methods

apply

### Classes and Modules

Module Sequel::Plugins::Serialization::ClassMethods
Module Sequel::Plugins::Serialization::InstanceMethods

### Public Class methods

apply(model, format, *columns)

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

[Source]

        # File lib/sequel/plugins/serialization.rb, line 24
    24:       def self.apply(model, format, *columns)
    25:         raise(Error, "Unsupported serialization format (#{format}), should be :marshal or :yaml") unless [:marshal, :yaml].include?(format)
    26:         raise(Error, "No columns given.  The serialization plugin requires you specify which columns to serialize") if columns.empty?
    27:         model.instance_eval do
    28:           @serialization_format = format
    29:           @serialized_columns = columns
    30:           InstanceMethods.module_eval do
    31:             columns.each do |column|
    32:               define_method(column) do
    33:                 if deserialized_values.has_key?(column)
    34:                   deserialized_values[column]
    35:                 else
    36:                   deserialized_values[column] = deserialize_value(@values[column])
    37:                 end
    38:               end
    39:               define_method("#{column}=") do |v|
    40:                 changed_columns << column unless changed_columns.include?(column)
    41:                 deserialized_values[column] = v
    42:               end
    43:             end
    44:           end
    45:         end
    46:       end

[Validate]