Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby30/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
#
[113] Fix | Delete
# === Usage with procs
[114] Fix | Delete
#
[115] Fix | Delete
# The +#notify_observers+ method can also be used with +proc+s by using
[116] Fix | Delete
# the +:call+ as +func+ parameter.
[117] Fix | Delete
#
[118] Fix | Delete
# The following example illustrates the use of a lambda:
[119] Fix | Delete
#
[120] Fix | Delete
# require 'observer'
[121] Fix | Delete
#
[122] Fix | Delete
# class Ticker
[123] Fix | Delete
# include Observable
[124] Fix | Delete
#
[125] Fix | Delete
# def run
[126] Fix | Delete
# # logic to retrieve the price (here 77.0)
[127] Fix | Delete
# changed
[128] Fix | Delete
# notify_observers(77.0)
[129] Fix | Delete
# end
[130] Fix | Delete
# end
[131] Fix | Delete
#
[132] Fix | Delete
# ticker = Ticker.new
[133] Fix | Delete
# warner = ->(price) { puts "New price received: #{price}" }
[134] Fix | Delete
# ticker.add_observer(warner, :call)
[135] Fix | Delete
# ticker.run
[136] Fix | Delete
module Observable
[137] Fix | Delete
VERSION = "0.1.1"
[138] Fix | Delete
[139] Fix | Delete
#
[140] Fix | Delete
# Add +observer+ as an observer on this object. So that it will receive
[141] Fix | Delete
# notifications.
[142] Fix | Delete
#
[143] Fix | Delete
# +observer+:: the object that will be notified of changes.
[144] Fix | Delete
# +func+:: Symbol naming the method that will be called when this Observable
[145] Fix | Delete
# has changes.
[146] Fix | Delete
#
[147] Fix | Delete
# This method must return true for +observer.respond_to?+ and will
[148] Fix | Delete
# receive <tt>*arg</tt> when #notify_observers is called, where
[149] Fix | Delete
# <tt>*arg</tt> is the value passed to #notify_observers by this
[150] Fix | Delete
# Observable
[151] Fix | Delete
def add_observer(observer, func=:update)
[152] Fix | Delete
@observer_peers = {} unless defined? @observer_peers
[153] Fix | Delete
unless observer.respond_to? func
[154] Fix | Delete
raise NoMethodError, "observer does not respond to `#{func}'"
[155] Fix | Delete
end
[156] Fix | Delete
@observer_peers[observer] = func
[157] Fix | Delete
end
[158] Fix | Delete
[159] Fix | Delete
#
[160] Fix | Delete
# Remove +observer+ as an observer on this object so that it will no longer
[161] Fix | Delete
# receive notifications.
[162] Fix | Delete
#
[163] Fix | Delete
# +observer+:: An observer of this Observable
[164] Fix | Delete
def delete_observer(observer)
[165] Fix | Delete
@observer_peers.delete observer if defined? @observer_peers
[166] Fix | Delete
end
[167] Fix | Delete
[168] Fix | Delete
#
[169] Fix | Delete
# Remove all observers associated with this object.
[170] Fix | Delete
#
[171] Fix | Delete
def delete_observers
[172] Fix | Delete
@observer_peers.clear if defined? @observer_peers
[173] Fix | Delete
end
[174] Fix | Delete
[175] Fix | Delete
#
[176] Fix | Delete
# Return the number of observers associated with this object.
[177] Fix | Delete
#
[178] Fix | Delete
def count_observers
[179] Fix | Delete
if defined? @observer_peers
[180] Fix | Delete
@observer_peers.size
[181] Fix | Delete
else
[182] Fix | Delete
0
[183] Fix | Delete
end
[184] Fix | Delete
end
[185] Fix | Delete
[186] Fix | Delete
#
[187] Fix | Delete
# Set the changed state of this object. Notifications will be sent only if
[188] Fix | Delete
# the changed +state+ is +true+.
[189] Fix | Delete
#
[190] Fix | Delete
# +state+:: Boolean indicating the changed state of this Observable.
[191] Fix | Delete
#
[192] Fix | Delete
def changed(state=true)
[193] Fix | Delete
@observer_state = state
[194] Fix | Delete
end
[195] Fix | Delete
[196] Fix | Delete
#
[197] Fix | Delete
# Returns true if this object's state has been changed since the last
[198] Fix | Delete
# #notify_observers call.
[199] Fix | Delete
#
[200] Fix | Delete
def changed?
[201] Fix | Delete
if defined? @observer_state and @observer_state
[202] Fix | Delete
true
[203] Fix | Delete
else
[204] Fix | Delete
false
[205] Fix | Delete
end
[206] Fix | Delete
end
[207] Fix | Delete
[208] Fix | Delete
#
[209] Fix | Delete
# Notify observers of a change in state *if* this object's changed state is
[210] Fix | Delete
# +true+.
[211] Fix | Delete
#
[212] Fix | Delete
# This will invoke the method named in #add_observer, passing <tt>*arg</tt>.
[213] Fix | Delete
# The changed state is then set to +false+.
[214] Fix | Delete
#
[215] Fix | Delete
# <tt>*arg</tt>:: Any arguments to pass to the observers.
[216] Fix | Delete
def notify_observers(*arg)
[217] Fix | Delete
if defined? @observer_state and @observer_state
[218] Fix | Delete
if defined? @observer_peers
[219] Fix | Delete
@observer_peers.each do |k, v|
[220] Fix | Delete
k.__send__(v, *arg)
[221] Fix | Delete
end
[222] Fix | Delete
end
[223] Fix | Delete
@observer_state = false
[224] Fix | Delete
end
[225] Fix | Delete
end
[226] Fix | Delete
[227] Fix | Delete
end
[228] Fix | Delete
[229] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function