class ArrayProgressOperation

Attributes

description[RW]
items[RW]
transaction[RW]

Public Class Methods

new(items_, description_, options_) click to toggle source
# File lib/array_with_progress/array_progress_operation.rb, line 8
def initialize(items_, description_, options_)
  self.items = items_
  self.description = description_
  self.transaction = options_[:transaction] || :none
end

Public Instance Methods

run!(&block) click to toggle source
# File lib/array_with_progress/array_progress_operation.rb, line 14
def run!(&block)
  count = self.items.count.to_f
  run_with_transaction(:collection) do
    self.items.each_with_index do |item, index|
      run_with_transaction(:member) do
        progress = index.to_f / count * 100.0
        ArrayProgressItem.run!(self, item, progress, &block)
      end
    end
  end
end

Private Instance Methods

run_with_transaction(transaction_type) { || ... } click to toggle source
# File lib/array_with_progress/array_progress_operation.rb, line 28
def run_with_transaction(transaction_type)
  if self.transaction == transaction_type
    ActiveRecord::Base.transaction do
      yield
    end
  else
    yield
  end
end