Class: Context::Publisher
- Inherits:
-
Object
- Object
- Context::Publisher
- Defined in:
- lib/Context/Publisher.rb
Overview
Publisher implements an observer pattern.
An object creates a Publisher, using itself as the source.
It can then publish to several different streams. Observers can
subscribe to a stream. When the stream for an observer
is updated, a method on each observer, named
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
-
#block ⇒ Object
Block a publisher from publishing.
-
#blocked? ⇒ Boolean
Returns true if the publisher is blocked.
-
#initialize(source) ⇒ Publisher
constructor
Create a Publisher with the source as the source object.
-
#subscribe(observer, stream) ⇒ Object
Subscribe an observer to a stream.
-
#unblock ⇒ Object
Unblock a publisher so that it may continue to publish.
-
#unsubscribe(observer, stream) ⇒ Object
Unsubscribe an observer from the stream.
-
#update(stream, source = @source) ⇒ Object
Publish to the observers that the stream has been updated.
Constructor Details
#initialize(source) ⇒ Publisher
Create a Publisher with the source as the source object.
16 17 18 19 20 |
# File 'lib/Context/Publisher.rb', line 16 def initialize(source) @source = source @streamSubscribers = {} @blocked = false end |
Instance Attribute Details
#source ⇒ Object (readonly)
Returns the value of attribute source.
13 14 15 |
# File 'lib/Context/Publisher.rb', line 13 def source @source end |
Instance Method Details
#block ⇒ Object
Block a publisher from publishing. A blocked publisher will not publish to observers, even if update() is called. This is useful for when you know that the source is being updated a lot and you only want to signal it at the end (for instance when you are loading a file). Important Note: block() and unblock() are not counted. No matter how many times you call block(), the first unblock() will unblock it.
95 96 97 |
# File 'lib/Context/Publisher.rb', line 95 def block @blocked = true end |
#blocked? ⇒ Boolean
Returns true if the publisher is blocked. A blocked publisher will not publish, even if update() is called.
82 83 84 |
# File 'lib/Context/Publisher.rb', line 82 def blocked? @blocked end |
#subscribe(observer, stream) ⇒ Object
Subscribe an observer to a stream. The stream must be
a string. It is allowable to observe to streams that
don't exist. The observer must implement
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/Context/Publisher.rb', line 31 def subscribe(observer, stream) if @streamSubscribers.has_key?(stream) observers = @streamSubscribers[stream] if !observers.find do |x| x == observer end observers.push(observer) end else @streamSubscribers[stream] = [observer] end end |
#unblock ⇒ Object
Unblock a publisher so that it may continue to publish. Important Note: block() and unblock() are not counted. No matter how many times you call block(), the first unblock() will unblock it.
103 104 105 |
# File 'lib/Context/Publisher.rb', line 103 def unblock @blocked = false end |
#unsubscribe(observer, stream) ⇒ Object
Unsubscribe an observer from the stream. Remove the observer from the list of objects that are subscribed to the stream. Very Important Note: If you don't unsubscribe from a publisher at the end of the lifetime of your object, the publisher will retain a reference to the object. This means it will continue to exist until the publisher is destroyed. This could potentially cause problems in your code. Always unsubscribe from a Publisher when you are finished with the object.
55 56 57 58 59 60 |
# File 'lib/Context/Publisher.rb', line 55 def unsubscribe(observer, stream) if @streamSubscribers.has_key?(stream) observers = @streamSubscribers[stream] observers.delete(observer) end end |
#update(stream, source = @source) ⇒ Object
Publish to the observers that the stream has been updated. This is usually called by the source object, but it doesn't have to be. The source object can also be changed (it defaults to the source in the Publisher). This is useful if a source is publishing on behalf of another object (essentially acting as a mediator).
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/Context/Publisher.rb', line 68 def update(stream, source=@source) if blocked? return end if @streamSubscribers.has_key?(stream) @streamSubscribers[stream].each do |subscriber| eval("subscriber." + stream + "Updated(source)") end end end |