class Unobservable::Event

Minimalistic Event implementation

Attributes

handlers[R]

Public Class Methods

new() click to toggle source
# File lib/unobservable.rb, line 190
def initialize
  @handlers = []
end

Public Instance Methods

add(*args, &block)
Alias for: register
call(*args, &block) click to toggle source

Pass the specific arguments / block to all of the event handlers. Return true if there was at least 1 event handler; return false otherwise.

# File lib/unobservable.rb, line 229
def call(*args, &block)
  if @handlers.empty?
    return false
  else
    # TODO: Add some form of error-handling
    @handlers.each do |h|
      begin
        h.call(*args, &block)
      rescue Exception
        # TODO: Should probably log when this happens
      end
    end

    return true
  end
end
delete(*args, &block)
Alias for: unregister
register(*args, &block) click to toggle source

Registers the given event handler so that it will be invoked when the event is raised.

# File lib/unobservable.rb, line 198
def register(*args, &block)
  h = Unobservable.handler_for(*args, &block)
  @handlers << h
  return h
end
Also aliased as: add
unregister(*args, &block) click to toggle source

Removes a single instance of the specified event handler from the list of event handlers. Therefore, if you've registered the same event handler 3 times, then you will need to unregister it 3 times as well.

# File lib/unobservable.rb, line 211
def unregister(*args, &block)
  h = Unobservable.handler_for(*args, &block)
  index = @handlers.index(h)
  if index
    @handlers.slice!(index)
    return h
  else
    return nil
  end
end
Also aliased as: delete