class ActiveRecordImportQueue
ActiveRecordImportQueue
will maintain a list of ActiveRecord objects and use the activerecord-import ‘.import’ extension to import them in to a database in bulk.
Public Class Methods
new(params={ :limit => 100 })
click to toggle source
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.
# File lib/activerecord-import-queue.rb, line 20 def initialize(params={ :limit => 100 }) @pending = {} @count = 0 @params = params end
Public Instance Methods
<<(obj)
click to toggle source
To queue a new object, use the ‘<<’ method to push an ActiveRecord object on to the queue.
# File lib/activerecord-import-queue.rb, line 43 def <<(obj) @pending[obj.class] ||= [] @pending[obj.class] << obj incr_count check_depth end
count_pending()
click to toggle source
Returns the total number of pending objects.
# File lib/activerecord-import-queue.rb, line 32 def count_pending @count end
finish()
click to toggle source
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.
# File lib/activerecord-import-queue.rb, line 58 def finish @pending.keys.collect { |klass| process_class(klass) } end
Private Instance Methods
check_depth()
click to toggle source
# File lib/activerecord-import-queue.rb, line 77 def check_depth @pending.keys.collect { |klass| process_class(klass) if @pending[klass].count > @params[:limit] } end
decr_count()
click to toggle source
# File lib/activerecord-import-queue.rb, line 68 def decr_count @count -= 1 end
incr_count()
click to toggle source
# File lib/activerecord-import-queue.rb, line 64 def incr_count @count += 1 end
process_class(klass)
click to toggle source
# File lib/activerecord-import-queue.rb, line 72 def process_class(klass) klass.import(@pending[klass]) @pending[klass] = [] end