Class: Weechat::Hook

Inherits:
Object show all
Includes:
Pointer
Defined in:
lib/weechat/hook.rb

Overview

Each hook as an unique ID, which is passed to the middle-man callback, which then calls the appropriate callback.

Constant Summary collapse

@@unique_id =
0

Instance Attribute Summary collapse

Attributes included from Pointer

#ptr

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pointer

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

Constructor Details

#initialize(*args) ⇒ Hook

Returns a new instance of Hook.



27
28
29
30
31
32
33
# File 'lib/weechat/hook.rb', line 27

def initialize(*args)
  @id       = self.class.compute_free_id
  @ptr      = nil
  @callback = nil
  @hooked   = true
  self.class.register(self)
end

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



26
27
28
# File 'lib/weechat/hook.rb', line 26

def callback
  @callback
end

#idObject (readonly)

Returns the value of attribute id.



25
26
27
# File 'lib/weechat/hook.rb', line 25

def id
  @id
end

Class Method Details

.compute_free_idObject

Returns a new, unique ID.



86
87
88
# File 'lib/weechat/hook.rb', line 86

def self.compute_free_id
  @@unique_id += 1
end

.find_by_id(id) ⇒ Object



81
82
83
# File 'lib/weechat/hook.rb', line 81

def self.find_by_id(id)
  @hooks[id.to_i]
end

.hooksObject Also known as: all



15
# File 'lib/weechat/hook.rb', line 15

def hooks; @hooks; end

.inherited(by) ⇒ Object



10
11
12
13
# File 'lib/weechat/hook.rb', line 10

def inherited(by)
  by.init
  @hook_classes << by
end

.initObject



18
19
20
# File 'lib/weechat/hook.rb', line 18

def init
  @hooks = {}
end

.register(hook) ⇒ Object



90
91
92
# File 'lib/weechat/hook.rb', line 90

def self.register(hook)
  @hooks[hook.id] = hook
end

.unhook(ptr) ⇒ Object

low level unhooking, no checks whatsoever. Basically used for unhooking foreign hooks.



53
54
55
# File 'lib/weechat/hook.rb', line 53

def self.unhook(ptr)
  Weechat.unhook(ptr)
end

.unhook_allObject

Note: this also unhooks all hooks that were made using the API



58
59
60
61
62
63
# File 'lib/weechat/hook.rb', line 58

def self.unhook_all
  @hook_classes.each do |hook_class|
    hook_class.hooks.values.each {|hook| hook.unhook}
  end
  Weechat.unhook_all
end

.unregister(hook) ⇒ Object



94
95
96
# File 'lib/weechat/hook.rb', line 94

def self.unregister(hook)
  @hooks.delete hook.id
end

Instance Method Details

#call(*args) ⇒ Object



77
78
79
# File 'lib/weechat/hook.rb', line 77

def call(*args)
  return @callback.call(*args)
end

#hooked?Boolean

def to_s

@ptr

end

Returns:



47
48
49
# File 'lib/weechat/hook.rb', line 47

def hooked?
  @hooked
end

#unhook(_raise = true) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/weechat/hook.rb', line 65

def unhook(_raise = true)
  if _raise and !hooked?
    raise "not hooked"
  end

  self.class.unhook(@ptr)
  self.class.unregister(self)
  @callback = nil
  @hooked = false
  true
end