Module: ActiveRecord::AttributeMethods::Serialization::ClassMethods

Defined in:
lib/serializable.rb

Instance Method Summary collapse

Instance Method Details

#serializable(method, class_name = Object) ⇒ Object



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
# File 'lib/serializable.rb', line 6

def serializable method, class_name = Object

  coder = if [:load, :dump].all? { |x| class_name.respond_to?(x) }
            class_name
          else
            Coders::YAMLColumn.new(class_name)
          end

  self.send(:define_method, "#{method}") do
    if self[method]
      @serializable_cache ||= {}
      @serializable_cache[method] ||= coder.load(self[method])
    else
      nil
    end
  end

  self.send(:define_method, "#{method}=") do |value|
    @serializable_cache ||= {}
    @serializable_cache[method] = value

    self[method] = coder.dump(value)
  end

end