Class: Infobar::Frequency

Inherits:
Object show all
Defined in:
lib/infobar/frequency.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#calledObject (readonly)

Returns the value of attribute called.



9
10
11
# File 'lib/infobar/frequency.rb', line 9

def called
  @called
end

#durationObject (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

#resetObject



24
25
26
27
28
# File 'lib/infobar/frequency.rb', line 24

def reset
  @update = nil
  @called = 0
  self
end

#to_sObject



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