[Rubygems-developers] UserInteraction capture capability implemented

Richard Kilmer rich at infoether.com
Thu Jun 17 00:17:58 EDT 2004


As I said in a prior post about the new UserInteraction module, I
implemented a class that hooks (through replacing) the module methods and
provides you a means of capturing the interaction.

Here is an example

require 'rubygems/user_interaction'

# to demonstrate closure
foo = 5

o = Object.new

# add the UserInteraction module
o.extend Gem::UserInteraction

o.say 'hello' #=> 'hello'

# capture the interaction
ui = Gem::UserInteraction.capture

# now redefine the 'say' method
ui.on_say do |statement|
  puts "you said: #{statement} and foo is: #{foo}"
o.say 'bar' #=> 'you said: bar and foo is: 5'

So, for each method in the UserInteraction module, once you call #capture
you get the Capture object (like 'ui' above) and can then create a block
with a corresponding:

  ui.on_<method> do |param1, ...|
    #do something here

If you don't capture a method with an on_<method> method then that method
does nothing.  In other words, when you capture interaction, you get rid of
ALL the existing modules methods and only implement the ones you want.  Oh,
and the last capture wins!...if you call capture twice, the last capture is
the one that has the hook (the prior capture's methods are replaced).

This will be the basis for unit testing the cmd_manager stuff and also can
be used by a GUI framework (Curt?) to capture IO as well.

I will begin writing a cmd_manager unit test file a bit later.



More information about the Rubygems-developers mailing list