lib/mass_insert/base.rb in mass_insert-0.1.1 vs lib/mass_insert/base.rb in mass_insert-0.1.2
- old
+ new
@@ -1,95 +1,59 @@
module MassInsert
module Base
# = MassInsert
#
- # This method does a mass database insertion just calling it from your
- # ActiveRecord model. Example...
+ # Invoke mass insert just calling this from your ActiveRecord model...
#
# User.mass_insert(values)
#
- # The values should be an array with the object values in a hash.
- # Example...
+ # The values should be an array of hashes. Include attributes and values
+ # in every hash. Example...
#
# values = [
# {:name => "user name", :email => "user email"},
- # {:name => "user name", :email => "user email"},
# {:name => "user name", :email => "user email"}
# ]
#
# == Options
#
- # And MassInset gem allow you to send it some options as second param
- # Example...
+ # MassInset gem allow you to send it options as second param. Example...
#
# User.mass_insert(values, options)
#
- # === table_name
+ # === Primary key
#
- # Default value is the table name to your model. This options rarely
- # needs to change but you can do it if you pass a string with the table
- # name. Example...
+ # By default primary key is ignored. If you wish primary key doesn't
+ # be ignored you need to pass the primary_key option on true.
#
- # options = {:table_name => "users"}
+ # options = {:primary_key => true}
#
- # === primary_key
+ # === Each slice
#
- # Default value is :id. You can change the name of primary key column
- # send it a symbol with the column name.
+ # Due you can get a database timeout error you can specify that the
+ # insertion will be in batches. You need to pass the each_slice option
+ # with the records per batch. Example...
#
- # options = {:primary_key => :post_id}
+ # User.mass_insert(values, :each_slice => 10000)
#
- # === primary_key_mode
- #
- # Default value is :auto. When is :auto MassInsert knows that database
- # will generate the value of the primary key column automatically. If
- # you pass :manual as primary key mode you need to create your value
- # hashes with the key and value of the primary key column.
- #
- # options = {:primary_key_mode => :manual}
- #
- # When a class that inherit from ActiveRecord::Base calls this method
- # is going to extend the methods in ClassMethods module. This module
- # contains some methods that provides some necessary functionality.
- #
- # After extends the class with methods in ClassMethods module. The
- # options that were passed by params are sanitized in the method
- # called mass_insert_options to be passed by params to ProcessControl
- # class. The record values are passed too.
- def mass_insert values, args = {}
- class_eval do
- extend ClassMethods
- end
+ def mass_insert values, options = {}
+ extend ClassMethods
- options = mass_insert_options(args)
- @mass_insert_process = ProcessControl.new(values, options)
+ options[:class_name] ||= self
+ options[:each_slice] ||= false
+ options[:primary_key] ||= false
+
+ @mass_insert_process = Process.new(values, options)
@mass_insert_process.start
end
module ClassMethods
- # Returns an OpenStruc instance where is possible to see the
- # results of MassInsert process. This method calls results method
- # in ProcessControl class. Returns nil if there is not a instance
- # variable with the MassInset process.
+
def mass_insert_results
- @mass_insert_process.results if @mass_insert_process
+ Result.new(@mass_insert_process)
end
-
- private
- # Sanitizes the MassInset options that were passed by params.
- # Prepares default options that come in the class that invokes the
- # mass_insert function and attributes options that were configured
- # and if the options weren't passed, they would be initialized with
- # the default values.
- def mass_insert_options options = {}
- options[:class_name] ||= self
- options[:table_name] ||= self.table_name
- options[:primary_key] ||= :id
- options[:primary_key_mode] ||= :auto
- options
- end
end
end
end