Implementation

Class: NodeObserver.

Constants

Attributes

observing

A subjects set.

changed

A changed subjects set.

Methods

NodeObserver.instance()

def NodeObserver.instance()
if (@instance.nil?())
self.instance = new()
end
return @instance
end

Gets instance. Lazily initializes instance. Returns the singleton NodeObserver.

subject(n = nil)

def subject(n = nil)
return (observing().include?(n))
end

Predicate. Verifies a Node is a subject. Takes an argument Node, n. Returns true in the case the argument is an observing element. Returns false otherwise.

changed_node(subject = nil)

def changed_node(subject = nil)
return (changed().include?(subject))
end

Predicate. Verifies a subject was changed. Takes a Node argument, subject. Returns true in the case the argument is a changed element. Returns false otherwise.

subject_changed(subject = nil)

def subject_changed(subject = nil)
receive_change(subject)
return nil
end

Adds an existing subject the changed set. Takes a subject argument, subject. Receives the change, adds the subject the changed set, notifies the subject's subscribers, and deletes changed's element reference. Returns nil.

add(n = nil)

def add(n = nil)
unless (n.instance_of?(Node))
raise(ArgumentError, "#{n} is not a Node instance.")
else
observing().add(n)
end
return nil
end

Adds observing a subject. Takes an addend argument, n, and adds observing the argument. Returns nil.

remove(n = nil)

def remove(n = nil)
unless (subject(n))
raise(ArgumentError, "#{n} is not a subject.")
else
observing().delete(n)
end
return nil
end

Removes a subject. Takes a Node argument, n, the removal. Removes the observing element. Returns nil.

observing() protected

def observing()
return @observing
end

Gets observing. Returns the Set reference.

changed() protected

def changed()
return @changed
end

Gets changed. Returns the Set reference.

add_changed(n = nil) protected

def add_changed(n = nil)
unless (subject(n))
raise(ArgumentError, "#{n} is not a subject.")
else
changed().add(n)
end
return nil
end

Adds changed a changed subject. Takes a changed subject Node, n. Adds changed the argument. Returns nil.

remove_changed(n = nil) protected

def remove_changed(n = nil)
unless (changed_node(n))
raise(ArgumentError, "#{n} is not a changed subject.")
else
changed().delete(n)
end
return nil
end

Removes a changed subject. Takes a changed element argument, n. Deletes changed's element reference. Returns nil.

notify(n = nil) protected

def notify(n = nil)
Subscription.update(n)
remove_changed(n)
return nil
end

Updates a subject's subscribers. Takes a changed subject argument, n. Updates the subscription, and removes changed's element. Returns nil.

observing=(s = nil) private

def observing=(s = nil)
@observing = s
end

Sets observing. Takes an empty Set, s. Assigns observing the Set. Returns the Set reference.

changed=(s = nil) private

def changed=(s = nil)
@changed = s
end

Sets changed. Takes an empty Set, s. Assigns changed the Set. Returns the Set reference.

receive_change(n = nil) private

def receive_change(n = nil)
add_changed(n)
notify(n)
return nil
end

Receives a Node's state change. Takes a changed subject argument, n. Adds changed the subject's reference, and notifies the subscribers. Returns nil.

initialize() private

def initialize()
self.observing = Set[]
self.changed = Set[]
end

Initializes the singleton instance. Returns the NodeObserver singleton.

NodeObserver.instance=(singleton = nil) private

def NodeObserver.instance=(singleton = nil)
@instance = singleton
return @instance
end

Sets instance. Takes a NodeObserver argument and the singleton instance, singleton . Sets instance the argument. Returns the argument NodeObserver.

NodeObserver.new() private, undefined