Method: Tire::Index#import

Defined in:
lib/tire/index.rb

#import(klass_or_collection, options = {}) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/tire/index.rb', line 284

def import(klass_or_collection, options={})
  case
    when method = options.delete(:method)
      options = {:page => 1, :per_page => 1000}.merge options
      while (documents = klass_or_collection.send(method.to_sym, options.merge(:page => options[:page]))) \
                        && documents.to_a.length > 0

        documents = yield documents if block_given?

        bulk_store documents, options
        GC.start
        options[:page] += 1
      end

    when klass_or_collection.respond_to?(:map)
      documents = block_given? ? yield(klass_or_collection) : klass_or_collection
      bulk_store documents, options

    else
      raise ArgumentError, "Please pass either an Enumerable compatible class, or a collection object " +
                           "with a method for fetching records in batches (such as 'paginate')."
  end
end