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
-
.compute_free_id ⇒ Object
Returns a new, unique ID.
- .find_by_id(id) ⇒ Object
- .hooks ⇒ Object (also: all)
- .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
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
#callback ⇒ Object
Returns the value of attribute callback.
26 27 28 |
# File 'lib/weechat/hook.rb', line 26 def callback @callback end |
#id ⇒ Object (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_id ⇒ Object
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 |
.hooks ⇒ Object 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 |
.init ⇒ Object
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_all ⇒ Object
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
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 |