Class: HookR::Hook
- Inherits:
-
Struct
- Object
- Struct
- HookR::Hook
- Includes:
- FailFast::Assertions
- Defined in:
- lib/hookr.rb
Overview
A single named hook
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#params ⇒ Object
Returns the value of attribute params.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#add_basic_callback(handle = nil, &block) ⇒ Object
Add a callback which will pass only the event object to
block
- it will not try to pass arguments as well. - #add_callback(callback) ⇒ Object
-
#add_external_callback(handle = nil, &block) ⇒ Object
Add a callback which will be executed in the context where it was defined.
-
#add_internal_callback(handle = nil, &block) ⇒ Object
Add a callback which will be executed in the context of the event source.
-
#add_method_callback(klass, message) ⇒ Object
Add a callback which will send the given
message
to the event source. - #callbacks ⇒ Object
-
#clear_all_callbacks! ⇒ Object
Empty this hook of its own AND parent callbacks.
-
#clear_callbacks! ⇒ Object
Empty this hook of callbacks.
-
#each_callback(&block) ⇒ Object
Yields callbacks in order of addition, starting with any parent hooks.
-
#each_callback_reverse(&block) ⇒ Object
Yields callbacks in reverse order of addition, starting with own callbacks and then moving on to any parent hooks.
- #eql?(other) ⇒ Boolean
-
#execute_callbacks(event) ⇒ Object
Excute the callbacks in order.
- #hash ⇒ Object
-
#initialize(name, parent = nil, params = []) ⇒ Hook
constructor
A new instance of Hook.
- #initialize_copy(original) ⇒ Object
- #remove_callback(handle_or_index) ⇒ Object
-
#root? ⇒ Boolean
Returns true if this hook has a null parent.
-
#terminal? ⇒ Boolean
Returns false.
-
#total_callbacks ⇒ Object
Callback count including parents.
Constructor Details
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name
287 288 289 |
# File 'lib/hookr.rb', line 287 def name @name end |
#params ⇒ Object
Returns the value of attribute params
287 288 289 |
# File 'lib/hookr.rb', line 287 def params @params end |
#parent ⇒ Object
Returns the value of attribute parent
287 288 289 |
# File 'lib/hookr.rb', line 287 def parent @parent end |
Instance Method Details
#==(other) ⇒ Object
303 304 305 |
# File 'lib/hookr.rb', line 303 def ==(other) name == other.name end |
#add_basic_callback(handle = nil, &block) ⇒ Object
Add a callback which will pass only the event object to block
- it will not try to pass arguments as well.
339 340 341 |
# File 'lib/hookr.rb', line 339 def add_basic_callback(handle=nil, &block) add_block_callback(HookR::BasicCallback, handle, &block) end |
#add_callback(callback) ⇒ Object
354 355 356 357 |
# File 'lib/hookr.rb', line 354 def add_callback(callback) fetch_or_create_callbacks << callback callback.handle end |
#add_external_callback(handle = nil, &block) ⇒ Object
Add a callback which will be executed in the context where it was defined
330 331 332 333 334 335 |
# File 'lib/hookr.rb', line 330 def add_external_callback(handle=nil, &block) if block.arity > -1 && block.arity < params.size raise ArgumentError, "Callback has incompatible arity" end add_block_callback(HookR::ExternalCallback, handle, &block) end |
#add_internal_callback(handle = nil, &block) ⇒ Object
Add a callback which will be executed in the context of the event source
344 345 346 |
# File 'lib/hookr.rb', line 344 def add_internal_callback(handle=nil, &block) add_block_callback(HookR::InternalCallback, handle, &block) end |
#add_method_callback(klass, message) ⇒ Object
Add a callback which will send the given message
to the event source
349 350 351 352 |
# File 'lib/hookr.rb', line 349 def add_method_callback(klass, ) method = klass.instance_method() add_callback(HookR::MethodCallback.new(, method, next_callback_index)) end |
#callbacks ⇒ Object
325 326 327 |
# File 'lib/hookr.rb', line 325 def callbacks fetch_or_create_callbacks.dup.freeze end |
#clear_all_callbacks! ⇒ Object
Empty this hook of its own AND parent callbacks. This also disconnects the hook from its parent, if any.
376 377 378 379 |
# File 'lib/hookr.rb', line 376 def clear_all_callbacks! disconnect! clear_callbacks! end |
#clear_callbacks! ⇒ Object
Empty this hook of callbacks. Parent hooks may still have callbacks.
370 371 372 |
# File 'lib/hookr.rb', line 370 def clear_callbacks! fetch_or_create_callbacks.clear end |
#each_callback(&block) ⇒ Object
Yields callbacks in order of addition, starting with any parent hooks
382 383 384 385 |
# File 'lib/hookr.rb', line 382 def each_callback(&block) parent.each_callback(&block) fetch_or_create_callbacks.each(&block) end |
#each_callback_reverse(&block) ⇒ Object
Yields callbacks in reverse order of addition, starting with own callbacks and then moving on to any parent hooks.
389 390 391 392 |
# File 'lib/hookr.rb', line 389 def each_callback_reverse(&block) fetch_or_create_callbacks.each_reverse(&block) parent.each_callback_reverse(&block) end |
#eql?(other) ⇒ Boolean
307 308 309 |
# File 'lib/hookr.rb', line 307 def eql?(other) self.class == other.class && name == other.name end |
#execute_callbacks(event) ⇒ Object
Excute the callbacks in order. source
is the object initiating the event.
395 396 397 398 399 400 |
# File 'lib/hookr.rb', line 395 def execute_callbacks(event) parent.execute_callbacks(event) fetch_or_create_callbacks.each do |callback| callback.call(event) end end |
#hash ⇒ Object
311 312 313 |
# File 'lib/hookr.rb', line 311 def hash name.hash end |
#initialize_copy(original) ⇒ Object
296 297 298 299 300 301 |
# File 'lib/hookr.rb', line 296 def initialize_copy(original) self.name = original.name self.parent = original self.params = original.params @callbacks = CallbackSet.new end |
#remove_callback(handle_or_index) ⇒ Object
359 360 361 362 363 364 365 366 367 |
# File 'lib/hookr.rb', line 359 def remove_callback(handle_or_index) assert_exists(handle_or_index) case handle_or_index when Symbol then fetch_or_create_callbacks.delete_if{|cb| cb.handle == handle_or_index} when Integer then fetch_or_create_callbacks.delete_if{|cb| cb.index == handle_or_index} else raise ArgumentError, "Key must be integer index or symbolic handle "\ "(was: #{handle_or_index.inspect})" end end |
#root? ⇒ Boolean
Returns true if this hook has a null parent
321 322 323 |
# File 'lib/hookr.rb', line 321 def root? parent.terminal? end |
#terminal? ⇒ Boolean
Returns false. Only true of NullHook.
316 317 318 |
# File 'lib/hookr.rb', line 316 def terminal? false end |
#total_callbacks ⇒ Object
Callback count including parents
403 404 405 |
# File 'lib/hookr.rb', line 403 def total_callbacks fetch_or_create_callbacks.size + parent.total_callbacks end |