Class: Linkage::ImportBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/linkage/import_buffer.rb

Instance Method Summary collapse

Constructor Details

#initialize(dataset, headers, limit = 1000) ⇒ ImportBuffer

Returns a new instance of ImportBuffer

Parameters:

  • dataset (Sequel::Dataset)
  • headers (Array<Symbol>)

    List of fields you want to insert

  • limit (Fixnum) (defaults to: 1000)

    Number of records to insert at a time



6
7
8
9
10
11
# File 'lib/linkage/import_buffer.rb', line 6

def initialize(dataset, headers, limit = 1000)
  @dataset = dataset
  @headers = headers
  @limit = limit
  @values = []
end

Instance Method Details

#add(values) ⇒ Object



13
14
15
16
17
18
# File 'lib/linkage/import_buffer.rb', line 13

def add(values)
  @values << values
  if @values.length == @limit
    flush
  end
end

#flushObject



20
21
22
23
24
25
26
# File 'lib/linkage/import_buffer.rb', line 20

def flush
  return if @values.empty?
  @dataset.db.synchronize do
    @dataset.import(@headers, @values)
    @values.clear
  end
end