Class: Dizby::TimedState

Inherits:
Object
  • Object
show all
Defined in:
lib/dizby/utility/timed_state.rb

Instance Method Summary collapse

Constructor Details

#initialize(timeout) ⇒ TimedState

Returns a new instance of TimedState.


4
5
6
7
# File 'lib/dizby/utility/timed_state.rb', line 4

def initialize(timeout)
  @timeout = timeout
  revive
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)

18
19
20
# File 'lib/dizby/utility/timed_state.rb', line 18

def alive?
  @state != :dead
end

#progress (private)


30
31
32
33
34
35
36
37
38
39
# File 'lib/dizby/utility/timed_state.rb', line 30

def progress
  @time = 0
  @state =
    case @state
    when :active
      :inactive
    when :inactive
      :dead
    end
end

#revive


22
23
24
25
26
# File 'lib/dizby/utility/timed_state.rb', line 22

def revive
  @state = :active
  @time = 0
  @last_update = Time.now.utc
end

#update


9
10
11
12
13
14
15
16
# File 'lib/dizby/utility/timed_state.rb', line 9

def update
  previous = @last_update
  @last_update = Time.now.utc
  timediff = (@last_update - previous) * 1000

  @time += timediff
  progress if @time >= @timeout
end