Class: GIGO::ActiveRecord::Base::GigoCoder

Inherits:
Object
  • Object
show all
Defined in:
lib/gigo/active_record/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ GigoCoder

Returns a new instance of GigoCoder.



47
48
49
50
# File 'lib/gigo/active_record/base.rb', line 47

def initialize(klass)
  @klass = klass
  @default_internal = Encoding.default_internal
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



45
46
47
# File 'lib/gigo/active_record/base.rb', line 45

def klass
  @klass
end

Instance Method Details

#dump(value) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/gigo/active_record/base.rb', line 64

def dump(value)
  return klass.new.to_yaml if value.nil?
  unless value.is_a?(klass)
    raise ::ActiveRecord::SerializationTypeMismatch, "Attribute was supposed to be a #{klass.to_s}, but was a #{value.class}: #{value.inspect}"
  end
  value.to_yaml
end

#load(yaml) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gigo/active_record/base.rb', line 52

def load(yaml)
  return klass.new if yaml.nil?
  Encoding.default_internal = GIGO.encoding
  value = YAML.load(GIGO.load(yaml))
  unless value.is_a?(klass)
    raise ::ActiveRecord::SerializationTypeMismatch, "Attribute was supposed to be a #{klass.to_s}, but was a #{value.class}: #{value.inspect}"
  end
  value
ensure
  Encoding.default_internal = @default_internal
end