Class: UmbrellioUtils::LargeObject::Writer
- Inherits:
-
Object
- Object
- UmbrellioUtils::LargeObject::Writer
- Defined in:
- lib/umbrellio_utils/large_object/writer.rb
Overview
:nocov:
Constant Summary collapse
- MAX_BUFFER_SIZE =
PG_PAGE_SIZE * 10
Instance Attribute Summary collapse
-
#large_object ⇒ Object
readonly
Returns the value of attribute large_object.
Instance Method Summary collapse
- #flush ⇒ Object
-
#initialize(large_object) ⇒ Writer
constructor
A new instance of Writer.
- #pos=(new_pos) ⇒ Object
- #reopen(other) ⇒ Object
- #rewind ⇒ Object
- #tell ⇒ Object (also: #pos)
- #write ⇒ Object (also: #<<)
Constructor Details
#initialize(large_object) ⇒ Writer
Returns a new instance of Writer.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/umbrellio_utils/large_object/writer.rb', line 11 def initialize(large_object) @large_object = large_object self.sio = StringIO.new.binmode super() if block_given? yield(self) flush end end |
Instance Attribute Details
#large_object ⇒ Object (readonly)
Returns the value of attribute large_object.
9 10 11 |
# File 'lib/umbrellio_utils/large_object/writer.rb', line 9 def large_object @large_object end |
Instance Method Details
#flush ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/umbrellio_utils/large_object/writer.rb', line 22 def flush sio.flush large_object.append!(sio.string) sio.truncate(0) sio.rewind self end |
#pos=(new_pos) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/umbrellio_utils/large_object/writer.rb', line 47 def pos=(new_pos) case when new_pos < large_object.str_offset flush relative_pos = new_pos - large_object.str_offset large_object.str_offset += relative_pos when new_pos > sio.string.length flush large_object.str_offset = new_pos else sio.pos = new_pos - large_object.str_offset end end |
#reopen(other) ⇒ Object
42 43 44 45 |
# File 'lib/umbrellio_utils/large_object/writer.rb', line 42 def reopen(other, *) sio.reopen(other.sio.string, *) self end |
#rewind ⇒ Object
61 62 63 64 |
# File 'lib/umbrellio_utils/large_object/writer.rb', line 61 def rewind large_object.str_offset = 0 sio.rewind end |
#tell ⇒ Object Also known as: pos
30 31 32 |
# File 'lib/umbrellio_utils/large_object/writer.rb', line 30 def tell sio.tell + large_object.str_offset end |
#write ⇒ Object Also known as: <<
35 36 37 38 39 |
# File 'lib/umbrellio_utils/large_object/writer.rb', line 35 def write(*) sio.write(*) flush if sio.string.size >= MAX_BUFFER_SIZE self end |