class UnderOs::Timer

Attributes

_[R]
block[R]
counter[R]
interval[R]
repeats[R]

Public Class Methods

every(duration, options={}, &block) click to toggle source
# 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) click to toggle source
# 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
new(seconds, options={}, &block) click to toggle source
# 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

Public Instance Methods

kick() click to toggle source
# File lib/under_os/timer.rb, line 29
def kick
  @block.call
  stop if @counter && (@counter -= 1) <= 0
end
stop() click to toggle source
# File lib/under_os/timer.rb, line 24
def stop
  @_.invalidate
  self
end