class ActiveGit::Synchronizer

Public Class Methods

synchronize(*events) click to toggle source
# File lib/active_git/synchronizer.rb, line 4
def self.synchronize(*events)
  batch = self.new

  Array(events).flatten.each do |event|
    event.synchronize batch
  end

  batch.run
end

Public Instance Methods

bulk_insert(data) click to toggle source
# File lib/active_git/synchronizer.rb, line 34
def bulk_insert(data)
  bulk_inserts[data.class] << data
end
define_job(&block) click to toggle source
# File lib/active_git/synchronizer.rb, line 38
def define_job(&block)
  jobs << block
end
run() click to toggle source
# File lib/active_git/synchronizer.rb, line 14
def run
  if bulk_inserts.any?
    define_job do
      bulk_inserts.each do |model, records|
        records.each_slice(ActiveGit.configuration.sync_batch_size) do |batch_records|
          ActiveGit.configuration.logger.debug "[ActiveGit] Inserting #{model.model_name} models"
          import_result = model.import batch_records, timestamps: false, validate: false
          raise SynchronizationError.new(import_result.failed_instances) unless import_result.failed_instances.empty?
        end
      end
    end
  end

  ::ActiveRecord::Base.transaction do
    jobs.each(&:call)
  end

  ActiveGit.add_all
end

Private Instance Methods

bulk_inserts() click to toggle source
# File lib/active_git/synchronizer.rb, line 44
def bulk_inserts
  @bulk_inserts ||= Hash.new{|h,k| h[k] = []}
end
jobs() click to toggle source
# File lib/active_git/synchronizer.rb, line 48
def jobs
  @jobs ||= []
end