# frozen_string_literal: true
# Implementation of the _Observer_ object-oriented design pattern. The
# following documentation is copied, with modifications, from "Programming
# Ruby", by Hunt and Thomas; http://www.ruby-doc.org/docs/ProgrammingRuby/html/lib_patterns.html.
# See Observable for more info.
# The Observer pattern (also known as publish/subscribe) provides a simple
# mechanism for one object to inform a set of interested third-party objects
# when its state changes.
# The notifying class mixes in the +Observable+
# module, which provides the methods for managing the associated observer
# The observable object must:
# * assert that it has +#changed+
# * call +#notify_observers+
# An observer subscribes to updates using Observable#add_observer, which also
# specifies the method called via #notify_observers. The default method for
# #notify_observers is #update.
# The following example demonstrates this nicely. A +Ticker+, when run,
# continually receives the stock +Price+ for its <tt>@symbol</tt>. A +Warner+
# is a general observer of the price, and two warners are demonstrated, a
# +WarnLow+ and a +WarnHigh+, which print a warning if the price is below or
# above their set limits, respectively.
# The +update+ callback allows the warners to run without being explicitly
# called. The system is set up with the +Ticker+ and several observers, and the
# observers do their duty without the top-level code having to interfere.
# Note that the contract between publisher and subscriber (observable and
# observer) is not declared or enforced. The +Ticker+ publishes a time and a
# price, and the warners receive that. But if you don't ensure that your
# contracts are correct, nothing else can warn you.
# class Ticker ### Periodically fetch a stock price.
# price = Price.fetch(@symbol)
# print "Current price: #{price}\n"
# changed # notify observers
# notify_observers(Time.now, price)
# class Price ### A mock class to fetch a stock price (60 - 140).
# class Warner ### An abstract observer of Ticker objects.
# def initialize(ticker, limit)
# ticker.add_observer(self)
# def update(time, price) # callback for observer
# print "--- #{time.to_s}: Price below #@limit: #{price}\n"
# class WarnHigh < Warner
# def update(time, price) # callback for observer
# print "+++ #{time.to_s}: Price above #@limit: #{price}\n"
# ticker = Ticker.new("MSFT")
# WarnLow.new(ticker, 80)
# WarnHigh.new(ticker, 120)
# --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 75
# +++ Sun Jun 09 00:10:25 CDT 2002: Price above 120: 134
# --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 79
# The +#notify_observers+ method can also be used with +proc+s by using
# the +:call+ as +func+ parameter.
# The following example illustrates the use of a lambda:
# # logic to retrieve the price (here 77.0)
# warner = ->(price) { puts "New price received: #{price}" }
# ticker.add_observer(warner, :call)
# Add +observer+ as an observer on this object. So that it will receive
# +observer+:: the object that will be notified of changes.
# +func+:: Symbol naming the method that will be called when this Observable
# This method must return true for +observer.respond_to?+ and will
# receive <tt>*arg</tt> when #notify_observers is called, where
# <tt>*arg</tt> is the value passed to #notify_observers by this
def add_observer(observer, func=:update)
@observer_peers = {} unless defined? @observer_peers
unless observer.respond_to? func
raise NoMethodError, "observer does not respond to `#{func}'"
@observer_peers[observer] = func
# Remove +observer+ as an observer on this object so that it will no longer
# +observer+:: An observer of this Observable
def delete_observer(observer)
@observer_peers.delete observer if defined? @observer_peers
# Remove all observers associated with this object.
@observer_peers.clear if defined? @observer_peers
# Return the number of observers associated with this object.
if defined? @observer_peers
# Set the changed state of this object. Notifications will be sent only if
# the changed +state+ is +true+.
# +state+:: Boolean indicating the changed state of this Observable.
# Returns true if this object's state has been changed since the last
# #notify_observers call.
if defined? @observer_state and @observer_state
# Notify observers of a change in state *if* this object's changed state is
# This will invoke the method named in #add_observer, passing <tt>*arg</tt>.
# The changed state is then set to +false+.
# <tt>*arg</tt>:: Any arguments to pass to the observers.
def notify_observers(*arg)
if defined? @observer_state and @observer_state
if defined? @observer_peers
@observer_peers.each do |k, v|