Module: IronNails::Core::Observable
- Included in:
- IronNails::Controller::Base, View::Command, View::View, View::XamlProxy
- Defined in:
- lib/ironnails/observable.rb
Instance Method Summary collapse
-
#add_observer(event, &observer) ⇒ Object
Add
observer
as an observer on this object. -
#count_observers ⇒ Object
Return the number of observers associated with this object.
-
#delete_observer(event, &observer) ⇒ Object
Delete
observer
as an observer on this object. -
#delete_observers ⇒ Object
Delete all observers associated with this object.
-
#notify_observers(event, sender, *args) ⇒ Object
Notifies the registered observers that some interesting
event
has occurred.
Instance Method Details
#add_observer(event, &observer) ⇒ Object
Add observer
as an observer on this object. observer
will now receive notifications. observer
is interest in the specified event
14 15 16 17 18 19 20 |
# File 'lib/ironnails/observable.rb', line 14 def add_observer(event, &observer) @observers = [] unless defined? @observers unless observer.respond_to? :call raise NoMethodError, "observer needs to respond to 'update'" end @observers << { :event => event, :observer => observer } end |
#count_observers ⇒ Object
Return the number of observers associated with this object.
41 42 43 44 45 46 47 |
# File 'lib/ironnails/observable.rb', line 41 def count_observers if defined? @observers @observers.size else 0 end end |
#delete_observer(event, &observer) ⇒ Object
Delete observer
as an observer on this object. It will no longer receive notifications of the specified event
.
26 27 28 29 |
# File 'lib/ironnails/observable.rb', line 26 def delete_observer(event, &observer) evt = { :event => event, :observer => observer } @observers.delete evt if defined? @observers end |
#delete_observers ⇒ Object
Delete all observers associated with this object.
34 35 36 |
# File 'lib/ironnails/observable.rb', line 34 def delete_observers @observers.clear if defined? @observers end |
#notify_observers(event, sender, *args) ⇒ Object
Notifies the registered observers that some interesting event
has occurred. It will notify the interested parties by calling the block and passing it some context
54 55 56 |
# File 'lib/ironnails/observable.rb', line 54 def notify_observers(event, sender, *args) @observers.select {|evt| evt[:event] == event }.each {|evt| evt[:observer].call sender, *args } unless count_observers.zero? end |