Class: HookR::Event

Inherits:
Struct
  • Object
show all
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

Instance Method Summary collapse

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments



611
612
613
# File 'lib/hookr.rb', line 611

def arguments
  @arguments
end

#callbacksObject

Returns the value of attribute callbacks



611
612
613
# File 'lib/hookr.rb', line 611

def callbacks
  @callbacks
end

#nameObject

Returns the value of attribute name



611
612
613
# File 'lib/hookr.rb', line 611

def name
  @name
end

#recursiveObject

Returns the value of attribute recursive



611
612
613
# File 'lib/hookr.rb', line 611

def recursive
  @recursive
end

#sourceObject

Returns the value of attribute source



611
612
613
# File 'lib/hookr.rb', line 611

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.



645
646
647
648
649
650
651
652
653
654
# File 'lib/hookr.rb', line 645

def next(*args)
  assert(recursive, callbacks)
  event = self.class.new(source, name, arguments, recursive, callbacks)
  event.arguments = args unless args.empty?
  if callbacks.next?
    callbacks.next.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:

  1. If arity is -1 (meaning any number of arguments), or 4, the result will be [event, arguments[0], arguments[1], arguments[2]]

  2. If arity is 3, the result will just be arguments

  3. 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.



626
627
628
629
630
631
632
633
634
635
636
# File 'lib/hookr.rb', line 626

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