unobservable 0.10.1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.10.1
1
+ 0.11.0
@@ -42,6 +42,30 @@ module Unobservable
42
42
  end
43
43
 
44
44
 
45
+ # There are 3 ways for end-users to provide an event handler:
46
+ #
47
+ # 1. They can pass an object that has a #call method
48
+ # 2. They can provide an object and the name of a method to invoke
49
+ # 3. They can pass in a block
50
+ def self.handler_for(*args, &block)
51
+ if block
52
+ return block
53
+ elsif args.size == 1
54
+ candidate = args[0]
55
+ if candidate.respond_to?(:to_proc)
56
+ return candidate.to_proc
57
+ else
58
+ raise ArgumentError, "The argument does not respond to the #to_proc method"
59
+ end
60
+ elsif args.size == 2
61
+ return args[0].method(args[1])
62
+ end
63
+
64
+ raise ArgumentError, "Unable to create an event handler using the given arguments"
65
+ end
66
+
67
+
68
+
45
69
  # This module is a mixin that provides support for "instance events".
46
70
  module ModuleSupport
47
71
 
@@ -167,32 +191,12 @@ module Unobservable
167
191
  @handlers = []
168
192
  end
169
193
 
170
- # There are 3 ways for end-users to provide an event handler:
171
- #
172
- # 1. They can pass an object that has a #call method
173
- # 2. They can provide an object and the name of a method to invoke
174
- # 3. They can pass in a block
175
- def handler_for(*args, &block)
176
- if block
177
- return block
178
- elsif args.size == 1
179
- candidate = args[0]
180
- if candidate.respond_to?(:to_proc)
181
- return candidate.to_proc
182
- else
183
- raise ArgumentError, "The argument does not respond to the #to_proc method"
184
- end
185
- elsif args.size == 2
186
- return args[0].method(args[1])
187
- end
188
194
 
189
- raise ArgumentError, "Unable to create an event handler using the given arguments"
190
- end
191
195
 
192
196
  # Registers the given event handler so that it will be
193
197
  # invoked when the event is raised.
194
198
  def register(*args, &block)
195
- h = handler_for(*args, &block)
199
+ h = Unobservable.handler_for(*args, &block)
196
200
  @handlers << h
197
201
  return h
198
202
  end
@@ -205,7 +209,7 @@ module Unobservable
205
209
  # registered the same event handler 3 times, then you will
206
210
  # need to unregister it 3 times as well.
207
211
  def unregister(*args, &block)
208
- h = handler_for(*args, &block)
212
+ h = Unobservable.handler_for(*args, &block)
209
213
  index = @handlers.index(h)
210
214
  if index
211
215
  @handlers.slice!(index)
@@ -6,39 +6,6 @@ module Unobservable
6
6
 
7
7
  let(:e) { Event.new }
8
8
 
9
- describe "#handler_for" do
10
- it "raises an ArgumentError when it does not receive any arguments" do
11
- expect{ e.handler_for() }.to raise_error(ArgumentError)
12
- end
13
-
14
-
15
- it "raises an ArgumentError when it is receives an argument that cannot be converted into a Proc" do
16
- expect{ e.handler_for(Object.new) }.to raise_error(ArgumentError)
17
- end
18
-
19
- it "allows a Proc to be used has an event handler" do
20
- p = Proc.new {}
21
- e.handler_for(p).should == p
22
- end
23
-
24
- it "allows a Block to be used as an event handler" do
25
- p = Proc.new {}
26
- e.handler_for(&p).should == p
27
- end
28
-
29
- it "can use a specified method on an object as an event handler" do
30
- x = Object.new
31
- e.handler_for(x, :class).should == x.method(:class)
32
- end
33
-
34
-
35
- it "raises an ArgumentError when it receives 3 or more arguments" do
36
- expect{ e.handler_for(Proc.new, :foo, :bar) }.to raise_error(ArgumentError)
37
- end
38
-
39
-
40
- end
41
-
42
9
 
43
10
  describe "#register" do
44
11
 
@@ -70,5 +70,41 @@ describe Unobservable do
70
70
 
71
71
  end
72
72
 
73
+
74
+
75
+
76
+ describe "#handler_for" do
77
+ it "raises an ArgumentError when it does not receive any arguments" do
78
+ expect{ Unobservable.handler_for() }.to raise_error(ArgumentError)
79
+ end
80
+
81
+
82
+ it "raises an ArgumentError when it is receives an argument that cannot be converted into a Proc" do
83
+ expect{ Unobservable.handler_for(Object.new) }.to raise_error(ArgumentError)
84
+ end
85
+
86
+ it "allows a Proc to be used has an event handler" do
87
+ p = Proc.new {}
88
+ Unobservable.handler_for(p).should == p
89
+ end
90
+
91
+ it "allows a Block to be used as an event handler" do
92
+ p = Proc.new {}
93
+ Unobservable.handler_for(&p).should == p
94
+ end
95
+
96
+ it "can use a specified method on an object as an event handler" do
97
+ x = Object.new
98
+ Unobservable.handler_for(x, :class).should == x.method(:class)
99
+ end
100
+
101
+
102
+ it "raises an ArgumentError when it receives 3 or more arguments" do
103
+ expect{ Unobservable.handler_for(Proc.new, :foo, :bar) }.to raise_error(ArgumentError)
104
+ end
105
+
106
+ end
107
+
108
+
73
109
  end
74
110
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unobservable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.1
4
+ version: 0.11.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -129,7 +129,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
129
129
  version: '0'
130
130
  segments:
131
131
  - 0
132
- hash: -1060921785598011427
132
+ hash: 2871391194077949932
133
133
  required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  none: false
135
135
  requirements: