Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby27/share/ruby
File: observer.rb
# frozen_string_literal: true
[0] Fix | Delete
#
[1] Fix | Delete
# Implementation of the _Observer_ object-oriented design pattern. The
[2] Fix | Delete
# following documentation is copied, with modifications, from "Programming
[3] Fix | Delete
# Ruby", by Hunt and Thomas; http://www.ruby-doc.org/docs/ProgrammingRuby/html/lib_patterns.html.
[4] Fix | Delete
#
[5] Fix | Delete
# See Observable for more info.
[6] Fix | Delete
[7] Fix | Delete
# The Observer pattern (also known as publish/subscribe) provides a simple
[8] Fix | Delete
# mechanism for one object to inform a set of interested third-party objects
[9] Fix | Delete
# when its state changes.
[10] Fix | Delete
#
[11] Fix | Delete
# == Mechanism
[12] Fix | Delete
#
[13] Fix | Delete
# The notifying class mixes in the +Observable+
[14] Fix | Delete
# module, which provides the methods for managing the associated observer
[15] Fix | Delete
# objects.
[16] Fix | Delete
#
[17] Fix | Delete
# The observable object must:
[18] Fix | Delete
# * assert that it has +#changed+
[19] Fix | Delete
# * call +#notify_observers+
[20] Fix | Delete
#
[21] Fix | Delete
# An observer subscribes to updates using Observable#add_observer, which also
[22] Fix | Delete
# specifies the method called via #notify_observers. The default method for
[23] Fix | Delete
# #notify_observers is #update.
[24] Fix | Delete
#
[25] Fix | Delete
# === Example
[26] Fix | Delete
#
[27] Fix | Delete
# The following example demonstrates this nicely. A +Ticker+, when run,
[28] Fix | Delete
# continually receives the stock +Price+ for its <tt>@symbol</tt>. A +Warner+
[29] Fix | Delete
# is a general observer of the price, and two warners are demonstrated, a
[30] Fix | Delete
# +WarnLow+ and a +WarnHigh+, which print a warning if the price is below or
[31] Fix | Delete
# above their set limits, respectively.
[32] Fix | Delete
#
[33] Fix | Delete
# The +update+ callback allows the warners to run without being explicitly
[34] Fix | Delete
# called. The system is set up with the +Ticker+ and several observers, and the
[35] Fix | Delete
# observers do their duty without the top-level code having to interfere.
[36] Fix | Delete
#
[37] Fix | Delete
# Note that the contract between publisher and subscriber (observable and
[38] Fix | Delete
# observer) is not declared or enforced. The +Ticker+ publishes a time and a
[39] Fix | Delete
# price, and the warners receive that. But if you don't ensure that your
[40] Fix | Delete
# contracts are correct, nothing else can warn you.
[41] Fix | Delete
#
[42] Fix | Delete
# require "observer"
[43] Fix | Delete
#
[44] Fix | Delete
# class Ticker ### Periodically fetch a stock price.
[45] Fix | Delete
# include Observable
[46] Fix | Delete
#
[47] Fix | Delete
# def initialize(symbol)
[48] Fix | Delete
# @symbol = symbol
[49] Fix | Delete
# end
[50] Fix | Delete
#
[51] Fix | Delete
# def run
[52] Fix | Delete
# last_price = nil
[53] Fix | Delete
# loop do
[54] Fix | Delete
# price = Price.fetch(@symbol)
[55] Fix | Delete
# print "Current price: #{price}\n"
[56] Fix | Delete
# if price != last_price
[57] Fix | Delete
# changed # notify observers
[58] Fix | Delete
# last_price = price
[59] Fix | Delete
# notify_observers(Time.now, price)
[60] Fix | Delete
# end
[61] Fix | Delete
# sleep 1
[62] Fix | Delete
# end
[63] Fix | Delete
# end
[64] Fix | Delete
# end
[65] Fix | Delete
#
[66] Fix | Delete
# class Price ### A mock class to fetch a stock price (60 - 140).
[67] Fix | Delete
# def self.fetch(symbol)
[68] Fix | Delete
# 60 + rand(80)
[69] Fix | Delete
# end
[70] Fix | Delete
# end
[71] Fix | Delete
#
[72] Fix | Delete
# class Warner ### An abstract observer of Ticker objects.
[73] Fix | Delete
# def initialize(ticker, limit)
[74] Fix | Delete
# @limit = limit
[75] Fix | Delete
# ticker.add_observer(self)
[76] Fix | Delete
# end
[77] Fix | Delete
# end
[78] Fix | Delete
#
[79] Fix | Delete
# class WarnLow < Warner
[80] Fix | Delete
# def update(time, price) # callback for observer
[81] Fix | Delete
# if price < @limit
[82] Fix | Delete
# print "--- #{time.to_s}: Price below #@limit: #{price}\n"
[83] Fix | Delete
# end
[84] Fix | Delete
# end
[85] Fix | Delete
# end
[86] Fix | Delete
#
[87] Fix | Delete
# class WarnHigh < Warner
[88] Fix | Delete
# def update(time, price) # callback for observer
[89] Fix | Delete
# if price > @limit
[90] Fix | Delete
# print "+++ #{time.to_s}: Price above #@limit: #{price}\n"
[91] Fix | Delete
# end
[92] Fix | Delete
# end
[93] Fix | Delete
# end
[94] Fix | Delete
#
[95] Fix | Delete
# ticker = Ticker.new("MSFT")
[96] Fix | Delete
# WarnLow.new(ticker, 80)
[97] Fix | Delete
# WarnHigh.new(ticker, 120)
[98] Fix | Delete
# ticker.run
[99] Fix | Delete
#
[100] Fix | Delete
# Produces:
[101] Fix | Delete
#
[102] Fix | Delete
# Current price: 83
[103] Fix | Delete
# Current price: 75
[104] Fix | Delete
# --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 75
[105] Fix | Delete
# Current price: 90
[106] Fix | Delete
# Current price: 134
[107] Fix | Delete
# +++ Sun Jun 09 00:10:25 CDT 2002: Price above 120: 134
[108] Fix | Delete
# Current price: 134
[109] Fix | Delete
# Current price: 112
[110] Fix | Delete
# Current price: 79
[111] Fix | Delete
# --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 79
[112] Fix | Delete
module Observable
[113] Fix | Delete
[114] Fix | Delete
#
[115] Fix | Delete
# Add +observer+ as an observer on this object. So that it will receive
[116] Fix | Delete
# notifications.
[117] Fix | Delete
#
[118] Fix | Delete
# +observer+:: the object that will be notified of changes.
[119] Fix | Delete
# +func+:: Symbol naming the method that will be called when this Observable
[120] Fix | Delete
# has changes.
[121] Fix | Delete
#
[122] Fix | Delete
# This method must return true for +observer.respond_to?+ and will
[123] Fix | Delete
# receive <tt>*arg</tt> when #notify_observers is called, where
[124] Fix | Delete
# <tt>*arg</tt> is the value passed to #notify_observers by this
[125] Fix | Delete
# Observable
[126] Fix | Delete
def add_observer(observer, func=:update)
[127] Fix | Delete
@observer_peers = {} unless defined? @observer_peers
[128] Fix | Delete
unless observer.respond_to? func
[129] Fix | Delete
raise NoMethodError, "observer does not respond to `#{func}'"
[130] Fix | Delete
end
[131] Fix | Delete
@observer_peers[observer] = func
[132] Fix | Delete
end
[133] Fix | Delete
[134] Fix | Delete
#
[135] Fix | Delete
# Remove +observer+ as an observer on this object so that it will no longer
[136] Fix | Delete
# receive notifications.
[137] Fix | Delete
#
[138] Fix | Delete
# +observer+:: An observer of this Observable
[139] Fix | Delete
def delete_observer(observer)
[140] Fix | Delete
@observer_peers.delete observer if defined? @observer_peers
[141] Fix | Delete
end
[142] Fix | Delete
[143] Fix | Delete
#
[144] Fix | Delete
# Remove all observers associated with this object.
[145] Fix | Delete
#
[146] Fix | Delete
def delete_observers
[147] Fix | Delete
@observer_peers.clear if defined? @observer_peers
[148] Fix | Delete
end
[149] Fix | Delete
[150] Fix | Delete
#
[151] Fix | Delete
# Return the number of observers associated with this object.
[152] Fix | Delete
#
[153] Fix | Delete
def count_observers
[154] Fix | Delete
if defined? @observer_peers
[155] Fix | Delete
@observer_peers.size
[156] Fix | Delete
else
[157] Fix | Delete
0
[158] Fix | Delete
end
[159] Fix | Delete
end
[160] Fix | Delete
[161] Fix | Delete
#
[162] Fix | Delete
# Set the changed state of this object. Notifications will be sent only if
[163] Fix | Delete
# the changed +state+ is +true+.
[164] Fix | Delete
#
[165] Fix | Delete
# +state+:: Boolean indicating the changed state of this Observable.
[166] Fix | Delete
#
[167] Fix | Delete
def changed(state=true)
[168] Fix | Delete
@observer_state = state
[169] Fix | Delete
end
[170] Fix | Delete
[171] Fix | Delete
#
[172] Fix | Delete
# Returns true if this object's state has been changed since the last
[173] Fix | Delete
# #notify_observers call.
[174] Fix | Delete
#
[175] Fix | Delete
def changed?
[176] Fix | Delete
if defined? @observer_state and @observer_state
[177] Fix | Delete
true
[178] Fix | Delete
else
[179] Fix | Delete
false
[180] Fix | Delete
end
[181] Fix | Delete
end
[182] Fix | Delete
[183] Fix | Delete
#
[184] Fix | Delete
# Notify observers of a change in state *if* this object's changed state is
[185] Fix | Delete
# +true+.
[186] Fix | Delete
#
[187] Fix | Delete
# This will invoke the method named in #add_observer, passing <tt>*arg</tt>.
[188] Fix | Delete
# The changed state is then set to +false+.
[189] Fix | Delete
#
[190] Fix | Delete
# <tt>*arg</tt>:: Any arguments to pass to the observers.
[191] Fix | Delete
def notify_observers(*arg)
[192] Fix | Delete
if defined? @observer_state and @observer_state
[193] Fix | Delete
if defined? @observer_peers
[194] Fix | Delete
@observer_peers.each do |k, v|
[195] Fix | Delete
k.send v, *arg
[196] Fix | Delete
end
[197] Fix | Delete
end
[198] Fix | Delete
@observer_state = false
[199] Fix | Delete
end
[200] Fix | Delete
end
[201] Fix | Delete
[202] Fix | Delete
end
[203] Fix | Delete
[204] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function