Module: Onfire

Defined in:
lib/onfire.rb,
lib/onfire/event.rb,
lib/onfire/version.rb,
lib/onfire/debugging.rb,
lib/onfire/event_table.rb

Defined Under Namespace

Classes: Event, EventTable

Constant Summary collapse

VERSION =
'0.2.0'

Instance Method Summary collapse

Instance Method Details

#event_tableObject



49
50
51
# File 'lib/onfire.rb', line 49

def event_table
  @event_table ||= Onfire::EventTable.new
end

#fire(event_type, data = {}) ⇒ Object

Fires an event which will bubble up starting from the receiver. While bubbling, the events checks the traversed object for matching observers and calls the handlers in the order they were attached.

Notice that you can append payload data to the event object.

fire :click, :time => Time.now

The payload is accessable in the data attribute.

evt.data[:time] # => 2011-03-12 11:25:57 +0100


45
46
47
# File 'lib/onfire.rb', line 45

def fire(event_type, data={})
  event_for(event_type, self, data).bubble!
end

#handlers_for_event(event) ⇒ Object

Get all handlers from self for the passed event (interface for Event).



54
55
56
# File 'lib/onfire.rb', line 54

def handlers_for_event(event)
  event_table.all_handlers_for(event.type, event.source)
end

#on(event_type, options = {}, &block) ⇒ Object

Attachs an event observer to the receiver which is called by a matching event.

The handler might be a block receiving the triggering event.

matz.on :applaus do |evt|
  puts "grin"
end

You can also pass a callable object as handler.

class GrinHandler
  def call(evt)
    puts "grin"
  end
end

matz.on :applaus, :do => GrinHandler.new

The :from option allows conditional filtering.

matz.on :applaus, :from => audience do |evt|

This handler is called only if audience trigger the :applaus event.



28
29
30
31
32
# File 'lib/onfire.rb', line 28

def on(event_type, options={}, &block)
  options[:event_type] = event_type
  
  event_table.add_handler(block || options[:call], options)
end