Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: observer.rb
#
[0] Fix | Delete
# observer.rb implements 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.rubycentral.com/book/lib_patterns.html.
[3] Fix | Delete
#
[4] Fix | Delete
# == About
[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
# In the Ruby implementation, 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 observers must implement the +update+ method to receive notifications.
[17] Fix | Delete
#
[18] Fix | Delete
# The observable object must:
[19] Fix | Delete
# * assert that it has +changed+
[20] Fix | Delete
# * call +notify_observers+
[21] Fix | Delete
#
[22] Fix | Delete
# == Example
[23] Fix | Delete
#
[24] Fix | Delete
# The following example demonstrates this nicely. A +Ticker+, when run,
[25] Fix | Delete
# continually receives the stock +Price+ for its +@symbol+. A +Warner+ is a
[26] Fix | Delete
# general observer of the price, and two warners are demonstrated, a +WarnLow+
[27] Fix | Delete
# and a +WarnHigh+, which print a warning if the price is below or above their
[28] Fix | Delete
# set limits, respectively.
[29] Fix | Delete
#
[30] Fix | Delete
# The +update+ callback allows the warners to run without being explicitly
[31] Fix | Delete
# called. The system is set up with the +Ticker+ and several observers, and the
[32] Fix | Delete
# observers do their duty without the top-level code having to interfere.
[33] Fix | Delete
#
[34] Fix | Delete
# Note that the contract between publisher and subscriber (observable and
[35] Fix | Delete
# observer) is not declared or enforced. The +Ticker+ publishes a time and a
[36] Fix | Delete
# price, and the warners receive that. But if you don't ensure that your
[37] Fix | Delete
# contracts are correct, nothing else can warn you.
[38] Fix | Delete
#
[39] Fix | Delete
# require "observer"
[40] Fix | Delete
#
[41] Fix | Delete
# class Ticker ### Periodically fetch a stock price.
[42] Fix | Delete
# include Observable
[43] Fix | Delete
#
[44] Fix | Delete
# def initialize(symbol)
[45] Fix | Delete
# @symbol = symbol
[46] Fix | Delete
# end
[47] Fix | Delete
#
[48] Fix | Delete
# def run
[49] Fix | Delete
# lastPrice = nil
[50] Fix | Delete
# loop do
[51] Fix | Delete
# price = Price.fetch(@symbol)
[52] Fix | Delete
# print "Current price: #{price}\n"
[53] Fix | Delete
# if price != lastPrice
[54] Fix | Delete
# changed # notify observers
[55] Fix | Delete
# lastPrice = price
[56] Fix | Delete
# notify_observers(Time.now, price)
[57] Fix | Delete
# end
[58] Fix | Delete
# sleep 1
[59] Fix | Delete
# end
[60] Fix | Delete
# end
[61] Fix | Delete
# end
[62] Fix | Delete
#
[63] Fix | Delete
# class Price ### A mock class to fetch a stock price (60 - 140).
[64] Fix | Delete
# def Price.fetch(symbol)
[65] Fix | Delete
# 60 + rand(80)
[66] Fix | Delete
# end
[67] Fix | Delete
# end
[68] Fix | Delete
#
[69] Fix | Delete
# class Warner ### An abstract observer of Ticker objects.
[70] Fix | Delete
# def initialize(ticker, limit)
[71] Fix | Delete
# @limit = limit
[72] Fix | Delete
# ticker.add_observer(self)
[73] Fix | Delete
# end
[74] Fix | Delete
# end
[75] Fix | Delete
#
[76] Fix | Delete
# class WarnLow < Warner
[77] Fix | Delete
# def update(time, price) # callback for observer
[78] Fix | Delete
# if price < @limit
[79] Fix | Delete
# print "--- #{time.to_s}: Price below #@limit: #{price}\n"
[80] Fix | Delete
# end
[81] Fix | Delete
# end
[82] Fix | Delete
# end
[83] Fix | Delete
#
[84] Fix | Delete
# class WarnHigh < Warner
[85] Fix | Delete
# def update(time, price) # callback for observer
[86] Fix | Delete
# if price > @limit
[87] Fix | Delete
# print "+++ #{time.to_s}: Price above #@limit: #{price}\n"
[88] Fix | Delete
# end
[89] Fix | Delete
# end
[90] Fix | Delete
# end
[91] Fix | Delete
#
[92] Fix | Delete
# ticker = Ticker.new("MSFT")
[93] Fix | Delete
# WarnLow.new(ticker, 80)
[94] Fix | Delete
# WarnHigh.new(ticker, 120)
[95] Fix | Delete
# ticker.run
[96] Fix | Delete
#
[97] Fix | Delete
# Produces:
[98] Fix | Delete
#
[99] Fix | Delete
# Current price: 83
[100] Fix | Delete
# Current price: 75
[101] Fix | Delete
# --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 75
[102] Fix | Delete
# Current price: 90
[103] Fix | Delete
# Current price: 134
[104] Fix | Delete
# +++ Sun Jun 09 00:10:25 CDT 2002: Price above 120: 134
[105] Fix | Delete
# Current price: 134
[106] Fix | Delete
# Current price: 112
[107] Fix | Delete
# Current price: 79
[108] Fix | Delete
# --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 79
[109] Fix | Delete
[110] Fix | Delete
[111] Fix | Delete
#
[112] Fix | Delete
# Implements the Observable design pattern as a mixin so that other objects can
[113] Fix | Delete
# be notified of changes in state. See observer.rb for details and an example.
[114] Fix | Delete
#
[115] Fix | Delete
module Observable
[116] Fix | Delete
[117] Fix | Delete
#
[118] Fix | Delete
# Add +observer+ as an observer on this object. +observer+ will now receive
[119] Fix | Delete
# notifications.
[120] Fix | Delete
#
[121] Fix | Delete
def add_observer(observer)
[122] Fix | Delete
@observer_peers = [] unless defined? @observer_peers
[123] Fix | Delete
unless observer.respond_to? :update
[124] Fix | Delete
raise NoMethodError, "observer needs to respond to `update'"
[125] Fix | Delete
end
[126] Fix | Delete
@observer_peers.push observer
[127] Fix | Delete
end
[128] Fix | Delete
[129] Fix | Delete
#
[130] Fix | Delete
# Delete +observer+ as an observer on this object. It will no longer receive
[131] Fix | Delete
# notifications.
[132] Fix | Delete
#
[133] Fix | Delete
def delete_observer(observer)
[134] Fix | Delete
@observer_peers.delete observer if defined? @observer_peers
[135] Fix | Delete
end
[136] Fix | Delete
[137] Fix | Delete
#
[138] Fix | Delete
# Delete all observers associated with this object.
[139] Fix | Delete
#
[140] Fix | Delete
def delete_observers
[141] Fix | Delete
@observer_peers.clear if defined? @observer_peers
[142] Fix | Delete
end
[143] Fix | Delete
[144] Fix | Delete
#
[145] Fix | Delete
# Return the number of observers associated with this object.
[146] Fix | Delete
#
[147] Fix | Delete
def count_observers
[148] Fix | Delete
if defined? @observer_peers
[149] Fix | Delete
@observer_peers.size
[150] Fix | Delete
else
[151] Fix | Delete
0
[152] Fix | Delete
end
[153] Fix | Delete
end
[154] Fix | Delete
[155] Fix | Delete
#
[156] Fix | Delete
# Set the changed state of this object. Notifications will be sent only if
[157] Fix | Delete
# the changed +state+ is +true+.
[158] Fix | Delete
#
[159] Fix | Delete
def changed(state=true)
[160] Fix | Delete
@observer_state = state
[161] Fix | Delete
end
[162] Fix | Delete
[163] Fix | Delete
#
[164] Fix | Delete
# Query the changed state of this object.
[165] Fix | Delete
#
[166] Fix | Delete
def changed?
[167] Fix | Delete
if defined? @observer_state and @observer_state
[168] Fix | Delete
true
[169] Fix | Delete
else
[170] Fix | Delete
false
[171] Fix | Delete
end
[172] Fix | Delete
end
[173] Fix | Delete
[174] Fix | Delete
#
[175] Fix | Delete
# If this object's changed state is +true+, invoke the update method in each
[176] Fix | Delete
# currently associated observer in turn, passing it the given arguments. The
[177] Fix | Delete
# changed state is then set to +false+.
[178] Fix | Delete
#
[179] Fix | Delete
def notify_observers(*arg)
[180] Fix | Delete
if defined? @observer_state and @observer_state
[181] Fix | Delete
if defined? @observer_peers
[182] Fix | Delete
for i in @observer_peers.dup
[183] Fix | Delete
i.update(*arg)
[184] Fix | Delete
end
[185] Fix | Delete
end
[186] Fix | Delete
@observer_state = false
[187] Fix | Delete
end
[188] Fix | Delete
end
[189] Fix | Delete
[190] Fix | Delete
end
[191] Fix | Delete
[192] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function