class Orator::EventHandler

This handles events and calls the callbacks for them.

Public Class Methods

new(&block) click to toggle source

Initialize the event handler.

@yields [] in the current instance. Mainly for setting up the events. @return [void]

# File lib/orator/event_handler.rb, line 10
def initialize(&block)
  @events  = []
  @_number = 0
  instance_exec &block if block_given?
end

Public Instance Methods

events(matching = nil) click to toggle source

Accessor for events. If the first parameter is passed, it checks for events that match that event.

@param matching [String, Symbol] the event to find. @return [Set<Hash>]

# File lib/orator/event_handler.rb, line 76
def events(matching = nil)
  if matching
    @events.select { |e| e[:event] == matching.to_s }
  else
    @events.dup
  end
end
on(event, klass = nil, count = nil, &block) click to toggle source

This adds an event to the set. It can accept a block or a class. If it’s a class, the class should inherit from [Base]. If both a class and a block is given, the block is preferred.

@param event [String, Symbol] the event to bind it to. @param klass [Base, nil] the class that contains the method to

use for the event.

@param count [Numeric, nil] the number of times to run this event. No

value is an infinite number of times.

@raise [ArgumentError] if klass isn’t a subclass of Base. @return [void]

# File lib/orator/event_handler.rb, line 27
def on(event, klass = nil, count = nil, &block)
  puts "Binding #{event} to #{klass || block}..." if Orator.debug
  data = { :event => event.to_s, :count => count }

  if block_given?
    data[:block] = block
  elsif klass and klass < Base
    data[:class] = klass
  else
    raise ArgumentError, "No class or block was given."
  end

  @events << data unless @events.include?(data)
end
trigger(event, context, *args) click to toggle source

This triggers the events. The events aren’t triggered in any particular order, since they are stored in a [Set]. Extra arguments are passed to the block/class. If no events are found, it runs the ‘socket.missing` event.

@param event [String, Symbol] the event to trigger. @param context [Object] the context to run in. If the event is mapped to

a block, the block is executed in the context.  In all cases, the
context is appended to the end of the arguments.

@return [Object, nil]

# File lib/orator/event_handler.rb, line 52
def trigger(event, context, *args)
  puts "Running event #{event}..." if Orator.debug
  matching_events = events(event)

  if matching_events.length == 0
    puts "Could not find event, running socket.missing..." if Orator.debug
    matching_events = events('socket.missing')
  end

  matching_events.map do |event|
    puts "Found responder #{event[:block] || event[:class]}" if Orator.debug

    run_event(event, context, args)
    if event[:count] then event[:count] -= 1 end
  end

  clear_events
end

Private Instance Methods

clear_events() click to toggle source

This removes events where their [:count] is less than or equal to zero.

@return [void]

# File lib/orator/event_handler.rb, line 89
def clear_events
  @events.delete_if do |event|
    if event[:count]
      event[:count] <= 0
    else
      false
    end
  end
end
run_event(event, context, arguments) click to toggle source

This runs a given event with the given context and arguments.

@param event [Hash] the event to run. @param context [Object] the context to run in. @param arguments [Array<Object>] the arguments to run with. @return [void]

# File lib/orator/event_handler.rb, line 105
def run_event(event, context, arguments)
  if event[:block]
    context.instance_exec *arguments, context, &event[:block]
  elsif event[:class]
    event[:class].new(context).__trigger(event[:event], *arguments)
  end
end