Class: ActiveRecordImportQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord-import-queue.rb

Overview

ActiveRecordImportQueue will maintain a list of ActiveRecord objects and use the activerecord-import ‘.import’ extension to import them in to a database in bulk.

Instance Method Summary collapse

Constructor Details

#initialize(params = { :limit => 100 }) ⇒ ActiveRecordImportQueue

Create a new instance of the import queue.

Specify ‘limit=n’ to limit the number of queued records to the value ‘n’. This limit will be checked each time an object is queued.



20
21
22
23
24
# File 'lib/activerecord-import-queue.rb', line 20

def initialize(params={ :limit => 100 })
  @pending = {}
  @count = 0
  @params = params
end

Instance Method Details

#<<(obj) ⇒ Object

To queue a new object, use the ‘<<’ method to push an ActiveRecord object on to the queue.



43
44
45
46
47
48
# File 'lib/activerecord-import-queue.rb', line 43

def <<(obj)
  @pending[obj.class] ||= []
  @pending[obj.class] << obj
  incr_count
  check_depth
end

#count_pendingObject

Returns the total number of pending objects.



32
33
34
# File 'lib/activerecord-import-queue.rb', line 32

def count_pending
  @count
end

#finishObject

Imports all queued objects irrespective of whether the queue limit has been reached. This should be used at the end of an import run to insert remaining objects in to the database.



58
59
60
# File 'lib/activerecord-import-queue.rb', line 58

def finish
  @pending.keys.collect { |klass| process_class(klass) }
end