class Events::Event

An event that can be subscribed to, and runs any subscribed callback when it is fired.

Attributes

callbacks[RW]

Gets the collection of callbacks subscribed to this event.

Public Class Methods

new() click to toggle source

Class initializer

# File lib/events.rb, line 27
def initialize
  @callbacks = []
end

Public Instance Methods

add_callback(&proc) click to toggle source

Adds a callback to the event

proc

The block to execute

# File lib/events.rb, line 46
def add_callback(&proc)
  @callbacks << proc
end
fire(*arguments) click to toggle source

Called when the event occurs

*arguments

The arguments to pass to each callback

Returns any values returned by any subscribed callback

# File lib/events.rb, line 34
def fire(*arguments)
  return_values = []

  @callbacks.each do |proc| 
    return_values << proc.call(*arguments) 
  end

  return_values
end
remove_callback(&proc) click to toggle source

Removes the given block from the event

proc

The block to remove

# File lib/events.rb, line 52
def remove_callback(&proc)
  @callbacks.delete proc
end