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
604 605 606 |
# File 'lib/hookr.rb', line 604 def arguments @arguments end |
#callbacks ⇒ Object
Returns the value of attribute callbacks
604 605 606 |
# File 'lib/hookr.rb', line 604 def callbacks @callbacks end |
#name ⇒ Object
Returns the value of attribute name
604 605 606 |
# File 'lib/hookr.rb', line 604 def name @name end |
#recursive ⇒ Object
Returns the value of attribute recursive
604 605 606 |
# File 'lib/hookr.rb', line 604 def recursive @recursive end |
#source ⇒ Object
Returns the value of attribute source
604 605 606 |
# File 'lib/hookr.rb', line 604 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.
638 639 640 641 642 643 644 645 646 647 |
# File 'lib/hookr.rb', line 638 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.
619 620 621 622 623 624 625 626 627 628 629 |
# File 'lib/hookr.rb', line 619 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 |