Sha256: 01266ff72e0f867e8414fbb17ec5f39c1a61bbfbb7951ad2cc347a7081a7343d
Contents?: true
Size: 1.05 KB
Versions: 2
Compression:
Stored size: 1.05 KB
Contents
# An accumulator buffer for Ruby objects. Use it to sequentially store a shitload # of objects on disk and then retreive them one by one. Make sure to call #close! when done with it to # discard the stored blob. This object is intended to be used as a Tracksperanto::Import::Base#receiver class Tracksperanto::Accumulator < DelegateClass(IO) # Stores the number of objects stored so far attr_reader :numt def initialize __setobj__(Tracksperanto::BufferIO.new) @numt = 0 end # Store an object def push(object_to_store) @numt += 1 d = Marshal.dump(object_to_store) bytelen = d.size write(bytelen) write("\t") write(d) write("\n") end # Retreive each stored object in succession def each_object_with_index rewind @numt.times { |i| yield(recover_object, i - 1) } end private def recover_object # Up to the tab is the amount of bytes to read demarshal_bytes = gets("\t").strip.to_i # Then read the bytes and unmarshal it Marshal.load(read(demarshal_bytes)) end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
tracksperanto-2.0.1 | lib/tracksperanto/accumulator.rb |
tracksperanto-2.0.0 | lib/tracksperanto/accumulator.rb |