Class: Volt::EventCounter
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
-
#count ⇒ Object
readonly
Returns the value of attribute count.
Instance Method Summary collapse
- #add ⇒ Object
-
#initialize(start, stop) ⇒ EventCounter
constructor
A new instance of EventCounter.
- #remove ⇒ Object
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
#count ⇒ Object (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
#add ⇒ Object
15 16 17 18 19 |
# File 'lib/volt/utils/event_counter.rb', line 15 def add @count += 1 @start.call if @count == 1 end |
#remove ⇒ Object
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 |