Class: Pakyow::Support::Serializer
- Inherits:
-
Object
- Object
- Pakyow::Support::Serializer
- Defined in:
- lib/pakyow/support/serializer.rb
Overview
Persists state for an object.
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #deserialize ⇒ Object
-
#initialize(object, name:, path:, logger:) ⇒ Serializer
constructor
A new instance of Serializer.
- #serialize ⇒ Object
Constructor Details
#initialize(object, name:, path:, logger:) ⇒ Serializer
Returns a new instance of Serializer.
12 13 14 |
# File 'lib/pakyow/support/serializer.rb', line 12 def initialize(object, name:, path:, logger:) @object, @name, @path, @logger = object, name, path, logger end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'lib/pakyow/support/serializer.rb', line 10 def logger @logger end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/pakyow/support/serializer.rb', line 10 def name @name end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
10 11 12 |
# File 'lib/pakyow/support/serializer.rb', line 10 def object @object end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
10 11 12 |
# File 'lib/pakyow/support/serializer.rb', line 10 def path @path end |
Instance Method Details
#deserialize ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/pakyow/support/serializer.rb', line 25 def deserialize if File.exist?(serialized_state_path) Marshal.load(File.read(serialized_state_path)).each do |ivar, value| @object.instance_variable_set(ivar, value) end end rescue => error FileUtils.rm(serialized_state_path) @logger.error "[Serializer] failed to deserialize `#{@name}': #{error}" end |
#serialize ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/pakyow/support/serializer.rb', line 16 def serialize FileUtils.mkdir_p(@path) File.open(serialized_state_path, "w+") do |file| file.write(Marshal.dump(@object.serialize)) end rescue => error @logger.error "[Serializer] failed to serialize `#{@name}': #{error}" end |