mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
c0e98a1384
* ostruct.rb: ditto. fix a bug in inspect which called String#+ with Symbol. [ruby-dev:18788] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3044 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
46 lines
1,014 B
Ruby
46 lines
1,014 B
Ruby
# Observable Mixin
|
|
#
|
|
# Observers must respond to update
|
|
|
|
module Observable
|
|
def add_observer(observer)
|
|
@observer_peers = [] unless defined? @observer_peers
|
|
unless observer.respond_to? :update
|
|
raise NoMethodError, "observer needs to respond to `update'"
|
|
end
|
|
@observer_peers.push observer
|
|
end
|
|
def delete_observer(observer)
|
|
@observer_peers.delete observer if defined? @observer_peers
|
|
end
|
|
def delete_observers
|
|
@observer_peers.clear if defined? @observer_peers
|
|
end
|
|
def count_observers
|
|
if defined? @observer_peers
|
|
@observer_peers.size
|
|
else
|
|
0
|
|
end
|
|
end
|
|
def changed(state=true)
|
|
@observer_state = state
|
|
end
|
|
def changed?
|
|
if defined? @observer_state and @observer_state
|
|
true
|
|
else
|
|
false
|
|
end
|
|
end
|
|
def notify_observers(*arg)
|
|
if defined? @observer_state and @observer_state
|
|
if defined? @observer_peers
|
|
for i in @observer_peers.dup
|
|
i.update(*arg)
|
|
end
|
|
end
|
|
@observer_state = false
|
|
end
|
|
end
|
|
end
|