Class: Weechat::Timer

Inherits:
Hook show all
Defined in:
lib/weechat/timer.rb

Instance Attribute Summary collapse

Attributes inherited from Hook

#callback, #id

Attributes included from Pointer

#ptr

Instance Method Summary collapse

Methods inherited from Hook

compute_free_id, find_by_id, #hooked?, hooks, inherited, init, register, #unhook, unhook, unhook_all, unregister

Methods included from Pointer

#==, #hash, #inspect, #to_s

Constructor Details

#initialize(interval, max = 0, align = 0, &block) ⇒ Timer

Returns a new instance of Timer.



6
7
8
9
10
11
12
13
14
15
# File 'lib/weechat/timer.rb', line 6

def initialize(interval, max=0, align=0, &block)
  super
  @remaining= nil
  @callback = EvaluatedCallback.new(block)
  @interval = interval
  @align    = align
  @max      = max
  @ptr      = _init(interval, align, max)
  self.class.register(self)
end

Instance Attribute Details

#alignObject (readonly)

Returns the value of attribute align.



4
5
6
# File 'lib/weechat/timer.rb', line 4

def align
  @align
end

#intervalObject (readonly)

Returns the value of attribute interval.



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

def interval
  @interval
end

#maxObject (readonly)

Returns the value of attribute max.



5
6
7
# File 'lib/weechat/timer.rb', line 5

def max
  @max
end

Instance Method Details

#_init(interval, align, max) ⇒ Object



17
18
19
# File 'lib/weechat/timer.rb', line 17

def _init(interval, align, max)
  Weechat.hook_timer(interval, align, max, "timer_callback", @id.to_s)
end

#call(remaining) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/weechat/timer.rb', line 21

def call(remaining)
  @remaining = remaining.to_i
  ret = super

  if @remaining == 0
    self.unhook
  end

  return ret
end

#restartObject



50
51
52
53
54
# File 'lib/weechat/timer.rb', line 50

def restart
  stop
  @remaining = nil
  start
end

#startObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/weechat/timer.rb', line 36

def start
  unless @hooked
    if @remaining == 0 || @remaining.nil?
      # the timer never ran or finished already. restart it
      max = @max
    else
      # continue running hook
      max = @remaining
    end

    @ptr = _init(@interval, @align, max)
  end
end

#stopObject



32
33
34
# File 'lib/weechat/timer.rb', line 32

def stop
  unhook
end