Sha256: 4e2da193b020eaa85790ef07243b15dbd4e1fa0c62f83b78988ca4389e8124f7

Contents?: true

Size: 1.38 KB

Versions: 6

Compression:

Stored size: 1.38 KB

Contents

module Counter::SidekiqReconciliation
  extend ActiveSupport::Concern

  ########################################################## Support for background reconciliation
  def add_item item
    record_counter_change
    enqueue_reconcilitation_job
  end

  def update_item item
    record_counter_change amount: 1
    enqueue_reconcilitation_job
  end

  def remove_item item
    record_counter_change amount: -1
    enqueue_reconcilitation_job
  end

  private

  def record_counter_change amount: 1
    Counter::Change.create! counter: self, increment: amount
  end

  # Enqueue a Sidekiq job
  def enqueue_reconcilitation_job
    Counter::ReconciliationJob.perform_now id
  end

  def filter_item item, on
    filtered_items = []
    filters = @@count_filters[:create] || []
    filters.all? do |filter|
      case filter.class
      when Symbol
        send filter, items
      when Proc
        instance_exec items, filter
      end
    end
  end

  def has_changed? attribute, from: Any.new, to: Any.new
    old_value, new_value = previous_changes[attribute]
    # Return true if the attribute changed at all
    return true if from.instance_of?(Any) && to.instance_of?(Any)

    return new_value == to if from.instance_of?(Any)
    return old_value == from if to.instance_of?(Any)

    old_value == from && new_value == to
  end

  class Any
    include Singleton
    def initialize
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
counterwise-0.1.5 app/models/concerns/counter/sidekiq_reconciliation.rb
counterwise-0.1.4 app/models/concerns/counter/sidekiq_reconciliation.rb
counterwise-0.1.3 app/models/concerns/counter/sidekiq_reconciliation.rb
counterwise-0.1.2 app/models/concerns/counter/sidekiq_reconciliation.rb
counterwise-0.1.1 app/models/concerns/counter/sidekiq_reconciliation.rb
counterwise-0.1.0 app/models/concerns/counter/sidekiq_reconciliation.rb