From ba23a5ac276e59fdda8186750c6d0fd2cfecdeac Mon Sep 17 00:00:00 2001 From: luke Date: Sat, 17 Mar 2007 02:48:41 +0000 Subject: Adding spec libs, so we can use them some day git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2283 980ebf18-57e1-0310-9a29-db15c13687c0 --- test/lib/spec/callback/callback_container.rb | 60 ++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 test/lib/spec/callback/callback_container.rb (limited to 'test/lib/spec/callback/callback_container.rb') diff --git a/test/lib/spec/callback/callback_container.rb b/test/lib/spec/callback/callback_container.rb new file mode 100644 index 000000000..24d4c0ced --- /dev/null +++ b/test/lib/spec/callback/callback_container.rb @@ -0,0 +1,60 @@ +module Callback + class CallbackContainer + def initialize + @callback_registry = Hash.new do |hash, key| + hash[key] = Array.new + end + end + + # Defines the callback with the key in this container. + def define(key, callback_proc=nil, &callback_block) + callback = extract_callback(callback_block, callback_proc) do + raise "You must define the callback that accepts the call method." + end + @callback_registry[key] << callback + callback + end + + # Undefines the callback with the key in this container. + def undefine(key, callback_proc) + callback = extract_callback(callback_proc) do + raise "You may only undefine callbacks that use the call method." + end + @callback_registry[key].delete callback + callback + end + + # Notifies the callbacks for the key. Arguments may be passed. + # An error handler may be passed in as a block. If there is an error, the block is called with + # error object as an argument. + # An array of the return values of the callbacks is returned. + def notify(key, *args, &error_handler) + @callback_registry[key].collect do |callback| + begin + callback.call(*args) + rescue Exception => e + yield(e) if error_handler + end + end + end + + # Clears all of the callbacks in this container. + def clear + @callback_registry.clear + end + + protected + def extract_callback(first_choice_callback, second_choice_callback = nil) + callback = nil + if first_choice_callback + callback = first_choice_callback + elsif second_choice_callback + callback = second_choice_callback + end + unless callback.respond_to? :call + yield + end + return callback + end + end +end -- cgit