rubysl-observer 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -6
- data/lib/rubysl/observer/observer.rb +45 -35
- data/lib/rubysl/observer/version.rb +1 -1
- data/rubysl-observer.gemspec +0 -1
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93ace3722a7a1a31ff19eb6dd3f274df55e79d58
|
4
|
+
data.tar.gz: 43cb458919cfc1c7a05ff8be99f1c68220600eaf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4058fc818ee2aa041614451d122199d1b93d03ef4bf361e1215ef2f3dd697172edb2bc640f22d0f920f5b750190221772886a559881e3071f54a3eb4d4c84546
|
7
|
+
data.tar.gz: a7e30103766f5d712f93292b58576f95bce33394a296dfb3f2bb4b496ddb300080ac51e9f5539163c2d87401463ec93edb7217814fa5013236a442ff46e7cca2
|
data/.travis.yml
CHANGED
@@ -1,33 +1,34 @@
|
|
1
1
|
#
|
2
|
-
#
|
2
|
+
# Implementation of the _Observer_ object-oriented design pattern. The
|
3
3
|
# following documentation is copied, with modifications, from "Programming
|
4
|
-
# Ruby", by Hunt and Thomas; http://www.
|
4
|
+
# Ruby", by Hunt and Thomas; http://www.ruby-doc.org/docs/ProgrammingRuby/html/lib_patterns.html.
|
5
5
|
#
|
6
|
-
#
|
7
|
-
|
8
|
-
# The Observer pattern
|
6
|
+
# See Observable for more info.
|
7
|
+
|
8
|
+
# The Observer pattern (also known as publish/subscribe) provides a simple
|
9
9
|
# mechanism for one object to inform a set of interested third-party objects
|
10
10
|
# when its state changes.
|
11
11
|
#
|
12
12
|
# == Mechanism
|
13
13
|
#
|
14
|
-
#
|
14
|
+
# The notifying class mixes in the +Observable+
|
15
15
|
# module, which provides the methods for managing the associated observer
|
16
16
|
# objects.
|
17
17
|
#
|
18
|
-
# The observers must implement
|
18
|
+
# The observers must implement a method called +update+ to receive
|
19
|
+
# notifications.
|
19
20
|
#
|
20
21
|
# The observable object must:
|
21
|
-
# * assert that it has
|
22
|
-
# * call
|
22
|
+
# * assert that it has +#changed+
|
23
|
+
# * call +#notify_observers+
|
23
24
|
#
|
24
|
-
#
|
25
|
+
# === Example
|
25
26
|
#
|
26
27
|
# The following example demonstrates this nicely. A +Ticker+, when run,
|
27
|
-
# continually receives the stock +Price+ for its
|
28
|
-
# general observer of the price, and two warners are demonstrated, a
|
29
|
-
# and a +WarnHigh+, which print a warning if the price is below or
|
30
|
-
# set limits, respectively.
|
28
|
+
# continually receives the stock +Price+ for its <tt>@symbol</tt>. A +Warner+
|
29
|
+
# is a general observer of the price, and two warners are demonstrated, a
|
30
|
+
# +WarnLow+ and a +WarnHigh+, which print a warning if the price is below or
|
31
|
+
# above their set limits, respectively.
|
31
32
|
#
|
32
33
|
# The +update+ callback allows the warners to run without being explicitly
|
33
34
|
# called. The system is set up with the +Ticker+ and several observers, and the
|
@@ -108,36 +109,39 @@
|
|
108
109
|
# Current price: 112
|
109
110
|
# Current price: 79
|
110
111
|
# --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 79
|
111
|
-
|
112
|
-
|
113
|
-
#
|
114
|
-
# Implements the Observable design pattern as a mixin so that other objects can
|
115
|
-
# be notified of changes in state. See observer.rb for details and an example.
|
116
|
-
#
|
117
112
|
module Observable
|
118
113
|
|
119
114
|
#
|
120
|
-
# Add +observer+ as an observer on this object.
|
115
|
+
# Add +observer+ as an observer on this object. so that it will receive
|
121
116
|
# notifications.
|
122
117
|
#
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
118
|
+
# +observer+:: the object that will be notified of changes.
|
119
|
+
# +func+:: Symbol naming the method that will be called when this Observable
|
120
|
+
# has changes.
|
121
|
+
#
|
122
|
+
# This method must return true for +observer.respond_to?+ and will
|
123
|
+
# receive <tt>*arg</tt> when #notify_observers is called, where
|
124
|
+
# <tt>*arg</tt> is the value passed to #notify_observers by this
|
125
|
+
# Observable
|
126
|
+
def add_observer(observer, func=:update)
|
127
|
+
@observer_peers = {} unless defined? @observer_peers
|
128
|
+
unless observer.respond_to? func
|
129
|
+
raise NoMethodError, "observer does not respond to `#{func.to_s}'"
|
127
130
|
end
|
128
|
-
@observer_peers
|
131
|
+
@observer_peers[observer] = func
|
129
132
|
end
|
130
133
|
|
131
134
|
#
|
132
|
-
#
|
133
|
-
# notifications.
|
135
|
+
# Remove +observer+ as an observer on this object so that it will no longer
|
136
|
+
# receive notifications.
|
134
137
|
#
|
138
|
+
# +observer+:: An observer of this Observable
|
135
139
|
def delete_observer(observer)
|
136
140
|
@observer_peers.delete observer if defined? @observer_peers
|
137
141
|
end
|
138
142
|
|
139
143
|
#
|
140
|
-
#
|
144
|
+
# Remove all observers associated with this object.
|
141
145
|
#
|
142
146
|
def delete_observers
|
143
147
|
@observer_peers.clear if defined? @observer_peers
|
@@ -158,12 +162,15 @@ module Observable
|
|
158
162
|
# Set the changed state of this object. Notifications will be sent only if
|
159
163
|
# the changed +state+ is +true+.
|
160
164
|
#
|
165
|
+
# +state+:: Boolean indicating the changed state of this Observable.
|
166
|
+
#
|
161
167
|
def changed(state=true)
|
162
168
|
@observer_state = state
|
163
169
|
end
|
164
170
|
|
165
171
|
#
|
166
|
-
#
|
172
|
+
# Returns true if this object's state has been changed since the last
|
173
|
+
# #notify_observers call.
|
167
174
|
#
|
168
175
|
def changed?
|
169
176
|
if defined? @observer_state and @observer_state
|
@@ -174,15 +181,18 @@ module Observable
|
|
174
181
|
end
|
175
182
|
|
176
183
|
#
|
177
|
-
#
|
178
|
-
#
|
179
|
-
#
|
184
|
+
# Notify observers of a change in state *if* this object's changed state is
|
185
|
+
# +true+.
|
186
|
+
#
|
187
|
+
# This will invoke the method named in #add_observer, passing <tt>*arg</tt>.
|
188
|
+
# The changed state is then set to +false+.
|
180
189
|
#
|
190
|
+
# <tt>*arg</tt>:: Any arguments to pass to the observers.
|
181
191
|
def notify_observers(*arg)
|
182
192
|
if defined? @observer_state and @observer_state
|
183
193
|
if defined? @observer_peers
|
184
|
-
@observer_peers.
|
185
|
-
|
194
|
+
@observer_peers.each do |k, v|
|
195
|
+
k.send v, *arg
|
186
196
|
end
|
187
197
|
end
|
188
198
|
@observer_state = false
|
data/rubysl-observer.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubysl-observer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Shirai
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-09-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,20 +52,6 @@ dependencies:
|
|
52
52
|
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '1.5'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: rubysl-prettyprint
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ~>
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '1.0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ~>
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '1.0'
|
69
55
|
description: Ruby standard library observer.
|
70
56
|
email:
|
71
57
|
- brixen@gmail.com
|