Class: Weechat::Hook
Overview
Each hook as an unique ID, which is passed to the middle-man callback, which then calls the appropriate callback.
Direct Known Subclasses
Command, Weechat::Hooks::CommandRun, Weechat::Hooks::Config, Weechat::Hooks::Print, Weechat::Hooks::Signal, Info, Modifier, Process, Timer
Constant Summary collapse
- @@unique_id =
0
Instance Attribute Summary collapse
-
#callback ⇒ Object
Returns the value of attribute callback.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Attributes included from Pointer
Class Method Summary collapse
- .all ⇒ Object
-
.compute_free_id ⇒ Object
Returns a new, unique ID.
- .find_by_id(id) ⇒ Object
- .inherited(by) ⇒ Object
- .init ⇒ Object
- .register(hook) ⇒ Object
-
.unhook(ptr) ⇒ Object
low level unhooking, no checks whatsoever.
-
.unhook_all ⇒ Object
Note: this also unhooks all hooks that were made using the API.
- .unregister(hook) ⇒ Object
Instance Method Summary collapse
- #call(*args) ⇒ Object
-
#hooked? ⇒ Boolean
def to_s @ptr end.
-
#initialize(*args) ⇒ Hook
constructor
A new instance of Hook.
- #unhook(_raise = true) ⇒ Object
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
#callback ⇒ Object
Returns the value of attribute callback.
25 26 27 |
# File 'lib/weechat/hook.rb', line 25 def callback @callback end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
24 25 26 |
# File 'lib/weechat/hook.rb', line 24 def id @id end |
Class Method Details
.all ⇒ Object
15 |
# File 'lib/weechat/hook.rb', line 15 def all; @hooks; end |
.compute_free_id ⇒ Object
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 |
.init ⇒ Object
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_all ⇒ Object
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
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 |