Module: SerializedAttributes::ClassMethods

Defined in:
lib/serialized_attributes/serialized_attributes.rb

Instance Method Summary collapse

Instance Method Details

#accessible_attribute(name, type, opts = {}) ⇒ Object



30
31
32
# File 'lib/serialized_attributes/serialized_attributes.rb', line 30

def accessible_attribute(name, type, opts = {})
  attribute(name, type, opts.merge({:attr_accessible => true}))
end

#attribute(name, type, opts = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/serialized_attributes/serialized_attributes.rb', line 38

def attribute(name, type, opts = {})
  name = name.to_s
  type = SerializedAttributes.type_to_sqltype(type)
  serialized_attributes_definition[name] = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, opts[:default], type.to_s, nil)

  define_method("#{name.to_s}=".to_sym) { |value| @attributes[name] = value }
  define_method(name) { self.class.serialized_attributes_definition[name].type_cast(@attributes[name]) }
  
  attr_accessible name if opts[:attr_accessible]
end

#instantiate(record) ⇒ Object



24
25
26
27
28
# File 'lib/serialized_attributes/serialized_attributes.rb', line 24

def instantiate(record)
  object = super(record)
  object.unpack_serialized_attributes!
  object
end

#serialized_attribute_namesObject



34
35
36
# File 'lib/serialized_attributes/serialized_attributes.rb', line 34

def serialized_attribute_names
  serialized_attributes_definition.keys
end

#serialized_attributes_definitionObject



20
21
22
# File 'lib/serialized_attributes/serialized_attributes.rb', line 20

def serialized_attributes_definition
  read_inheritable_attribute(:serialized_attributes_definition)
end