Class: ActiveRecordImportQueue
- Inherits:
-
Object
- Object
- ActiveRecordImportQueue
- 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
-
#<<(obj) ⇒ Object
To queue a new object, use the ‘<<’ method to push an ActiveRecord object on to the queue.
-
#count_pending ⇒ Object
Returns the total number of pending objects.
-
#finish ⇒ Object
Imports all queued objects irrespective of whether the queue limit has been reached.
-
#initialize(params = { :limit => 100 }) ⇒ ActiveRecordImportQueue
constructor
Create a new instance of the import queue.
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_pending ⇒ Object
Returns the total number of pending objects.
32 33 34 |
# File 'lib/activerecord-import-queue.rb', line 32 def count_pending @count end |
#finish ⇒ Object
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 |