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



604
605
606
# File 'lib/hookr.rb', line 604

def arguments
  @arguments
end

#callbacksObject

Returns the value of attribute callbacks

Returns:

  • (Object)

    the current value of callbacks



604
605
606
# File 'lib/hookr.rb', line 604

def callbacks
  @callbacks
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



604
605
606
# File 'lib/hookr.rb', line 604

def name
  @name
end

#recursiveObject

Returns the value of attribute recursive

Returns:

  • (Object)

    the current value of recursive



604
605
606
# File 'lib/hookr.rb', line 604

def recursive
  @recursive
end

#sourceObject

Returns the value of attribute source

Returns:

  • (Object)

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

  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.



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