Ruby Eventing

R

Ronggen Pan

------=_NextPart_000_000C_01C6097D.5A1D27E0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

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!



------=_NextPart_000_000C_01C6097D.5A1D27E0--
 
R

Ronggen Pan

Here's the latest <a
href="http://competo.com/ruby/event.rb">event.rb</a>.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top