module Believer::Counting

Model functionality for counter columns

Public Instance Methods

decr_counter!(name, decr = 1) click to toggle source

Increments a counter and saves the receiver model

# 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?() click to toggle source

Returns true if there are any counter increments or decrements

# 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) click to toggle source

Increments a counter and saves the receiver model

# 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?() click to toggle source

Is this a model with counters?

# File lib/believer/counting.rb, line 22
def is_counter_instance?
  self.class.is_counter_table?
end
reset_counters!() click to toggle source

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

# 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