Class: HookR::Event
- Inherits:
-
Struct
- Object
- Struct
- HookR::Event
- Includes:
- FailFast::Assertions
- Defined in:
- lib/hookr.rb
Overview
Represents an event which is triggering callbacks.
source
-
The object triggering the event.
name
-
The name of the event
arguments
-
Any arguments passed associated with the event
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Returns the value of attribute arguments.
-
#callbacks ⇒ Object
Returns the value of attribute callbacks.
-
#name ⇒ Object
Returns the value of attribute name.
-
#recursive ⇒ Object
Returns the value of attribute recursive.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
-
#next(*args) ⇒ Object
This method, along with the callback generator defined in Hook, implements recursive callback execution.
-
#to_args(arity) ⇒ Object
Convert to arguments for a callback of the given arity.
Instance Attribute Details
#arguments ⇒ Object
Returns the value of attribute arguments
626 627 628 |
# File 'lib/hookr.rb', line 626 def arguments @arguments end |
#callbacks ⇒ Object
Returns the value of attribute callbacks
626 627 628 |
# File 'lib/hookr.rb', line 626 def callbacks @callbacks end |
#name ⇒ Object
Returns the value of attribute name
626 627 628 |
# File 'lib/hookr.rb', line 626 def name @name end |
#recursive ⇒ Object
Returns the value of attribute recursive
626 627 628 |
# File 'lib/hookr.rb', line 626 def recursive @recursive end |
#source ⇒ Object
Returns the value of attribute source
626 627 628 |
# File 'lib/hookr.rb', line 626 def source @source end |
Instance Method Details
#next(*args) ⇒ Object
This method, along with the callback generator defined in Hook, implements recursive callback execution.
TODO: Consider making the next() automatically if the callback doesn’t call it explicitly.
TODO: Consider adding a cancel() method, implementation TBD.
660 661 662 663 664 665 666 667 668 669 |
# File 'lib/hookr.rb', line 660 def next(*args) assert(recursive, callbacks) event = self.class.new(source, name, arguments, recursive, callbacks) event.arguments = args unless args.empty? if c = callbacks.next c.call(event) else raise "No more callbacks!" end end |
#to_args(arity) ⇒ Object
Convert to arguments for a callback of the given arity. Given an event with three arguments, the rules are as follows:
-
If arity is -1 (meaning any number of arguments), or 4, the result will be [event,
arguments[0]
,arguments[1]
,arguments[2]
] -
If arity is 3, the result will just be
arguments
-
If arity is < 3, an error will be raised.
Notice that as the arity is reduced, the event argument is trimmed off. However, it is not permitted to generate a subset of the arguments
list. If the arity is too small to allow all arguments to be passed, the method fails.
641 642 643 644 645 646 647 648 649 650 651 |
# File 'lib/hookr.rb', line 641 def to_args(arity) case arity when -1 full_arguments when (min_argument_count..full_argument_count) full_arguments.slice(full_argument_count - arity, arity) else raise ArgumentError, "Arity must be between #{min_argument_count} "\ "and #{full_argument_count}" end end |