Project

emittable

0.0
No commit activity in last 3 years
No release in over 3 years
A simple event registering/triggering module to mix into classes.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Emittable

A Ruby Gem to register and trigger events. It is based on Vienna::Emittable. It is thread safe.

Install

gem install emittable

Example

First require 'emittable' in your project. Then you can include Emittable in your classes. If you override initialize remember to call super.

require 'emittable'

class A
  include Emittable
  
  def initialize(...)
    super
    ...
  end
  
  ...
  ...
  
end

a = A.new(...)

To register a new event callback call on on an instance of your class, passing the name of the event and a block as the callback. You can add as many callbacks as you want for an event.

a.on(:shutdown) do
  ...
end

a.on(:shutdown) do
  ...
  ...
end

To trigger all the event's callbacks call trigger, passing the name of the event.

a.trigger(:shutdown)

You can also pass arguments to trigger that will get passed to a callback block.

a.on(:shutdown) do |a, b|
  ...
end

a.trigger(:shutdown, 1, 2)

To remove a callback you must have already saved a reference to the callback block. You can then call off, passing the block.

callback = proc { ... }

a.on(:shutdown, &callback)

a.off(:shutdown, callback)