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