Module: ObservableThing
- Included in:
- Jabber::Observable
- Defined in:
- lib/xmpp4r/observable/observable_thing.rb
Overview
XMPP4R - XMPP Library for Ruby
This file’s copyright © 2009 by Pablo Lorenzzoni <[email protected]>
- License
-
Ruby’s license (see the LICENSE file) or GNU GPL, at your option.
Website::xmpp4r.github.io
This was based on Observable module from Ruby.
Instance Method Summary collapse
-
#add_observer(thing, observer, func = :update) ⇒ Object
Adds an observer for some “thing”.
-
#changed(thing, state = true) ⇒ Object
Change the state of some “thing”.
-
#changed?(thing) ⇒ Boolean
Check the state of some “thing”.
-
#count_notifications(thing) ⇒ Object
Count the number of notifications for some “thing”.
-
#count_observers(thing = nil) ⇒ Object
Count the number of observers for some “thing”.
-
#delete_observer(thing, observer) ⇒ Object
Deletes an observer for some “thing”.
-
#delete_observers(thing = nil) ⇒ Object
Delete observers for some “thing”.
-
#notify_observers(thing, *arg) ⇒ Object
Notify all observers of “thing” about something.
-
#pending_notifications? ⇒ Boolean
Is there pending notifications?.
-
#wait_notifications ⇒ Object
Wait all notifications.
Instance Method Details
#add_observer(thing, observer, func = :update) ⇒ Object
Adds an observer for some “thing”.
- thing
-
what will be observed.
- observer
-
the observer.
- func
-
the observer method that will be called (default: :update).
16 17 18 19 20 21 22 23 |
# File 'lib/xmpp4r/observable/observable_thing.rb', line 16 def add_observer(thing, observer, func = :update) @things = {} unless defined? @things @things[thing] = {} unless ! @things[thing].nil? unless observer.respond_to? func raise NoMethodError, "observer does not respond to `#{func.to_s}'" end @things[thing][observer] = func unless @things[thing].include?(observer) end |
#changed(thing, state = true) ⇒ Object
Change the state of some “thing”.
- thing
-
what will have the state changed.
- state
-
the state (default = true).
72 73 74 75 |
# File 'lib/xmpp4r/observable/observable_thing.rb', line 72 def changed(thing, state = true) @things_state = {} unless defined? @things_state @things_state[thing] = state end |
#changed?(thing) ⇒ Boolean
Check the state of some “thing”.
thing: what to have its state checked.
80 81 82 83 84 85 86 |
# File 'lib/xmpp4r/observable/observable_thing.rb', line 80 def changed?(thing) if defined? @things_state and @things_state[thing] true else false end end |
#count_notifications(thing) ⇒ Object
Count the number of notifications for some “thing”.
- thing
-
what has been observed.
63 64 65 66 |
# File 'lib/xmpp4r/observable/observable_thing.rb', line 63 def count_notifications(thing) return 0 if (! defined?(@things_counter)) or (! @things_counter.include?(thing)) @things_counter[thing] end |
#count_observers(thing = nil) ⇒ Object
Count the number of observers for some “thing”.
- thing
-
what has been observed (if nil, count all observers).
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/xmpp4r/observable/observable_thing.rb', line 47 def count_observers(thing = nil) return 0 if ! defined? @things size = 0 if thing.nil? @things.each { |thing, hash| size += hash.size } else size = @things[thing].size unless @things[thing].nil? end size end |
#delete_observer(thing, observer) ⇒ Object
Deletes an observer for some “thing”.
- thing
-
what has been observed.
- observer
-
the observer.
29 30 31 |
# File 'lib/xmpp4r/observable/observable_thing.rb', line 29 def delete_observer(thing, observer) @things[thing].delete observer if defined? @things and ! @things[thing].nil? end |
#delete_observers(thing = nil) ⇒ Object
Delete observers for some “thing”.
- thing
-
what has been observed (if nil, deletes all observers).
36 37 38 39 40 41 42 |
# File 'lib/xmpp4r/observable/observable_thing.rb', line 36 def delete_observers(thing = nil) if thing.nil? @things.clear if defined? @things else @things[thing].clear if defined? @things and ! @things[thing].nil? end end |
#notify_observers(thing, *arg) ⇒ Object
Notify all observers of “thing” about something. This will only be enforced if the state of that “thing” is true. Also, if the observer returns the Symbol :delete_me, it will be deleted after being notified.
- thing
-
what has been observed.
- args
-
notification to be sent to the observers of “thing”.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/xmpp4r/observable/observable_thing.rb', line 94 def notify_observers(thing, *arg) if changed?(thing) if defined? @things and ! @things[thing].nil? @things[thing].each { |observer, func| increase_counter(thing) @thread_store = ThreadStore.new if ! defined? @thread_store @thread_store.add Thread.new { if observer.send(func, thing, *arg) == :delete_me delete_observer(thing, observer) end } } end changed(thing, false) end end |
#pending_notifications? ⇒ Boolean
Is there pending notifications?
112 113 114 115 |
# File 'lib/xmpp4r/observable/observable_thing.rb', line 112 def pending_notifications? return false if ! defined? @thread_store @thread_store.size > 0 end |
#wait_notifications ⇒ Object
Wait all notifications
118 119 120 |
# File 'lib/xmpp4r/observable/observable_thing.rb', line 118 def wait_notifications sleep 1 while pending_notifications? end |