class EventCounter

This class defines model that stores all counters.

Public Class Methods

change(val = 1, vector: :up, on_time: nil, force: nil) click to toggle source
# File lib/event_counter.rb, line 50
def self.change(val = 1, vector: :up, on_time: nil, force: nil)
  counter_error!(:direction) unless [:up, :down].include?(vector)

  val ||= 1

  on_time = normalize_on_time!(on_time)

  counter = where(created_at: on_time).first

  return counter.update!(vector, val, force) if counter

  val = -val if vector == :down
  make(val, on_time: on_time, force: force)
end
counter_error!(*args) click to toggle source
# File lib/event_counter.rb, line 86
def self.counter_error!(*args)
  fail CounterError, args
end
counter_name() click to toggle source
# File lib/event_counter.rb, line 46
def self.counter_name
  scoped_relation.proxy_association.reflection.name
end
current_interval() click to toggle source
# File lib/event_counter.rb, line 42
def self.current_interval
  scoped_relation.proxy_association.owner.event_counters[counter_name]
end
down!(*args) click to toggle source
# File lib/event_counter.rb, line 82
def self.down!(*args)
  change(:down, *args)
end
make(val = 1, on_time: nil, force: false) click to toggle source
# File lib/event_counter.rb, line 29
def self.make(val = 1, on_time: nil, force: false)
  on_time = normalize_on_time!(on_time)

  attrs = { created_at: on_time }

  if force && (found = scoped_relation.where(attrs).first)
    found.reset_value(val)
  else
    attrs.merge!(value: val)
    scoped_relation.create!(attrs)
  end
end
normalize_on_time!(on_time) click to toggle source
# File lib/event_counter.rb, line 90
def self.normalize_on_time!(on_time)
  on_time ||= Time.zone.now

  counter_error!(:time_zone) unless on_time.is_a?(ActiveSupport::TimeWithZone)

  on_time =
    case current_interval
    when Symbol
      on_time.in_time_zone.send(:"beginning_of_#{current_interval}")
    else
      on_time.in_time_zone.floor(current_interval)
    end
  on_time
end
scoped_relation() click to toggle source
# File lib/event_counter.rb, line 74
def self.scoped_relation
  ActiveRecord::VERSION::MAJOR > 3 ? where(nil) : scoped
end
up!(*args) click to toggle source
# File lib/event_counter.rb, line 78
def self.up!(*args)
  change(:up, *args)
end

Public Instance Methods

decrease_by(decrement) click to toggle source
# File lib/event_counter.rb, line 17
def decrease_by(decrement)
  self.class.where(id: id).update_all(['value = value - ?', decrement])
  decrement(:value, decrement)
  self
end
increase_by(val) click to toggle source
# File lib/event_counter.rb, line 11
def increase_by(val)
  self.class.where(id: id).update_all(['value = value + ?', val])
  increment(:value, val)
  self
end
reset_value(val = 0) click to toggle source
# File lib/event_counter.rb, line 23
def reset_value(val = 0)
  self.class.where(id: id).update_all(['value = ?', val])
  self.value = val
  self
end
update!(vector, val = 1, force = false) click to toggle source
# File lib/event_counter.rb, line 65
def update!(vector, val = 1, force = false)
  if force
    val = -val if vector == :down
    reset_value(val)
  else
    vector == :up ? increase_by(val) : decrease_by(val)
  end
end