central_notifications 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +41 -3
- data/lib/registration.rb +3 -3
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -1,10 +1,15 @@
|
|
1
1
|
== Overview
|
2
2
|
|
3
3
|
Based on the idea of notifications in objective-C.
|
4
|
+
|
4
5
|
Register an object method for notifications.
|
6
|
+
|
5
7
|
When registered method is called, all objects which have registered will be notified and will be able to know the result of the registered method.
|
8
|
+
|
6
9
|
Can act as Rails observers, by registering on setters methods.
|
10
|
+
|
7
11
|
You can register both class or instance methods.
|
12
|
+
|
8
13
|
Several different objects, with different class can register for same object method.
|
9
14
|
|
10
15
|
See tests units for more examples of usage.
|
@@ -28,10 +33,11 @@ You can declare a notifier inside the model by including the module CentralNotif
|
|
28
33
|
registration.method = :payment
|
29
34
|
notification.method = :one_order_has_been_payed
|
30
35
|
end
|
31
|
-
In this way you don't have to
|
36
|
+
In this way you don't have to declare the notification klass, it will be automatically set to self.
|
32
37
|
|
33
38
|
==== In an initializer
|
34
39
|
You can register your notification in an initializer (for exemple, in Rails, in a file under config/initializers).
|
40
|
+
|
35
41
|
The analog as previous example will be in this initializer :
|
36
42
|
CentralNotifications.notify do |registration, notification|
|
37
43
|
registration.klass = Order
|
@@ -55,9 +61,9 @@ Suppose you have classes User and Order, and, you have registered for notificati
|
|
55
61
|
end
|
56
62
|
end
|
57
63
|
|
64
|
+
user = User.new
|
58
65
|
order = Order.new
|
59
66
|
order.payment(100)
|
60
|
-
|
61
67
|
# => "Hey! one user know that one order has been paid!"
|
62
68
|
|
63
69
|
If you register notification for another class objects :
|
@@ -77,6 +83,8 @@ If you register notification for another class objects :
|
|
77
83
|
end
|
78
84
|
|
79
85
|
then :
|
86
|
+
user = User.new
|
87
|
+
accounting = Accounting.new
|
80
88
|
order = Order.new
|
81
89
|
order.payment(100)
|
82
90
|
|
@@ -91,8 +99,38 @@ You can get in User class the result of Order#payment in the instance variable @
|
|
91
99
|
puts "Payment is : " + @registration_result.to_s
|
92
100
|
end
|
93
101
|
|
102
|
+
user = User.new
|
94
103
|
order = Order.new
|
95
104
|
order.payment(100)
|
96
105
|
|
97
106
|
# => "Payment is : 100"
|
98
|
-
|
107
|
+
|
108
|
+
== Acting as Observer
|
109
|
+
|
110
|
+
You can use central notifications as observers on attribute values change.
|
111
|
+
For that, just register on setter :
|
112
|
+
|
113
|
+
class Order
|
114
|
+
attr_accessor :amount
|
115
|
+
end
|
116
|
+
|
117
|
+
class User
|
118
|
+
include CentralNotification
|
119
|
+
|
120
|
+
def amount_has_changed
|
121
|
+
puts "The amount has changed and is now:" + @registration_result.to_s
|
122
|
+
end
|
123
|
+
|
124
|
+
register_for_notification do |registration, notification|
|
125
|
+
registration.klass = Order
|
126
|
+
registration.method = :amount=
|
127
|
+
notification.method = :amount_has_changed
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
user = User.new
|
134
|
+
order = Order.new
|
135
|
+
order.amount = 100
|
136
|
+
#=> From user : The amount has changed and is now: 100
|
data/lib/registration.rb
CHANGED
@@ -22,7 +22,7 @@ module CentralNotifications
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def already_registered?
|
25
|
-
@real_klass.instance_methods.include?(@original_alias_symbol
|
25
|
+
@real_klass.instance_methods.map(&:to_sym).include?(@original_alias_symbol)
|
26
26
|
end
|
27
27
|
|
28
28
|
def set_notifier_in_notifier_class
|
@@ -35,7 +35,7 @@ module CentralNotifications
|
|
35
35
|
redefine_method
|
36
36
|
end
|
37
37
|
|
38
|
-
def redefine_method
|
38
|
+
def redefine_method
|
39
39
|
registration, eigenclass, original = self, on_eigenclass?, @original_alias_symbol
|
40
40
|
@real_klass.send(:define_method, method) do |*params|
|
41
41
|
registration.result = send(original, *params)
|
@@ -46,7 +46,7 @@ module CentralNotifications
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def singleton_method?
|
49
|
-
klass.send(:singleton_methods).include?(method
|
49
|
+
klass.send(:singleton_methods).map(&:to_sym).include?(@method)
|
50
50
|
end
|
51
51
|
|
52
52
|
def on_eigenclass?
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: central_notifications
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 1.0.
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Philippe Cantin
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-12-
|
18
|
+
date: 2010-12-20 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|