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
309 310 311 |
# File 'lib/hookr.rb', line 309 def name @name end |
#params ⇒ Object
Returns the value of attribute params
309 310 311 |
# File 'lib/hookr.rb', line 309 def params @params end |
#parent ⇒ Object
Returns the value of attribute parent
309 310 311 |
# File 'lib/hookr.rb', line 309 def parent @parent end |
Instance Method Details
#==(other) ⇒ Object
325 326 327 |
# File 'lib/hookr.rb', line 325 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.
361 362 363 |
# File 'lib/hookr.rb', line 361 def add_basic_callback(handle=nil, &block) add_block_callback(HookR::BasicCallback, handle, &block) end |
#add_callback(callback) ⇒ Object
376 377 378 379 |
# File 'lib/hookr.rb', line 376 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
352 353 354 355 356 357 |
# File 'lib/hookr.rb', line 352 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
366 367 368 |
# File 'lib/hookr.rb', line 366 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
371 372 373 374 |
# File 'lib/hookr.rb', line 371 def add_method_callback(klass, ) method = klass.instance_method() add_callback(HookR::MethodCallback.new(, method, next_callback_index)) end |
#callbacks ⇒ Object
347 348 349 |
# File 'lib/hookr.rb', line 347 def callbacks fetch_or_create_callbacks.dup end |
#clear_all_callbacks! ⇒ Object
Empty this hook of its own AND parent callbacks. This also disconnects the hook from its parent, if any.
398 399 400 401 |
# File 'lib/hookr.rb', line 398 def clear_all_callbacks! disconnect! clear_callbacks! end |
#clear_callbacks! ⇒ Object
Empty this hook of callbacks. Parent hooks may still have callbacks.
392 393 394 |
# File 'lib/hookr.rb', line 392 def clear_callbacks! fetch_or_create_callbacks.clear end |
#each_callback(&block) ⇒ Object
Yields callbacks in order of addition, starting with any parent hooks
404 405 406 407 |
# File 'lib/hookr.rb', line 404 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.
411 412 413 414 |
# File 'lib/hookr.rb', line 411 def each_callback_reverse(&block) fetch_or_create_callbacks.each_reverse(&block) parent.each_callback_reverse(&block) end |
#eql?(other) ⇒ Boolean
329 330 331 |
# File 'lib/hookr.rb', line 329 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.
417 418 419 420 421 422 |
# File 'lib/hookr.rb', line 417 def execute_callbacks(event) parent.execute_callbacks(event) fetch_or_create_callbacks.each do |callback| callback.call(event) end end |
#hash ⇒ Object
333 334 335 |
# File 'lib/hookr.rb', line 333 def hash name.hash end |
#initialize_copy(original) ⇒ Object
318 319 320 321 322 323 |
# File 'lib/hookr.rb', line 318 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
381 382 383 384 385 386 387 388 389 |
# File 'lib/hookr.rb', line 381 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
343 344 345 |
# File 'lib/hookr.rb', line 343 def root? parent.terminal? end |
#terminal? ⇒ Boolean
Returns false. Only true of NullHook.
338 339 340 |
# File 'lib/hookr.rb', line 338 def terminal? false end |
#total_callbacks ⇒ Object
Callback count including parents
425 426 427 |
# File 'lib/hookr.rb', line 425 def total_callbacks fetch_or_create_callbacks.size + parent.total_callbacks end |