Sha256: 4d34e589861f94e4cb538598679b605c903fbdb08eeeaf35e2a1db5a90729181

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

module ActiveGit
  class Synchronizer

    def self.synchronize(*events)
      batch = self.new

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

      batch.run
    end

    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

    def bulk_insert(data)
      bulk_inserts[data.class] << data
    end

    def define_job(&block)
      jobs << block
    end

    private

    def bulk_inserts
      @bulk_inserts ||= Hash.new{|h,k| h[k] = []}
    end

    def jobs
      @jobs ||= []
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_git-0.0.10 lib/active_git/synchronizer.rb
active_git-0.0.9 lib/active_git/synchronizer.rb
active_git-0.0.8 lib/active_git/synchronizer.rb
active_git-0.0.7 lib/active_git/synchronizer.rb