Class: EventCounter

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
EventCounterVersion
Defined in:
lib/event_counter.rb,
lib/event_counter/active_record_extension.rb

Overview

This class defines model that stores all counters.

Defined Under Namespace

Modules: ActiveRecordExtension Classes: CounterError

Constant Summary

Constants included from EventCounterVersion

EventCounterVersion::VERSION

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.change(val = 1, vector: :up, on_time: nil, force: nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object



86
87
88
# File 'lib/event_counter.rb', line 86

def self.counter_error!(*args)
  fail CounterError, args
end

.counter_nameObject



46
47
48
# File 'lib/event_counter.rb', line 46

def self.counter_name
  scoped_relation.proxy_association.reflection.name
end

.current_intervalObject



42
43
44
# File 'lib/event_counter.rb', line 42

def self.current_interval
  scoped_relation.proxy_association.owner.event_counters[counter_name]
end

.down!(*args) ⇒ Object



82
83
84
# File 'lib/event_counter.rb', line 82

def self.down!(*args)
  change(:down, *args)
end

.make(val = 1, on_time: nil, force: false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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_relationObject



74
75
76
# File 'lib/event_counter.rb', line 74

def self.scoped_relation
  ActiveRecord::VERSION::MAJOR > 3 ? where(nil) : scoped
end

.up!(*args) ⇒ Object



78
79
80
# File 'lib/event_counter.rb', line 78

def self.up!(*args)
  change(:up, *args)
end

Instance Method Details

#decrease_by(decrement) ⇒ Object



17
18
19
20
21
# 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) ⇒ Object



11
12
13
14
15
# 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) ⇒ Object



23
24
25
26
27
# 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) ⇒ Object



65
66
67
68
69
70
71
72
# 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