Class: BioDSL::Serializer
- Inherits:
-
Object
- Object
- BioDSL::Serializer
- Includes:
- Enumerable
- Defined in:
- lib/BioDSL/serializer.rb
Overview
Class for serializing and de-serializing data using Marshal.
Instance Method Summary collapse
-
#<<(obj) ⇒ Object
(also: #writei)
Method to write serialized data using Marshal to a given IO.
-
#each {|Object| ... } ⇒ Object
Iterator for reading and de-serialized data from a given IO.
-
#initialize(io, &block) ⇒ Serializer
constructor
Constructor for serializer.
-
#next_entry ⇒ Object
Read next entry from serialized stream.
Constructor Details
#initialize(io, &block) ⇒ Serializer
Constructor for serializer.
45 46 47 48 49 50 51 |
# File 'lib/BioDSL/serializer.rb', line 45 def initialize(io, &block) @io = io fail SerializerError, 'No block given' unless block block.call(self) end |
Instance Method Details
#<<(obj) ⇒ Object Also known as: writei
Method to write serialized data using Marshal to a given IO.
64 65 66 67 68 |
# File 'lib/BioDSL/serializer.rb', line 64 def <<(obj) data = Marshal.dump(obj) @io.write([data.size].pack('N')) @io.write(data) end |
#each {|Object| ... } ⇒ Object
Iterator for reading and de-serialized data from a given IO.
84 85 86 |
# File 'lib/BioDSL/serializer.rb', line 84 def each yield next_entry until @io.eof? end |
#next_entry ⇒ Object
Read next entry from serialized stream.
91 92 93 94 95 96 |
# File 'lib/BioDSL/serializer.rb', line 91 def next_entry size = @io.read(4) fail EOFError unless size data = @io.read(size.unpack('N').first) Marshal.load(data) end |