Forum: Ruby Ruby Eventing

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
870ba160c27767b2fba52a688e9b17ad?d=identicon&s=25 Ronggen Pan (Guest)
on 2005-12-26 01:04
(Received via mailing list)
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!
870ba160c27767b2fba52a688e9b17ad?d=identicon&s=25 Ronggen Pan (Guest)
on 2005-12-26 08:10
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
This topic is locked and can not be replied to.