Module: ObservableThing

Included in:
Jabber::Observable
Defined in:
lib/observable_thing.rb

Overview

This was based on Observable module from Ruby.

Instance Method Summary collapse

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).



12
13
14
15
16
17
18
19
# File 'lib/observable_thing.rb', line 12

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).



68
69
70
71
# File 'lib/observable_thing.rb', line 68

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.

Returns:

  • (Boolean)


76
77
78
79
80
81
82
# File 'lib/observable_thing.rb', line 76

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.



59
60
61
62
# File 'lib/observable_thing.rb', line 59

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).



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/observable_thing.rb', line 43

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.



25
26
27
# File 'lib/observable_thing.rb', line 25

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).



32
33
34
35
36
37
38
# File 'lib/observable_thing.rb', line 32

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”.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/observable_thing.rb', line 90

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?

Returns:

  • (Boolean)


108
109
110
111
# File 'lib/observable_thing.rb', line 108

def pending_notifications?
	return false if ! defined? @thread_store
	@thread_store.size > 0
end

#wait_notificationsObject

Wait all notifications



114
115
116
# File 'lib/observable_thing.rb', line 114

def wait_notifications
	sleep 1 while pending_notifications?
end