Module: Believer::Counting

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/believer/counting.rb

Overview

Model functionality for counter columns

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#decr_counter!(name, decr = 1) ⇒ Object

Increments a counter and saves the receiver model



53
54
55
56
57
58
# File 'lib/believer/counting.rb', line 53

def decr_counter!(name, decr = 1)
  counter = self.send(name.to_sym)
  counter.decr(val)
  save
  counter.reconcile!
end

#has_counter_diffs?Boolean

Returns true if there are any counter increments or decrements

Returns:

  • (Boolean)


27
28
29
30
31
32
# File 'lib/believer/counting.rb', line 27

def has_counter_diffs?
  self.class.counter_columns.any? do |col|
    counter = self.send(col.name)
    counter && counter.diff > 0
  end
end

#incr_counter!(name, val = 1) ⇒ Object

Increments a counter and saves the receiver model



45
46
47
48
49
50
# File 'lib/believer/counting.rb', line 45

def incr_counter!(name, val = 1)
  counter = self.send(name.to_sym)
  counter.incr(val)
  save
  counter.reconcile!
end

#is_counter_instance?Boolean

Is this a model with counters?

Returns:

  • (Boolean)


22
23
24
# File 'lib/believer/counting.rb', line 22

def is_counter_instance?
  self.class.is_counter_table?
end

#reset_counters!Object

Reloads from DB, resets all counters and saves, which effectively resets all counters



35
36
37
38
39
40
41
42
# File 'lib/believer/counting.rb', line 35

def reset_counters!
  reload!
  self.class.counter_columns.each do |cc|
    counter = self.send(cc.name)
    counter.reset! unless counter.nil?
  end
  save
end