Class: Infobar::Frequency
Instance Attribute Summary collapse
-
#called ⇒ Object
readonly
Returns the value of attribute called.
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
Instance Method Summary collapse
- #call(&block) ⇒ Object
-
#initialize(duration) ⇒ Frequency
constructor
A new instance of Frequency.
- #reset ⇒ Object
- #to_s ⇒ Object
- #update(now: Time.now) ⇒ Object
Constructor Details
#initialize(duration) ⇒ Frequency
Returns a new instance of Frequency.
2 3 4 5 |
# File 'lib/infobar/frequency.rb', line 2 def initialize(duration) @duration = duration.to_f @called = 0 end |
Instance Attribute Details
#called ⇒ Object (readonly)
Returns the value of attribute called.
9 10 11 |
# File 'lib/infobar/frequency.rb', line 9 def called @called end |
#duration ⇒ Object (readonly)
Returns the value of attribute duration.
7 8 9 |
# File 'lib/infobar/frequency.rb', line 7 def duration @duration end |
Instance Method Details
#call(&block) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/infobar/frequency.rb', line 16 def call(&block) now = Time.now if !@update || now - @update > @duration update now: now block.call end end |
#reset ⇒ Object
24 25 26 27 28 |
# File 'lib/infobar/frequency.rb', line 24 def reset @update = nil @called = 0 self end |
#to_s ⇒ Object
30 31 32 |
# File 'lib/infobar/frequency.rb', line 30 def to_s @duration.to_s end |
#update(now: Time.now) ⇒ Object
11 12 13 14 |
# File 'lib/infobar/frequency.rb', line 11 def update(now: Time.now) @update = now @called += 1 end |