Class: UnderOs::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/under_os/timer.rb

Defined Under Namespace

Classes: Duration

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seconds, options = {}, &block) ⇒ Timer

Returns a new instance of Timer.



14
15
16
17
18
19
20
21
22
# File 'lib/under_os/timer.rb', line 14

def initialize(seconds, options={}, &block)
  @block    = block
  @counter  = options[:repeat].to_i if options[:repeat].is_a?(Numeric)
  @interval = seconds
  @repeats  = options[:repeat] != false

  @_ = NSTimer.scheduledTimerWithTimeInterval @interval,
    target: self, selector: :kick, userInfo: nil, repeats: @repeats
end

Instance Attribute Details

#_Object (readonly)

Returns the value of attribute _.



12
13
14
# File 'lib/under_os/timer.rb', line 12

def _
  @_
end

#blockObject (readonly)

Returns the value of attribute block.



12
13
14
# File 'lib/under_os/timer.rb', line 12

def block
  @block
end

#counterObject (readonly)

Returns the value of attribute counter.



12
13
14
# File 'lib/under_os/timer.rb', line 12

def counter
  @counter
end

#intervalObject (readonly)

Returns the value of attribute interval.



12
13
14
# File 'lib/under_os/timer.rb', line 12

def interval
  @interval
end

#repeatsObject (readonly)

Returns the value of attribute repeats.



12
13
14
# File 'lib/under_os/timer.rb', line 12

def repeats
  @repeats
end

Class Method Details

.every(duration, options = {}, &block) ⇒ Object



7
8
9
10
# File 'lib/under_os/timer.rb', line 7

def self.every(duration, options={}, &block)
  duration = Duration.new(duration) if duration.is_a?(Numeric)
  new duration.to_f, options.merge(repeat: true), &block
end

.in(duration, options = {}, &block) ⇒ Object



2
3
4
5
# File 'lib/under_os/timer.rb', line 2

def self.in(duration, options={}, &block)
  duration = Duration.new(duration) if duration.is_a?(Numeric)
  new duration.to_f, options.merge(repeat: false), &block
end

Instance Method Details

#kickObject



29
30
31
32
# File 'lib/under_os/timer.rb', line 29

def kick
  @block.call
  stop if @counter && (@counter -= 1) <= 0
end

#stopObject



24
25
26
27
# File 'lib/under_os/timer.rb', line 24

def stop
  @_.invalidate
  self
end