Class: ObjectStreamWrapper
- Inherits:
-
Object
- Object
- ObjectStreamWrapper
- Includes:
- Enumerable
- Defined in:
- lib/object-stream-wrapper.rb
Overview
Utility wrapper for basic ObjectStream class. Adds three groups of functionality:
* peer_name
* expect
* consume
Instance Attribute Summary collapse
-
#peer_name ⇒ Object
Not set by this library, but available for users to keep track of the peer in a symbolic, application-specific manner.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
-
#consume(&bl) ⇒ Object
The block is appended to a queue of procs that are called for the subsequently read objects, instead of iterating over or returning them.
- #each ⇒ Object
- #eof? ⇒ Boolean (also: #eof)
-
#expect(cl) ⇒ Object
Set the stream state so that subsequent objects returned by read will be instances of a custom class
cl
. -
#initialize(*args, **opts) ⇒ ObjectStreamWrapper
constructor
A new instance of ObjectStreamWrapper.
- #read ⇒ Object
- #to_io ⇒ Object
- #to_s ⇒ Object
-
#unexpect ⇒ Object
Turn off the custom class instantiation of #expect.
- #write(*objects) ⇒ Object (also: #<<)
- #write_to_outbox(*args, &bl) ⇒ Object
Constructor Details
#initialize(*args, **opts) ⇒ ObjectStreamWrapper
Returns a new instance of ObjectStreamWrapper.
20 21 22 23 24 25 26 |
# File 'lib/object-stream-wrapper.rb', line 20 def initialize *args, **opts @stream = ObjectStream.new(*args, **opts) @peer_name = "unknown" @expected_class = nil @consumers = [] unexpect end |
Instance Attribute Details
#peer_name ⇒ Object
Not set by this library, but available for users to keep track of the peer in a symbolic, application-specific manner. See funl for an example.
18 19 20 |
# File 'lib/object-stream-wrapper.rb', line 18 def peer_name @peer_name end |
Instance Method Details
#close ⇒ Object
103 104 105 |
# File 'lib/object-stream-wrapper.rb', line 103 def close @stream.close end |
#closed? ⇒ Boolean
107 108 109 |
# File 'lib/object-stream-wrapper.rb', line 107 def closed? @stream.closed? end |
#consume(&bl) ⇒ Object
The block is appended to a queue of procs that are called for the subsequently read objects, instead of iterating over or returning them. Helps with handshake protocols. Not affected by #expect.
46 47 48 |
# File 'lib/object-stream-wrapper.rb', line 46 def consume &bl @consumers << bl end |
#each ⇒ Object
83 84 85 86 87 |
# File 'lib/object-stream-wrapper.rb', line 83 def each return to_enum unless block_given? read {|obj| yield obj} until eof rescue EOFError end |
#eof? ⇒ Boolean Also known as: eof
98 99 100 |
# File 'lib/object-stream-wrapper.rb', line 98 def eof? @stream.eof? end |
#expect(cl) ⇒ Object
Set the stream state so that subsequent objects returned by read will be instances of a custom class cl
. Does not affect #consume. Class cl
should define cl.from_serialized, plus #to_json, #to_msgpack, etc. as needed by the underlying serialization library.
36 37 38 |
# File 'lib/object-stream-wrapper.rb', line 36 def expect cl @expected_class = cl end |
#read ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/object-stream-wrapper.rb', line 69 def read if block_given? @stream.read do |obj| try_consume(obj) or yield convert_to_expected(obj) end return nil else begin obj = @stream.read end while try_consume(obj) convert_to_expected(obj) end end |
#to_io ⇒ Object
111 112 113 |
# File 'lib/object-stream-wrapper.rb', line 111 def to_io @stream.to_io end |
#to_s ⇒ Object
28 29 30 |
# File 'lib/object-stream-wrapper.rb', line 28 def to_s "#<Wrapped #{@stream.class} to #{peer_name}, io=#{@stream.inspect}>" end |
#unexpect ⇒ Object
Turn off the custom class instantiation of #expect.
41 |
# File 'lib/object-stream-wrapper.rb', line 41 def unexpect; expect nil; end |
#write(*objects) ⇒ Object Also known as: <<
89 90 91 |
# File 'lib/object-stream-wrapper.rb', line 89 def write *objects @stream.write *objects end |
#write_to_outbox(*args, &bl) ⇒ Object
94 95 96 |
# File 'lib/object-stream-wrapper.rb', line 94 def write_to_outbox *args, &bl @stream.write_to_outbox *args, &bl end |