module Sequel::Plugins::Serialization::ClassMethods

Attributes

deserialization_map[R]

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

serialization_map[R]

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

Public Instance Methods

freeze() click to toggle source

Freeze serialization metadata when freezing model class.

Calls superclass method
    # File lib/sequel/plugins/serialization.rb
127 def freeze
128   @deserialization_map.freeze
129   @serialization_map.freeze
130   @serialization_module.freeze if @serialization_module
131 
132   super
133 end
serialize_attributes(format, *columns) click to toggle source

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.

    # File lib/sequel/plugins/serialization.rb
142 def serialize_attributes(format, *columns)
143   if format.is_a?(Symbol)
144     unless format = Sequel.synchronize{REGISTERED_FORMATS[format]}
145       raise(Error, "Unsupported serialization format: #{format} (valid formats: #{Sequel.synchronize{REGISTERED_FORMATS.keys}.inspect})")
146     end
147   end
148   serializer, deserializer = format
149   raise(Error, "No columns given.  The serialization plugin requires you specify which columns to serialize") if columns.empty?
150   define_serialized_attribute_accessor(serializer, deserializer, *columns)
151 end

Private Instance Methods

define_serialized_attribute_accessor(serializer, deserializer, *columns) click to toggle source

Add serializated attribute acessor methods to the serialization_module

Calls superclass method
    # File lib/sequel/plugins/serialization.rb
156 def define_serialized_attribute_accessor(serializer, deserializer, *columns)
157   m = self
158   include(@serialization_module ||= Sequel.set_temp_name(Module.new){"#{name}::@serialization_module"}) unless @serialization_module
159   @serialization_module.class_eval do
160     columns.each do |column|
161       m.serialization_map[column] = serializer
162       m.deserialization_map[column] = deserializer
163       define_method(column) do 
164         if deserialized_values.has_key?(column)
165           deserialized_values[column]
166         elsif frozen?
167           deserialize_value(column, super())
168         else
169           deserialized_values[column] = deserialize_value(column, super())
170         end
171       end
172       alias_method(column, column)
173 
174       setter = :"#{column}="
175       define_method(setter) do |v| 
176         cc = changed_columns
177         if !cc.include?(column) && (new? || get_column_value(column) != v)
178           cc << column
179 
180           will_change_column(column) if respond_to?(:will_change_column)
181         end
182 
183         deserialized_values[column] = v
184       end
185       alias_method(setter, setter)
186     end
187   end
188 end