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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#deserialization_mapObject (readonly)

A hash with column name symbols and callable values, with the value called to deserialize the column.


109
110
111
# File 'lib/sequel/plugins/serialization.rb', line 109

def deserialization_map
  @deserialization_map
end

#serialization_mapObject (readonly)

A hash with column name symbols and callable values, with the value called to serialize the column.


113
114
115
# File 'lib/sequel/plugins/serialization.rb', line 113

def serialization_map
  @serialization_map
end

Instance Method Details

#freezeObject

Freeze serialization metadata when freezing model class.

[View source]

118
119
120
121
122
123
124
# File 'lib/sequel/plugins/serialization.rb', line 118

def freeze
  @deserialization_map.freeze
  @serialization_map.freeze
  @serialization_module.freeze if @serialization_module

  super
end

#serialize_attributes(format, *columns) ⇒ Object

Create instance level reader that deserializes column values on request, and instance level writer that stores new deserialized values. If format is a symbol, it should correspond to a previously-registered format using register_format. Otherwise, format is expected to be a 2-element array of callables, with the first element being the serializer, used to convert the value used by the application to the value that will be stored in the database, and the second element being the deserializer, used to convert the value stored the database to the value used by the application.

Raises:

[View source]

133
134
135
136
137
138
139
140
141
142
# File 'lib/sequel/plugins/serialization.rb', line 133

def serialize_attributes(format, *columns)
  if format.is_a?(Symbol)
    unless format = Sequel.synchronize{REGISTERED_FORMATS[format]}
      raise(Error, "Unsupported serialization format: #{format} (valid formats: #{Sequel.synchronize{REGISTERED_FORMATS.keys}.inspect})")
    end
  end
  serializer, deserializer = format
  raise(Error, "No columns given.  The serialization plugin requires you specify which columns to serialize") if columns.empty?
  define_serialized_attribute_accessor(serializer, deserializer, *columns)
end