Method: ModelIterator#initialize

Defined in:
lib/model_iterator.rb

#initialize(klass, *args) ⇒ ModelIterator

Initializes a ModelIterator instance.

klass - ActiveRecord::Base class to iterate. clause - String SQL WHERE clause, with ‘?’ placeholders for values. *values - Optional array of values to be added to a custom SQL WHERE

clause.

options - Optional Hash options.

        :redis     - A Redis object for storing the state.
        :order     - Symbol specifying the order to iterate.  :asc or
                     :desc.  Default: :asc
        :id_field  - String name of the ID column.  Default: "id"
        :id_clause - String name of the fully qualified ID column.
                     Prepends the model's table name to the front of
                     the ID field.  Default: "table_name.id"
        :start_id  - Fixnum to start iterating from.  Default: 1
        :prefix    - Custom String prefix for redis keys.
        :select    - Optional String of the columns to retrieve.
        :joins     - Optional Symbol or Hash :joins option for 
                     ActiveRecord::Base.find.
        :max       - Optional Fixnum of the maximum number of iterations.
                     Use max * limit to process a known number of records
                     at a time.
        :limit     - Fixnum limit of objects to fetch from the db.
                     Default: 100
        :conditions - Array of String SQL WHERE clause and optional values
                      (Will override clause/values given in arguments.)

ModelIterator.new(Repository, :start_id => 5000)
ModelIterator.new(Repository, 'public=?', true, :start_id => 1000)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/model_iterator.rb', line 92

def initialize(klass, *args)
  @klass = klass
  @options = if args.last.respond_to?(:fetch)
    args.pop
  else
    {}
  end
  @redis = @options[:redis] || self.class.redis
  @id_field = @options[:id_field] || klass.primary_key
  @id_clause = @options[:id_clause] || "#{klass.table_name}.#{@id_field}"
  @order = @options[:order] == :desc ? :desc : :asc
  op = @order == :asc ? '>' : '<'
  @max = @options[:max].to_i
  @joins = @options[:joins]
  @clause =  "#{@id_clause} #{op} ?"
  if @options[:conditions]
    conditions = Array(@options[:conditions])
    @clause += " AND (#{conditions.first})"
    @clause_args = conditions[1..-1]
  elsif !args.empty?
    @clause += " AND (#{args.shift})"
    @clause_args = args
  end
  @current_id = @options[:start_id]
  @limit = @options[:limit] || 100
  @job = @prefix = @key = nil
end