Module: Defaultable::Serialization::ClassMethods

Defined in:
lib/defaultable/serialization.rb

Instance Method Summary collapse

Instance Method Details

#dump(obj) ⇒ Object

Called to convert from ruby object to serialized data.

Raises:

  • (TypeError)


22
23
24
25
26
27
28
29
30
# File 'lib/defaultable/serialization.rb', line 22

def dump(obj)
	raise TypeError, "Serialization failed: Object is not of type #{self.name}." if !obj.kind_of?(self) && !obj.nil?

	if obj.nil?
		self.new
  else
  	obj.class.new(obj.registry.as_hash).to_yaml if obj
 end
end

#load(data) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/defaultable/serialization.rb', line 9

def load(data)
	overrides = {}

	if data
  	obj = raw_load(data)
  	raise TypeError, "Deserialized object is not of type #{self.name}. Got #{obj.class}" unless obj.kind_of?(self)
  	overrides = obj.as_hash
  end

 	self.new(overrides)
end

#raw_load(data) ⇒ Object



32
33
34
# File 'lib/defaultable/serialization.rb', line 32

def raw_load(data)
	YAML.load(data)
end