Class: OpenHAB::DSL::Timer

Inherits:
Object
  • Object
show all
Includes:
MockedZonedDateTime
Defined in:
lib/rspec/openhab/dsl/timers/timer.rb

Defined Under Namespace

Modules: MockedZonedDateTime, TimeCopStackItem

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MockedZonedDateTime

#now

Constructor Details

#initialize(duration:, thread_locals: {}, &block) ⇒ Timer

rubocop:disable Lint/UnusedMethodArgument



33
34
35
36
# File 'lib/rspec/openhab/dsl/timers/timer.rb', line 33

def initialize(duration:, thread_locals: {}, &block) # rubocop:disable Lint/UnusedMethodArgument
  @block = block
  reschedule(duration)
end

Instance Attribute Details

#execution_timeObject (readonly)

Returns the value of attribute execution_time.



31
32
33
# File 'lib/rspec/openhab/dsl/timers/timer.rb', line 31

def execution_time
  @execution_time
end

Instance Method Details

#active?Boolean Also known as: is_active

Returns:

  • (Boolean)


74
75
76
# File 'lib/rspec/openhab/dsl/timers/timer.rb', line 74

def active?
  !terminated?
end

#cancelObject



55
56
57
58
59
60
# File 'lib/rspec/openhab/dsl/timers/timer.rb', line 55

def cancel
  Timers.timer_manager.delete(self)
  @executed = false
  @cancelled = true
  true
end

#cancelled?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/rspec/openhab/dsl/timers/timer.rb', line 62

def cancelled?
  @cancelled
end

#executeObject



46
47
48
49
50
51
52
53
# File 'lib/rspec/openhab/dsl/timers/timer.rb', line 46

def execute
  raise "Timer already cancelled" if cancelled?
  raise "Timer already executed" if terminated?

  @block.call(self)
  Timers.timer_manager.delete(self)
  @executed = true
end

#reschedule(duration = nil) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/rspec/openhab/dsl/timers/timer.rb', line 38

def reschedule(duration = nil)
  @duration = duration || @duration
  @execution_time = ::OpenHAB::DSL.to_zdt(@duration)
  @executed = @cancelled = false

  Timers.timer_manager.add(self)
end

#running?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/rspec/openhab/dsl/timers/timer.rb', line 70

def running?
  active? && @execution_time > ZonedDateTime.now
end

#terminated?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/rspec/openhab/dsl/timers/timer.rb', line 66

def terminated?
  @executed || @cancelled
end