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 <pablo@propus.com.br>

License

Ruby's license (see the LICENSE file) or GNU GPL, at your option.

Website::home.gna.org/xmpp4r/

This was based on Observable module from Ruby.

Instance Method Summary (collapse)

Instance Method Details

- (Object) add_observer(thing, observer, func = :update)

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

- (Object) changed(thing, state = true)

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

- (Boolean) changed?(thing)

Check the state of some “thing”.

thing: what to have its state checked.

Returns:

  • (Boolean)


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

- (Object) count_notifications(thing)

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

- (Object) count_observers(thing = nil)

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

- (Object) delete_observer(thing, observer)

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

- (Object) delete_observers(thing = nil)

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

- (Object) notify_observers(thing, *arg)

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

- (Boolean) pending_notifications?

Is there pending notifications?

Returns:

  • (Boolean)


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

- (Object) wait_notifications

Wait all notifications



118
119
120
# File 'lib/xmpp4r/observable/observable_thing.rb', line 118

def wait_notifications
  sleep 1 while pending_notifications?
end