Class: Volt::EventCounter

Inherits:
Object show all
Defined in:
lib/volt/utils/event_counter.rb

Overview

EventCounter has an #add and #remove method, and when the first one is added will call the #start proc (passed to new), and when the last is removed will call #stop.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start, stop) ⇒ EventCounter

Returns a new instance of EventCounter.



8
9
10
11
12
13
# File 'lib/volt/utils/event_counter.rb', line 8

def initialize(start, stop)
  @start = start
  @stop = stop

  @count = 0
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



6
7
8
# File 'lib/volt/utils/event_counter.rb', line 6

def count
  @count
end

Instance Method Details

#addObject



15
16
17
18
19
# File 'lib/volt/utils/event_counter.rb', line 15

def add
  @count += 1

  @start.call if @count == 1
end

#removeObject



21
22
23
24
25
26
27
# File 'lib/volt/utils/event_counter.rb', line 21

def remove
  @count -= 1

  fail 'count below 0' if @count < 0

  @stop.call if @count == 0
end