Skip to content
A Ruby hack to add events in a similar was as in c#
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
spec
.document
.gitignore
.rspec
Gemfile
LICENSE.txt
README.rdoc
Rakefile
railsonfire.yml

README.rdoc

Event0rz!

A Ruby hack (or gem) to add events in a similar way as C#

Disclaimer

This is a hack to learn a bit more about metaprogramming. If you find it useful, please use it and contact me if you have any ideas about how to improve it.

I still wish I would become millionare by selling this library a trillion times and do a hostile takeover of Apple, Google and Ben & Jerry, but it's just a wish.

Crash introduction

Download the latest code by running (I might upload to gemcutter at some point)

git clone git@github.com:augusto/eventorz.git
cd eventorz
bundle install
rake install

then from your code

require 'eventorz'

class EventProducer
  event :my_event

  def do_something
    # do some serious business and then fire event
    on_my_event :param => 'some param', :param2 => 5
  end
end

class EventConsumer
  def initialize(producer)
    producer.my_event += handle(:event_handler)
  end

  def event_handler(source, parameters)
    puts "received event from #{source} with parameters #{parameters}"
  end
end

producer = EventProducer.new
consumer = EventConsumer.new producer
producer.do_something

this code will produce the ouput

received event from #<EventProducer:0x000000101cc270> with parameters {:param=>"some param", :param2=>5}

Other handlers

The handle method also supports procs and blocks so you can wire a handler in the following ways

class MoreWiring
  def hook_handlers(producer)
    producer.my_event += handle { |source, params| puts "I've received #{params} from #{source}" }
    producer.my_event += handle lambda { |source, params| puts "this is a lambda handler" }
  end
end

mw = MoreWiring.new
mw.hook_handlers producer
producer.do_something

this code produces the output (it also fires the handler defined above in EventCosumer)

received event from #<EventProducer:0x000000101cc270> with parameters {:param=>"some param", :param2=>5}
I've received {:param=>"some param", :param2=>5} from #<EventProducer:0x000000101cc270>
this is a lambda handler

Copyright

Only the letter 'e' is copyrighted, so feel free to use any code that doesn't contain any 'e'.

Something went wrong with that request. Please try again.