callback 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,7 @@
1
+ 2006-11-04 (0.2.0)
2
+ * Added register_callback as an alias to define_callback. Added unregistration of callbacks in the
3
+ unregister_callback and undefine_callback methods.
4
+
1
5
  2006-11-03 (0.1.0)
2
6
  * Initial Rubyforge Version. Simple callback library. Supports registering and notifying callbacks.
3
7
  Does not yet support unregistering callbacks.
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'rake/clean'
5
5
  require 'rake/rdoctask'
6
6
 
7
7
  PKG_NAME = "callback"
8
- PKG_VERSION = "0.1.0"
8
+ PKG_VERSION = "0.2.0"
9
9
  PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
10
10
  PKG_FILES = FileList[
11
11
  '[A-Z]*',
@@ -1,15 +1,21 @@
1
1
  module Callback
2
- def define_callback(key, &callback)
3
- callbacks.define(key, &callback)
2
+ def define_callback(key, callback_proc=nil, &callback_block)
3
+ callbacks.define(key, callback_proc, &callback_block)
4
4
  end
5
+ alias_method :register_callback, :define_callback
5
6
 
7
+ def undefine_callback(key, callback_proc)
8
+ callbacks.undefine(key, callback_proc)
9
+ end
10
+ alias_method :unregister_callback, :undefine_callback
11
+
12
+ protected
6
13
  def notify_callbacks(key, *args, &error_handler)
7
14
  callbacks.notify(key, *args, &error_handler)
8
15
  end
9
- protected :notify_callbacks
10
16
 
17
+ private
11
18
  def callbacks
12
19
  @callbacks ||= CallbackContainer.new
13
20
  end
14
- private :callbacks
15
21
  end
@@ -6,8 +6,20 @@ module Callback
6
6
  end
7
7
  end
8
8
 
9
- def define(key, &callback)
9
+ def define(key, callback_proc=nil, &callback_block)
10
+ callback = extract_callback(callback_block, callback_proc) do
11
+ raise "You must define the callback that accepts the call method."
12
+ end
10
13
  @callback_registry[key] << callback
14
+ callback
15
+ end
16
+
17
+ def undefine(key, callback_proc)
18
+ callback = extract_callback(callback_proc) do
19
+ raise "You may only undefine callbacks that use the call method."
20
+ end
21
+ @callback_registry[key].delete callback
22
+ callback
11
23
  end
12
24
 
13
25
  def notify(key, *args, &error_handler)
@@ -19,5 +31,19 @@ module Callback
19
31
  end
20
32
  end
21
33
  end
34
+
35
+ protected
36
+ def extract_callback(first_choice_callback, second_choice_callback = nil)
37
+ callback = nil
38
+ if first_choice_callback
39
+ callback = first_choice_callback
40
+ elsif second_choice_callback
41
+ callback = second_choice_callback
42
+ end
43
+ unless callback.respond_to? :call
44
+ yield
45
+ end
46
+ return callback
47
+ end
22
48
  end
23
49
  end
@@ -10,18 +10,74 @@ context "A Callback" do
10
10
  end
11
11
  end
12
12
 
13
- specify "should be notified" do
14
- callback_key = :create
15
- passed_callback_argument = nil
16
- @callback_object.define_callback(callback_key) do |callback_argument|
17
- passed_callback_argument = callback_argument
13
+ specify "should be registered as a block and notified" do
14
+ specify_registration_and_notification do |callback_key, expected_callback|
15
+ @callback_object.register_callback(callback_key, &expected_callback)
18
16
  end
17
+ end
19
18
 
20
- expected_argument = Object.new
21
- @callback_object.notify_callbacks(callback_key, expected_argument)
22
- passed_callback_argument.should_equal expected_argument
19
+ specify "should be defined as a block and notified" do
20
+ specify_registration_and_notification do |callback_key, expected_callback|
21
+ @callback_object.define_callback(callback_key, &expected_callback)
22
+ end
23
+ end
24
+
25
+ specify "should be registered as a proc and notified" do
26
+ specify_registration_and_notification do |callback_key, expected_callback|
27
+ @callback_object.register_callback(callback_key, expected_callback)
28
+ end
23
29
  end
24
30
 
31
+ specify "should be defined as a proc and notified" do
32
+ specify_registration_and_notification do |callback_key, expected_callback|
33
+ @callback_object.define_callback(callback_key, expected_callback)
34
+ end
35
+ end
36
+
37
+ specify "define should require callback with the call method" do
38
+ proc do
39
+ specify_registration_and_notification do |callback_key, expected_callback|
40
+ @callback_object.define_callback(callback_key)
41
+ end
42
+ end.should_raise(RuntimeError, "You must define the callback that accepts the call method.")
43
+ end
44
+
45
+ specify "register should require callback with the call method" do
46
+ proc do
47
+ specify_registration_and_notification do |callback_key, expected_callback|
48
+ @callback_object.register_callback(callback_key)
49
+ end
50
+ end.should_raise(RuntimeError, "You must define the callback that accepts the call method.")
51
+ end
52
+
53
+ specify "should unregister a proc" do
54
+ specify_unregistration do |callback_key, callback|
55
+ @callback_object.unregister_callback callback_key, callback
56
+ end
57
+ end
58
+
59
+ specify "should undefine a proc" do
60
+ specify_unregistration do |callback_key, callback|
61
+ @callback_object.undefine_callback callback_key, callback
62
+ end
63
+ end
64
+
65
+ specify "undefine should require callback with the call method" do
66
+ proc do
67
+ specify_unregistration do |callback_key, expected_callback|
68
+ @callback_object.undefine_callback(callback_key, nil)
69
+ end
70
+ end.should_raise(RuntimeError, "You may only undefine callbacks that use the call method.")
71
+ end
72
+
73
+ specify "unregister should require callback with the call method" do
74
+ proc do
75
+ specify_unregistration do |callback_key, expected_callback|
76
+ @callback_object.unregister_callback(callback_key, nil)
77
+ end
78
+ end.should_raise(RuntimeError, "You may only undefine callbacks that use the call method.")
79
+ end
80
+
25
81
  specify "should handle errors" do
26
82
  @callback_object.define_callback(:error_callback) do
27
83
  raise "First Error"
@@ -42,4 +98,32 @@ context "A Callback" do
42
98
  specify "should not fail with nothing to notify" do
43
99
  @callback_object.notify_callbacks(:foobar, :argument)
44
100
  end
101
+
102
+ protected
103
+ def specify_registration_and_notification
104
+ callback_key = :create
105
+ passed_callback_argument = nil
106
+ expected_callback = proc do |callback_argument|
107
+ passed_callback_argument = callback_argument
108
+ end
109
+
110
+ returned_callback = yield(callback_key, expected_callback)
111
+ returned_callback.should_equal expected_callback
112
+
113
+ expected_argument = Object.new
114
+ @callback_object.notify_callbacks(callback_key, expected_argument)
115
+ passed_callback_argument.should_equal expected_argument
116
+ end
117
+
118
+ def specify_unregistration
119
+ callback_key = :create
120
+ callback_called = false
121
+ callback = @callback_object.define_callback(callback_key) do
122
+ callback_called = true
123
+ end
124
+
125
+ yield(callback_key, callback)
126
+ @callback_object.notify_callbacks(callback_key)
127
+ callback_called.should_equal false
128
+ end
45
129
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: callback
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
7
- date: 2006-11-03 00:00:00 -08:00
6
+ version: 0.2.0
7
+ date: 2006-11-04 00:00:00 -08:00
8
8
  summary: A simple callback library for ruby objects.
9
9
  require_paths:
10
10
  - lib