Class: ActiveRecord::Coders::YAMLColumn

Inherits:
Object
  • Object
show all
Defined in:
activerecord/lib/active_record/coders/yaml_column.rb

Overview

:nodoc:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (YAMLColumn) initialize(object_class = Object)

Returns a new instance of YAMLColumn



9
10
11
# File 'activerecord/lib/active_record/coders/yaml_column.rb', line 9

def initialize(object_class = Object)
  @object_class = object_class
end

Instance Attribute Details

- (Object) object_class

Returns the value of attribute object_class



7
8
9
# File 'activerecord/lib/active_record/coders/yaml_column.rb', line 7

def object_class
  @object_class
end

Instance Method Details

- (Object) dump(obj)



13
14
15
16
17
18
19
20
21
# File 'activerecord/lib/active_record/coders/yaml_column.rb', line 13

def dump(obj)
  return if obj.nil?

  unless obj.is_a?(object_class)
    raise SerializationTypeMismatch,
      "Attribute was supposed to be a #{object_class}, but was a #{obj.class}. -- #{obj.inspect}"
  end
  YAML.dump obj
end

- (Object) load(yaml)



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'activerecord/lib/active_record/coders/yaml_column.rb', line 23

def load(yaml)
  return object_class.new if object_class != Object && yaml.nil?
  return yaml unless yaml.is_a?(String) && yaml =~ /^---/
  obj = YAML.load(yaml)

  unless obj.is_a?(object_class) || obj.nil?
    raise SerializationTypeMismatch,
      "Attribute was supposed to be a #{object_class}, but was a #{obj.class}"
  end
  obj ||= object_class.new if object_class != Object

  obj
end