Module | Observable
In: | observer.rb

Implements the Observable design pattern as a mixin so that other objects can be notified of changes in state. See observer.rb for details and an example.

### Methods

add_observer changed changed? count_observers delete_observer delete_observers notify_observers

### Public Instance methods

add_observer(observer)

Add `observer` as an observer on this object. `observer` will now receive notifications.

[Source]

    # File observer.rb, line 123
      def add_observer(observer)
        @observer_peers = [] unless defined? @observer_peers
        unless observer.respond_to? :update
          raise NoMethodError, "observer needs to respond to `update'"
        end
        @observer_peers.push observer
      end

changed(state=true)

Set the changed state of this object. Notifications will be sent only if the changed `state` is `true`.

[Source]

    # File observer.rb, line 161
      def changed(state=true)
        @observer_state = state
      end

changed?()

Query the changed state of this object.

[Source]

    # File observer.rb, line 168
      def changed?
        if defined? @observer_state and @observer_state
          true
        else
          false
        end
      end

count_observers()

Return the number of observers associated with this object.

[Source]

    # File observer.rb, line 149
      def count_observers
        if defined? @observer_peers
          @observer_peers.size
        else
          0
        end
      end

delete_observer(observer)

Delete `observer` as an observer on this object. It will no longer receive notifications.

[Source]

    # File observer.rb, line 135
      def delete_observer(observer)
        @observer_peers.delete observer if defined? @observer_peers
      end

delete_observers()

Delete all observers associated with this object.

[Source]

    # File observer.rb, line 142
      def delete_observers
        @observer_peers.clear if defined? @observer_peers
      end

notify_observers(*arg)

If this object’s changed state is `true`, invoke the update method in each currently associated observer in turn, passing it the given arguments. The changed state is then set to `false`.

[Source]

    # File observer.rb, line 181
      def notify_observers(*arg)
        if defined? @observer_state and @observer_state
          if defined? @observer_peers
            for i in @observer_peers.dup
              i.update(*arg)
            end
          end
          @observer_state = false
        end
      end

[Validate]