Implementation

A NodeSubscription implementation. Class: NodeSubscription.

Constants

Attributes

instance

A singleton instance.

feeds

All subscription feeds. Its elements are Hashes. The keys are Node references, and their values are Sets containing corresponding subscribers.

Methods

NodeSubscription.instance()

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

Lazily initializes a singleton instance, or gets the singleton instance. Returns the NodeSubscription singleton instance.

publisher(p = nil)

def publisher(p = nil)
feeds().to_a().each { |feed|
if (feed.key?(p)) then return true end
}
return false
end

Predicate. Verifies a Node is a feed publisher. Takes a publishable object argument, p. Returns true in the case p is a feeds key. false otherwise.

update_subscribers(publisher = nil)

def update_subscribers(publisher = nil)
unless (publisher(p))
raise(ArgumentError, "#{p} is not a publisher.")
else
p_feed = feed(p)
p_feed.each { |subscriber|
factory = kind_factory(subscriber)
singleton = factory.instance()
singleton.diagram_update(p)
}
end
return nil
end

Updates a publisher's subscribers. Takes a changed Node publisher argument, publisher. Updates publisher's subscribers. In the case publisher is not a feeds key, raises an ArgumentError. Returns nil.

add_publisher(p = nil)

def add_publisher(p = nil)
publishable_set = NodeSubscription.publishable()
p_class = p.class()
if (publisher(p))
raise(ArgumentError, "#{p} was added previously.")
elsif (!publishable_set.include?(p_class))
raise(ArgumentError, "#{p} is not a publishable object.")
else
feed_hash = {}
feed_hash[p] = Set[]
feeds().add(feed_hash)
end
return nil
end

Adds a Node publisher. Takes a Node argument, p. Creates a subscription feed. The feed's key is p, and the feed Set is empty.

add_subscriber(p = nil, s = nil)

def add_subscriber(p = nil, s = nil)
if ((!publisher(p)) || (feed(p).include?(s)) ||
(!NodeSubscription.s_instance(s)))
raise(ArgumentError,
"#{p} never registered as a publisher, or #{s} was previously
subscribed.")
else
p_feed = feed(p)
p_feed.add(s)
end
return nil
end

Adds a subscriber a feed. Takes a Node argument, p, and a subscribable instance, s. Adds the subscriber, s, the publisher p's feed. In the case p is not a feed publisher, or s is not a subscribable object, raises an ArgumentError.

feeds() protected

def feeds()
return @feeds
end

Gets feeds. Returns the Set reference.

feed(p = nil) protected

def feed(p = nil)
if (publisher(p))
feeds().to_a().each { |feed|
if (feed.key?(p)) then return feed[p] end
}
else
raise(ArgumentError, "#{p} was never added.")
end
end

Gets a publisher's feed. Takes a Node argument, p. Finds the publisher p's feed, and returns its Set reference. In the case p is not a feeds element Hash key, raises an ArgumentError.

feeds=(s = Set[]) private

def feeds=(s = Set[])
@feeds = s
end

Sets feeds. The parameter s defaults an empty Set. feeds calls once, in the singleton instance's initialization, and sets feeds the empty Set.

kind_factory(s = nil) private

def kind_factory(s = nil)
case
when s.instance_of?(NodeDiagram)
return DiagramFactory
else
raise(ArgumentError, "#{s}\'s factory is nonexistent.")
end
end

Gets a subscriber's factory identifier. Takes an argument subscriber, s. Discerns s's factory identifier, and returns the identifier.

initialize() private

def initialize()
self.feeds = Set[]
end

Initializes the singleton instance.

NodeSubscription.instance=(s = nil) private

def NodeSubscription.instance=(s = nil)
@instance = s
end

Sets the singleton instance. Takes a NodeSubscription instance argument, s. Sets instance the instance. Returns the argument NodeSubscription.

NodeSubscription.new() private, undefined