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


6
7
8
9
# File 'lib/dizby/utility/timed_state.rb', line 6

def initialize(timeout)
  @timeout = timeout
  revive
end

Instance Method Details

#alive?Boolean


20
21
22
# File 'lib/dizby/utility/timed_state.rb', line 20

def alive?
  @state != :dead
end

#progress (private)


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

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

#revive


24
25
26
27
28
# File 'lib/dizby/utility/timed_state.rb', line 24

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

#update


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

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

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