Class: Weechat::Hook

Inherits:
Object
  • 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, included, #inspect, #to_s

Constructor Details

#initialize(*args) ⇒ Hook

Returns a new instance of Hook.



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

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.



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

def callback
  @callback
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

Class Method Details

.allObject



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

def all; @hooks; end

.compute_free_idObject

Returns a new, unique ID.



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

def self.compute_free_id
  @@unique_id += 1
end

.find_by_id(id) ⇒ Object



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

def self.find_by_id(id)
  @hooks[id.to_i]
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



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

def init
  @hooks = {}
end

.register(hook) ⇒ Object



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

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

.unhook(ptr) ⇒ Object

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



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

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

.unhook_allObject

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



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

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



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

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

Instance Method Details

#call(*args) ⇒ Object



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

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

#hooked?Boolean

def to_s

@ptr

end

Returns:



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

def hooked?
  @hooked
end

#unhook(_raise = true) ⇒ Object



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

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