Class: SpoolPool::Spool
- Inherits:
-
Object
- Object
- SpoolPool::Spool
- Defined in:
- lib/spool_pool/spool.rb
Overview
This class manages the data storage and retrieval within a specific spool.
Instance Attribute Summary collapse
-
#pathname ⇒ Object
readonly
Returns the value of attribute pathname.
Class Method Summary collapse
-
.deserialize(data) ⇒ Object
:nodoc:.
-
.serialize(data) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#deserialize(data) ⇒ Object
Deserializes the
datathat has previously been serialized withserialize. -
#flush ⇒ Object
Retrieves and deserializes all the data in the spool, oldest data first.
-
#get(&block) ⇒ Object
Reads and returns the deserialized data of the oldest file in the spool, yielding the deserialized data to an optionally passed block..
-
#initialize(pathname) ⇒ Spool
constructor
Uses the directory given in
pathnameas the directory for the subsequent spooling operations. -
#put(data) ⇒ Object
Serializes and stores the
data. -
#serialize(data) ⇒ Object
Serializes the data so that it can be deserialized with the
deserializemethod later on.
Constructor Details
#initialize(pathname) ⇒ Spool
Uses the directory given in pathname as the directory for the subsequent spooling operations.
Will perform a simple check on the directory and throw an exception if the directory already exists but isn’t read- and writeable by the effective user id of the process.
19 20 21 22 |
# File 'lib/spool_pool/spool.rb', line 19 def initialize( pathname ) @pathname = pathname validate_spool_directory end |
Instance Attribute Details
#pathname ⇒ Object (readonly)
Returns the value of attribute pathname.
9 10 11 |
# File 'lib/spool_pool/spool.rb', line 9 def pathname @pathname end |
Class Method Details
.deserialize(data) ⇒ Object
:nodoc:
91 92 93 |
# File 'lib/spool_pool/spool.rb', line 91 def self.deserialize( data ) # :nodoc: YAML.load( data ) end |
.serialize(data) ⇒ Object
:nodoc:
80 81 82 |
# File 'lib/spool_pool/spool.rb', line 80 def self.serialize( data ) # :nodoc: YAML.dump( data ) end |
Instance Method Details
#deserialize(data) ⇒ Object
Deserializes the data that has previously been serialized with serialize.
87 88 89 |
# File 'lib/spool_pool/spool.rb', line 87 def deserialize( data ) self.class.deserialize( data ) end |
#flush ⇒ Object
Retrieves and deserializes all the data in the spool, oldest data first. Each piece of spooled data will be yielded to the supplied block.
Ordering is based on the files ctime, but the ordering is non-strict. Data stored within the same second will be returned in a random order.
63 64 65 66 67 68 69 70 |
# File 'lib/spool_pool/spool.rb', line 63 def flush loop do data = get break if data.nil? yield data end end |
#get(&block) ⇒ Object
Reads and returns the deserialized data of the oldest file in the spool, yielding the deserialized data to an optionally passed block..
Deletes the file only if no exception was raised within the block.
Ordering is based on the filename (which in turn is based on the files creation time), but the ordering is non-strict.
Data stored within the same second will be returned in a random order.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/spool_pool/spool.rb', line 45 def get(&block) file = oldest_spooled_file return nil unless file data = if block_given? SpoolPool::File.safe_read( file ) { |data| block.call( deserialize(data) ) } else SpoolPool::File.safe_read( file ) end deserialize(data) end |
#put(data) ⇒ Object
Serializes and stores the data.
Returns the path of the file storing the data.
29 30 31 32 |
# File 'lib/spool_pool/spool.rb', line 29 def put( data ) @pathname.mkpath unless @pathname.exist? SpoolPool::File.write( @pathname, serialize( data ) ) end |
#serialize(data) ⇒ Object
Serializes the data so that it can be deserialized with the deserialize method later on.
76 77 78 |
# File 'lib/spool_pool/spool.rb', line 76 def serialize( data ) self.class.serialize( data ) end |