MicroEvent.rb is a event emitter library which provides the observer pattern to Ruby objects. It is inspired by MicroEvent.js, implemented in less than 20 lines of Ruby.
Add to your Gemfile
gem 'microevent'or copy the source file into your project.
Suppose you got a class Klass, and you wish it to support the observer partern, do
class Klass
include MicroEvent
endThat's it. Now all instances of this class can #bind, #unbind and trigger:
fn = proc{ puts "Go" }
object = Klass.new
object.bind :slot, &fn
object.trigger :slot # => GoYou could also use it on class/singleton level:
class Klass
extend MicroEvent
end
Klass.bind :slot do
puts "Go"
end
Klass.trigger :slot # => GoYou will find more examples in the tests.
Ruby version by Jan Lelis. Inspired by MicroEvent.js by Jerome Etienne.
![[travis]](https://travis-ci.org/janlelis/microevent.rb.png)