Class: Context::Publisher

Inherits:
Object
  • Object
show all
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 <stream>Updated(), is called. The source is passed to this method. In Context, Publishers are usually used to notify Contexts when model objects have been updated (and hence the view needs to change).

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#sourceObject (readonly)

Returns the value of attribute source.



13
14
15
# File 'lib/Context/Publisher.rb', line 13

def source
  @source
end

Instance Method Details

#blockObject

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.

Returns:

  • (Boolean)


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 <stream>Updated(source). For example if in a method of class A I write:

publisher.subscribe(self, "hello")

then A must implement the method helloUpdated(source) otherwise the program will crash when the stream is publish.



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

#unblockObject

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