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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d688ce9f9a7f41c345eb041aaab60e3da171af7
4
- data.tar.gz: 1a636e1298a86954dbd1da417863537a1fcd4de4
3
+ metadata.gz: 93ace3722a7a1a31ff19eb6dd3f274df55e79d58
4
+ data.tar.gz: 43cb458919cfc1c7a05ff8be99f1c68220600eaf
5
5
  SHA512:
6
- metadata.gz: ddd59dbc50a5c515f3307c20d89af59c0f59a609be912f3736c230874ecfc237947eb307f65e9a82d746640263094a4b8a320a01cbabe08503562d5127e636de
7
- data.tar.gz: 22cf3263b3cc9b1e9e784678bb2dea310ddaed40e79c0360098cad5087b049d12ec3944d7a5eb1bb293a84ecae7fb5487a5f6f95196edc1e236bf0d08b8322ef
6
+ metadata.gz: 4058fc818ee2aa041614451d122199d1b93d03ef4bf361e1215ef2f3dd697172edb2bc640f22d0f920f5b750190221772886a559881e3071f54a3eb4d4c84546
7
+ data.tar.gz: a7e30103766f5d712f93292b58576f95bce33394a296dfb3f2bb4b496ddb300080ac51e9f5539163c2d87401463ec93edb7217814fa5013236a442ff46e7cca2
@@ -1,8 +1,7 @@
1
1
  language: ruby
2
- before_install:
3
- - gem update --system
4
- - gem --version
5
- - gem install rubysl-bundler
6
- script: bundle exec mspec spec
2
+ env:
3
+ - RUBYLIB=lib
4
+ script: bundle exec mspec
7
5
  rvm:
8
- - rbx-nightly-18mode
6
+ - 1.9.3
7
+ - rbx-nightly-19mode
@@ -1,33 +1,34 @@
1
1
  #
2
- # observer.rb implements the _Observer_ object-oriented design pattern. The
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.rubycentral.com/book/lib_patterns.html.
4
+ # Ruby", by Hunt and Thomas; http://www.ruby-doc.org/docs/ProgrammingRuby/html/lib_patterns.html.
5
5
  #
6
- # == About
7
- #
8
- # The Observer pattern, also known as Publish/Subscribe, provides a simple
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
- # In the Ruby implementation, the notifying class mixes in the +Observable+
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 the +update+ method to receive notifications.
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 +changed+
22
- # * call +notify_observers+
22
+ # * assert that it has +#changed+
23
+ # * call +#notify_observers+
23
24
  #
24
- # == Example
25
+ # === Example
25
26
  #
26
27
  # The following example demonstrates this nicely. A +Ticker+, when run,
27
- # continually receives the stock +Price+ for its +@symbol+. A +Warner+ is a
28
- # general observer of the price, and two warners are demonstrated, a +WarnLow+
29
- # and a +WarnHigh+, which print a warning if the price is below or above their
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. +observer+ will now receive
115
+ # Add +observer+ as an observer on this object. so that it will receive
121
116
  # notifications.
122
117
  #
123
- def add_observer(observer)
124
- @observer_peers = [] unless defined? @observer_peers
125
- unless observer.respond_to? :update
126
- raise NoMethodError, "observer needs to respond to `update'"
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.push observer
131
+ @observer_peers[observer] = func
129
132
  end
130
133
 
131
134
  #
132
- # Delete +observer+ as an observer on this object. It will no longer receive
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
- # Delete all observers associated with this object.
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
- # Query the changed state of this object.
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
- # If this object's changed state is +true+, invoke the update method in each
178
- # currently associated observer in turn, passing it the given arguments. The
179
- # changed state is then set to +false+.
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.dup.each do |i|
185
- i.update(*arg)
194
+ @observer_peers.each do |k, v|
195
+ k.send v, *arg
186
196
  end
187
197
  end
188
198
  @observer_state = false
@@ -1,5 +1,5 @@
1
1
  module Rubysl
2
2
  module Observable
3
- VERSION = "1.0.0"
3
+ VERSION = "2.0.0"
4
4
  end
5
5
  end
@@ -19,5 +19,4 @@ Gem::Specification.new do |spec|
19
19
  spec.add_development_dependency "bundler", "~> 1.3"
20
20
  spec.add_development_dependency "rake", "~> 10.0"
21
21
  spec.add_development_dependency "mspec", "~> 1.5"
22
- spec.add_development_dependency "rubysl-prettyprint", "~> 1.0"
23
22
  end
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: 1.0.0
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-12-27 00:00:00.000000000 Z
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