Class: ActiveRecordCopy::EncodeForCopy

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord-copy/encode_for_copy.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ EncodeForCopy

Returns a new instance of EncodeForCopy.



6
7
8
9
10
11
12
# File 'lib/activerecord-copy/encode_for_copy.rb', line 6

def initialize(options = {})
  @options = options
  @closed = false
  @column_types = @options[:column_types] || {}
  @io = nil
  @buffer = TempBuffer.new
end

Instance Method Details

#add(row) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/activerecord-copy/encode_for_copy.rb', line 14

def add(row)
  setup_io unless @io
  @io.write([row.size].pack(PACKED_UINT_16))
  row.each_with_index do |col, index|
    encode_field(@buffer, col, index)
    next if @buffer.empty?
    @io.write(@buffer.read)
    @buffer.reopen
  end
end

#closeObject



25
26
27
28
29
30
31
32
33
# File 'lib/activerecord-copy/encode_for_copy.rb', line 25

def close
  @closed = true
  unless @buffer.empty?
    @io.write(@buffer.read)
    @buffer.reopen
  end
  @io.write([-1].pack(PACKED_UINT_16)) rescue raise Exception, 'No rows have been added to the encoder!'
  @io.rewind
end

#get_ioObject



35
36
37
38
# File 'lib/activerecord-copy/encode_for_copy.rb', line 35

def get_io
  close unless @closed
  @io
end

#removeObject



40
41
42
43
44
45
# File 'lib/activerecord-copy/encode_for_copy.rb', line 40

def remove
  return unless @io.is_a?(Tempfile)

  @io.close
  @io.unlink
end