Class: Linkage::ImportBuffer

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

Instance Method Summary collapse

Constructor Details

#initialize(uri, table_name, headers, options = {}, limit = 1000) ⇒ ImportBuffer

Returns a new instance of ImportBuffer.

Parameters:

  • uri (String)

    Sequel-style URI

  • table_name (Symbol, String)
  • headers (Array<Symbol>)

    List of fields you want to insert

  • options (Hash) (defaults to: {})

    Sequel.connect options

  • limit (Fixnum) (defaults to: 1000)

    Number of records to insert at a time



8
9
10
11
12
13
14
15
# File 'lib/linkage/import_buffer.rb', line 8

def initialize(uri, table_name, headers, options = {}, limit = 1000)
  @uri = uri
  @table_name = table_name.to_sym
  @headers = headers
  @options = options
  @limit = limit
  @values = []
end

Instance Method Details

#add(values) ⇒ Object



17
18
19
20
21
22
# File 'lib/linkage/import_buffer.rb', line 17

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

#flushObject



24
25
26
27
28
29
30
31
# File 'lib/linkage/import_buffer.rb', line 24

def flush
  return if @values.empty?
  database do |db|
    ds = db[@table_name]
    ds.import(@headers, @values)
    @values.clear
  end
end