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.



120
121
122
# File 'lib/sequel/plugins/serialization.rb', line 120

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.



124
125
126
# File 'lib/sequel/plugins/serialization.rb', line 124

def serialization_map
  @serialization_map
end

Instance Method Details

#freezeObject

Freeze serialization metadata when freezing model class.



129
130
131
132
133
134
135
# File 'lib/sequel/plugins/serialization.rb', line 129

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:



144
145
146
147
148
149
150
151
152
153
# File 'lib/sequel/plugins/serialization.rb', line 144

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