Module: ArAttributeSerializer::ClassMethods

Defined in:
lib/ar_attribute_serializer.rb

Instance Method Summary collapse

Instance Method Details

#attribute_serializer(field, *attributes) ⇒ Object

You need a blob field to store serialized data, and a list of attributes and their types. For example, with field called ‘data’: attribute_serializer :data, :name, :bio



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ar_attribute_serializer.rb', line 14

def attribute_serializer(field, *attributes)
  
  serialize field, Hash
  
  attributes.each do |attribute|
    class_eval %Q^
      def #{attribute}
        self.#{field} ||= { }
        self.#{field}[:#{attribute}]
      end
      def #{attribute}=(value)
        self.#{field} ||= { }
        self.#{field}[:#{attribute}] = value
      end
    ^
  end
end