Module: Spira::Serialization

Included in:
Base
Defined in:
lib/spira/serialization.rb

Instance Method Summary collapse

Instance Method Details

#encode_with(coder) ⇒ Object

Support for Psych (YAML) custom serializer.

This causes the subject and all attributes to be saved to a YAML or JSON serialization in such a way that they can be restored in the future.

Parameters:

  • coder (Psych::Coder)


10
11
12
13
# File 'lib/spira/serialization.rb', line 10

def encode_with(coder)
  coder["subject"] = subject
  attributes.each {|p,v| coder[p.to_s] = v if v}
end

#init_with(coder) ⇒ Object

Support for Psych (YAML) custom de-serializer.

Updates a previously allocated Spira::Base instance to that of a previously serialized instance.

Parameters:

  • coder (Psych::Coder)


22
23
24
25
# File 'lib/spira/serialization.rb', line 22

def init_with(coder)
  instance_variable_set(:"@subject", coder["subject"])
  assign_attributes coder.map.except("subject")
end