Ruby Eventing


#1

Hi,

While working on a state machine work flow, I need a flexible way to
define
event/event handling. Since I came from C# background, so I have come up
event.rb to share with everyone. Welcome for comments.

Here’s the example codes. You can get event.rb from
http://competo.com/code/event.rb.
Thanks!
class CoffeeMaker

include Event

def initialize

 event 'brew_ready'

 event 'brewing_started'

end

def brew

 on_brewing_started('new brew started')

 #brewing ...

 on_brew_ready('coffee is ready!')

end

end

class CoffeeLover

def get_coffee(*args)

puts “thanks for the coffee!”

end

end

cm = CoffeeMaker.new

cm.brewing_started(’+s1’) {|*args| puts args}

cm.brew_ready(’+s2’) {|*args| puts args}

cl = CoffeeLover.new

cm.brew_ready(’+s3’) {|*args| cl.get_coffee args} # add sink with +

cm.brew

cm.brew_ready(’-s3’) #remove sink with -

cm.brew

produces …

new brew started

thanks for the coffee!

coffee is ready!

new brew started

coffee is ready!


#2

Here’s the latest event.rb.

require ‘event’

class CoffeeMaker
event :brew_ready, :brewing_started
def brew
on_brewing_started(‘new brew started’)
#brewing …
on_brew_ready(‘coffee is ready!’)
end
end

class CoffeeLover
def get_coffee(*args)
puts “thanks for the coffee!”
end
end

cm = CoffeeMaker.new
cm.brewing_started(’+s1’) {|*args| puts args}
cm.brew_ready(’+s2’) {|*args| puts args}
cl = CoffeeLover.new
cm.brew_ready(’+s3’) {|*args| cl.get_coffee args} # add sink with +
cm.brew

cm.brew_ready(’-s3’) #remove sink with -
cm.brew