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

Returns:

  • (Object)

    the current value of arguments



626
627
628
# File 'lib/hookr.rb', line 626

def arguments
  @arguments
end

#callbacksObject

Returns the value of attribute callbacks

Returns:

  • (Object)

    the current value of callbacks



626
627
628
# File 'lib/hookr.rb', line 626

def callbacks
  @callbacks
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



626
627
628
# File 'lib/hookr.rb', line 626

def name
  @name
end

#recursiveObject

Returns the value of attribute recursive

Returns:

  • (Object)

    the current value of recursive



626
627
628
# File 'lib/hookr.rb', line 626

def recursive
  @recursive
end

#sourceObject

Returns the value of attribute source

Returns:

  • (Object)

    the current value of 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:

  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.



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