$22 GRAYBYTE WORDPRESS FILE MANAGER $81

SERVER : premium201.web-hosting.com #1 SMP Wed Mar 26 12:08:09 UTC 2025
SERVER IP : 172.67.217.254 | ADMIN IP 216.73.216.23
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : NONE

/opt/alt/ruby21/lib64/ruby/2.1.0/

HOME
Current File : /opt/alt/ruby21/lib64/ruby/2.1.0//observer.rb
#
# 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.
#
# == Mechanism
#
# The notifying class mixes in the +Observable+
# module, which provides the methods for managing the associated observer
# objects.
#
# 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.
#
# === Example
#
# 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.
#
#   require "observer"
#
#   class Ticker          ### Periodically fetch a stock price.
#     include Observable
#
#     def initialize(symbol)
#       @symbol = symbol
#     end
#
#     def run
#       last_price = nil
#       loop do
#         price = Price.fetch(@symbol)
#         print "Current price: #{price}\n"
#         if price != last_price
#           changed                 # notify observers
#           last_price = price
#           notify_observers(Time.now, price)
#         end
#         sleep 1
#       end
#     end
#   end
#
#   class Price           ### A mock class to fetch a stock price (60 - 140).
#     def self.fetch(symbol)
#       60 + rand(80)
#     end
#   end
#
#   class Warner          ### An abstract observer of Ticker objects.
#     def initialize(ticker, limit)
#       @limit = limit
#       ticker.add_observer(self)
#     end
#   end
#
#   class WarnLow < Warner
#     def update(time, price)       # callback for observer
#       if price < @limit
#         print "--- #{time.to_s}: Price below #@limit: #{price}\n"
#       end
#     end
#   end
#
#   class WarnHigh < Warner
#     def update(time, price)       # callback for observer
#       if price > @limit
#         print "+++ #{time.to_s}: Price above #@limit: #{price}\n"
#       end
#     end
#   end
#
#   ticker = Ticker.new("MSFT")
#   WarnLow.new(ticker, 80)
#   WarnHigh.new(ticker, 120)
#   ticker.run
#
# Produces:
#
#   Current price: 83
#   Current price: 75
#   --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 75
#   Current price: 90
#   Current price: 134
#   +++ Sun Jun 09 00:10:25 CDT 2002: Price above 120: 134
#   Current price: 134
#   Current price: 112
#   Current price: 79
#   --- Sun Jun 09 00:10:25 CDT 2002: Price below 80: 79
module Observable

  #
  # Add +observer+ as an observer on this object. so that it will receive
  # notifications.
  #
  # +observer+:: the object that will be notified of changes.
  # +func+:: Symbol naming the method that will be called when this Observable
  #          has changes.
  #
  #          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
  #          Observable
  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.to_s}'"
    end
    @observer_peers[observer] = func
  end

  #
  # Remove +observer+ as an observer on this object so that it will no longer
  # receive notifications.
  #
  # +observer+:: An observer of this Observable
  def delete_observer(observer)
    @observer_peers.delete observer if defined? @observer_peers
  end

  #
  # Remove all observers associated with this object.
  #
  def delete_observers
    @observer_peers.clear if defined? @observer_peers
  end

  #
  # Return the number of observers associated with this object.
  #
  def count_observers
    if defined? @observer_peers
      @observer_peers.size
    else
      0
    end
  end

  #
  # 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.
  #
  def changed(state=true)
    @observer_state = state
  end

  #
  # Returns true if this object's state has been changed since the last
  # #notify_observers call.
  #
  def changed?
    if defined? @observer_state and @observer_state
      true
    else
      false
    end
  end

  #
  # Notify observers of a change in state *if* this object's changed state is
  # +true+.
  #
  # 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|
          k.send v, *arg
        end
      end
      @observer_state = false
    end
  end

end


Current_dir [ NOT WRITEABLE ] Document_root [ NOT WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
3 Mar 2024 10.43 PM
root / root
0755
cgi
--
3 Mar 2024 10.43 PM
root / linksafe
0755
date
--
3 Mar 2024 10.43 PM
root / linksafe
0755
digest
--
3 Mar 2024 10.43 PM
root / linksafe
0755
dl
--
3 Mar 2024 10.43 PM
root / linksafe
0755
drb
--
3 Mar 2024 10.43 PM
root / linksafe
0755
fiddle
--
3 Mar 2024 10.43 PM
root / linksafe
0755
io
--
3 Mar 2024 10.43 PM
root / linksafe
0755
irb
--
3 Mar 2024 10.43 PM
root / linksafe
0755
json
--
3 Mar 2024 10.43 PM
root / linksafe
0755
matrix
--
3 Mar 2024 10.43 PM
root / linksafe
0755
net
--
3 Mar 2024 10.43 PM
root / linksafe
0755
openssl
--
3 Mar 2024 10.43 PM
root / linksafe
0755
optparse
--
3 Mar 2024 10.43 PM
root / linksafe
0755
psych
--
3 Mar 2024 10.43 PM
root / linksafe
0755
racc
--
3 Mar 2024 10.43 PM
root / linksafe
0755
rake
--
3 Mar 2024 10.53 PM
root / linksafe
0755
rbconfig
--
3 Mar 2024 10.43 PM
root / linksafe
0755
rdoc
--
3 Mar 2024 10.43 PM
root / linksafe
0755
rexml
--
3 Mar 2024 10.43 PM
root / linksafe
0755
rinda
--
3 Mar 2024 10.43 PM
root / linksafe
0755
ripper
--
3 Mar 2024 10.43 PM
root / linksafe
0755
rss
--
3 Mar 2024 10.43 PM
root / linksafe
0755
rubygems
--
3 Mar 2024 10.43 PM
root / linksafe
0755
shell
--
3 Mar 2024 10.43 PM
root / linksafe
0755
syslog
--
3 Mar 2024 10.43 PM
root / root
0755
test
--
3 Mar 2024 10.43 PM
root / linksafe
0755
uri
--
3 Mar 2024 10.43 PM
root / linksafe
0755
webrick
--
3 Mar 2024 10.43 PM
root / linksafe
0755
x86_64-linux
--
3 Mar 2024 10.43 PM
root / root
0755
xmlrpc
--
3 Mar 2024 10.43 PM
root / linksafe
0755
yaml
--
3 Mar 2024 10.43 PM
root / linksafe
0755
English.rb
6.443 KB
4 Feb 2013 2.50 AM
root / linksafe
0644
abbrev.rb
3.313 KB
21 Feb 2013 5.35 PM
root / linksafe
0644
base64.rb
2.631 KB
19 May 2013 3.10 AM
root / linksafe
0644
benchmark.rb
17.952 KB
20 Sep 2013 4.05 PM
root / linksafe
0644
cgi.rb
9.841 KB
18 Jul 2013 1.50 PM
root / linksafe
0644
cmath.rb
8.927 KB
3 Jul 2015 9.12 AM
root / linksafe
0644
complex.rb
0.371 KB
16 Aug 2009 3.34 PM
root / linksafe
0644
csv.rb
81.682 KB
14 Sep 2014 3.25 PM
root / linksafe
0644
date.rb
0.924 KB
26 Jul 2023 2.22 PM
root / linksafe
0644
debug.rb
29.077 KB
14 Dec 2013 2.48 PM
root / linksafe
0644
delegate.rb
11.125 KB
2 Feb 2014 1.46 PM
root / linksafe
0644
digest.rb
2.338 KB
26 Jul 2023 2.22 PM
root / linksafe
0644
dl.rb
0.273 KB
26 Jul 2023 2.22 PM
root / linksafe
0644
drb.rb
0.019 KB
2 Oct 2009 10.45 AM
root / linksafe
0644
e2mmap.rb
3.891 KB
17 Dec 2013 11.20 AM
root / linksafe
0644
erb.rb
26.073 KB
11 Aug 2014 2.55 PM
root / linksafe
0644
expect.rb
2.144 KB
26 Jul 2023 2.22 PM
root / linksafe
0644
fiddle.rb
1.652 KB
26 Jul 2023 2.22 PM
root / linksafe
0644
fileutils.rb
47.171 KB
28 Apr 2015 5.16 AM
root / linksafe
0644
find.rb
2.359 KB
15 Oct 2014 3.31 PM
root / linksafe
0644
forwardable.rb
7.863 KB
23 May 2013 9.46 PM
root / linksafe
0644
getoptlong.rb
15.381 KB
19 May 2013 2.50 PM
root / linksafe
0644
gserver.rb
8.856 KB
6 Jul 2014 1.57 PM
root / linksafe
0644
ipaddr.rb
17.051 KB
29 Oct 2015 2.36 PM
root / linksafe
0644
irb.rb
20.034 KB
25 Mar 2016 8.22 AM
root / linksafe
0644
json.rb
1.737 KB
26 Jul 2023 2.22 PM
root / linksafe
0644
kconv.rb
5.737 KB
26 Jul 2023 2.22 PM
root / linksafe
0644
logger.rb
22.371 KB
25 Feb 2016 11.04 AM
root / linksafe
0644
mathn.rb
6.524 KB
26 Aug 2011 10.22 PM
root / linksafe
0644
matrix.rb
45.432 KB
11 Jul 2014 2.08 PM
root / linksafe
0644
mkmf.rb
80.503 KB
26 Jul 2023 2.19 PM
root / linksafe
0644
monitor.rb
6.935 KB
16 Nov 2012 4.55 PM
root / linksafe
0644
mutex_m.rb
2.002 KB
20 Feb 2013 2.51 AM
root / linksafe
0644
observer.rb
5.807 KB
21 Nov 2013 4.44 AM
root / linksafe
0644
open-uri.rb
24.268 KB
22 Feb 2014 8.54 AM
root / linksafe
0644
open3.rb
20.366 KB
1 Dec 2013 3.13 AM
root / linksafe
0644
openssl.rb
0.516 KB
26 Jul 2023 2.22 PM
root / linksafe
0644
optparse.rb
50.796 KB
28 Nov 2013 7.34 AM
root / linksafe
0644
ostruct.rb
7.702 KB
23 Oct 2013 3.14 PM
root / linksafe
0644
pathname.rb
15.297 KB
26 Jul 2023 2.22 PM
root / linksafe
0644
pp.rb
14.097 KB
22 Oct 2013 9.29 AM
root / linksafe
0644
prettyprint.rb
16.331 KB
19 May 2013 3.10 AM
root / linksafe
0644
prime.rb
13.203 KB
15 Jul 2013 4.21 AM
root / linksafe
0644
profile.rb
0.2 KB
2 Oct 2009 10.45 AM
root / linksafe
0644
profiler.rb
4.509 KB
19 May 2013 11.04 PM
root / linksafe
0644
pstore.rb
14.849 KB
11 Nov 2012 4.23 AM
root / linksafe
0644
psych.rb
14.878 KB
26 Jul 2023 2.22 PM
root / linksafe
0644
rake.rb
2.116 KB
11 Oct 2013 9.35 PM
root / linksafe
0644
rational.rb
0.301 KB
24 Sep 2009 12.42 AM
root / linksafe
0644
rdoc.rb
4.921 KB
22 Dec 2013 10.25 AM
root / linksafe
0644
resolv-replace.rb
1.732 KB
11 Mar 2013 1.47 PM
root / linksafe
0644
resolv.rb
72.308 KB
13 May 2015 5.33 AM
root / linksafe
0644
ripper.rb
2.525 KB
26 Jul 2023 2.22 PM
root / linksafe
0644
rss.rb
2.841 KB
11 May 2011 10.22 AM
root / linksafe
0644
rubygems.rb
30.974 KB
26 Jul 2023 2.19 PM
root / linksafe
0644
scanf.rb
23.542 KB
14 Dec 2013 2.55 AM
root / linksafe
0644
securerandom.rb
8.49 KB
7 Nov 2013 6.04 PM
root / linksafe
0644
set.rb
18.704 KB
22 Nov 2013 11.50 PM
root / linksafe
0644
shell.rb
10.299 KB
19 May 2013 3.10 AM
root / linksafe
0644
shellwords.rb
5.941 KB
14 Dec 2013 6.26 PM
root / linksafe
0644
singleton.rb
4.018 KB
18 May 2011 2.09 PM
root / linksafe
0644
socket.rb
25.596 KB
26 Jul 2023 2.22 PM
root / linksafe
0644
sync.rb
7.255 KB
19 May 2013 3.10 AM
root / linksafe
0644
tempfile.rb
11.398 KB
21 Nov 2013 9.28 AM
root / linksafe
0644
thwait.rb
3.377 KB
7 Nov 2013 5.02 PM
root / linksafe
0644
time.rb
21.316 KB
17 Aug 2015 7.41 AM
root / linksafe
0644
timeout.rb
3.697 KB
17 Aug 2015 7.55 AM
root / linksafe
0644
tmpdir.rb
4.149 KB
19 May 2013 3.10 AM
root / linksafe
0644
tracer.rb
6.402 KB
18 Jul 2013 1.50 PM
root / linksafe
0644
tsort.rb
14.145 KB
17 Oct 2013 3.59 PM
root / linksafe
0644
ubygems.rb
0.262 KB
2 Oct 2009 10.45 AM
root / linksafe
0644
un.rb
8.873 KB
5 Jul 2013 1.43 PM
root / linksafe
0644
uri.rb
3.07 KB
13 May 2011 8.03 PM
root / linksafe
0644
weakref.rb
3.229 KB
9 Nov 2013 10.42 PM
root / linksafe
0644
webrick.rb
6.689 KB
5 Oct 2013 11.39 PM
root / linksafe
0644
xmlrpc.rb
8.488 KB
12 Dec 2013 3.09 AM
root / linksafe
0644
yaml.rb
2.313 KB
12 Aug 2013 3.49 AM
root / linksafe
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2025 CONTACT ME
Static GIF