class Patterns::Subject
The object that will be observed.
Contains a list of observers that are watching this subject
Attributes
observers[R]
The list of observers this subject stores
Public Class Methods
new()
click to toggle source
Initializes the subject class
Examples
=> subject = Subject.new
# File app/models/observer/subject.rb, line 17 def initialize @observers = Array.new end
Public Instance Methods
add_observer(observer)
click to toggle source
Adds an observer to the list of observers
-
observer
- The observer to add to the list
Examples
=> subject_observer = Observer.new => subject.add_observer(subject_observer)
# File app/models/observer/subject.rb, line 29 def add_observer(observer) @observers << observer end
notify(notification, event)
click to toggle source
Notifies the observer of some change
-
notification
- The notification to send to the observers -
event
- The event to send to the observers
Examples
=> subject.notify('An event just happened!', the_event)
# File app/models/observer/subject.rb, line 52 def notify(notification, event) @observers.each do |observer| observer.on_notify(notification, event) end end
remove_observer(observer)
click to toggle source
Removes an observer from the list of observers
-
observer
- The observer to remove from the list
Examples
=> subject.remove_observer(subject_observer)
# File app/models/observer/subject.rb, line 40 def remove_observer(observer) @observers.delete(observer) end