django_signal 1.0.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/django_signal.rb +15 -10
- metadata +1 -1
data/lib/django_signal.rb
CHANGED
@@ -13,6 +13,7 @@ require 'weakref'
|
|
13
13
|
# * No providing_args, since they had no functional relevance.
|
14
14
|
#
|
15
15
|
class DjangoSignal
|
16
|
+
class InvalidReceiver < StandardError; end
|
16
17
|
#
|
17
18
|
# Create a new signal.
|
18
19
|
#
|
@@ -24,7 +25,8 @@ class DjangoSignal
|
|
24
25
|
#
|
25
26
|
# Connect receiver to sender for signal.
|
26
27
|
#
|
27
|
-
# +receiver+:: A callable which is to receive signals.
|
28
|
+
# +receiver+:: A callable which is to receive signals. The receiver must
|
29
|
+
# be able to handler 2+ arguments (+signal, sender, *send_args+).
|
28
30
|
# If dispatch_uid is given, the receiver will not be added if
|
29
31
|
# another receiver already exists with that dispatch_uid.
|
30
32
|
# +sender+:: The sender to which the receiver should respond or nil to
|
@@ -36,6 +38,16 @@ class DjangoSignal
|
|
36
38
|
def connect(receiver, sender=nil, dispatch_uid=nil)
|
37
39
|
lookup_key = make_key(receiver, sender, dispatch_uid)
|
38
40
|
|
41
|
+
begin
|
42
|
+
receiver.method(:call) rescue nil or raise NoMethodError
|
43
|
+
if -1 < receiver.arity && receiver.arity < 2
|
44
|
+
raise InvalidReceiver, 'Receiver must be able to handle 2+ arguments.'
|
45
|
+
end
|
46
|
+
rescue NoMethodError
|
47
|
+
raise InvalidReceiver, 'Receiver must be a callable (and respond to arity method).'
|
48
|
+
end
|
49
|
+
|
50
|
+
|
39
51
|
@lock.synchronize {
|
40
52
|
@receivers[lookup_key] ||= receiver
|
41
53
|
}
|
@@ -100,15 +112,8 @@ class DjangoSignal
|
|
100
112
|
end
|
101
113
|
|
102
114
|
def simple_call(receiver, sender, *args)
|
103
|
-
|
104
|
-
|
105
|
-
opts = args.pop
|
106
|
-
end
|
107
|
-
opts.merge!(
|
108
|
-
:signal => self,
|
109
|
-
:sender => sender
|
110
|
-
)
|
111
|
-
args.push(opts)
|
115
|
+
args.unshift(sender)
|
116
|
+
args.unshift(self)
|
112
117
|
|
113
118
|
receiver.call(*args)
|
114
119
|
end
|