Class: UmbrellioUtils::LargeObject::Writer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_objectObject (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

#flushObject



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

#rewindObject



61
62
63
64
# File 'lib/umbrellio_utils/large_object/writer.rb', line 61

def rewind
  large_object.str_offset = 0
  sio.rewind
end

#tellObject 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

#writeObject 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