Module: ActiveRecord::PackedFields::ClassMethods

Defined in:
lib/active_record/packed_fields.rb

Instance Method Summary collapse

Instance Method Details

#packed(options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_record/packed_fields.rb', line 5

def packed(options)
  column = options[:column] || :packed
  fields = options[:fields]
  validation = options[:validation]

  serialize column, Hash
  

  fields.each do |field|
    define_method(field) { 
      hash = read_attribute(column)
      if hash
        hash[field]
      end
    }
    define_method("#{field}="){|value|
      hash = read_attribute(column)
      if hash
        hash[field] = value
      else
        write_attribute(column, { field => value })
      end
    }

    validates field, validation.dup if validation
  end
end