Implementation

Class: DiagramFactory.

Constants

Attributes

instance

A DiagramFactory singleton instance.

inventory

An assembly inventory. The elements are Hashes. The keys are diagrammable instances, and the values are Sets, or, in the case a diagrammable's class has no children classes, the value is a single Diagram. If a key's value is a Set, it contains the diagrammable's kind Diagram Hashes. A diagrammable's inventory Set's element Hash keys are the kinds' String identifiers, and their corresponding values are the kinds' corresponding Diagrams.

Methods

DiagramFactory.instance()

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

Lazily initializes an instance. In the case the singleton already exists, gets the instance. Otherwise, instantiates and returns the instance. Returns the DiagramFactory singleton instance.

diagram(diagrammable = nil)

def diagram(diagrammable = nil)
if (!Diagram.verify_diagrammable(diagrammable))
raise(ArgumentError, "#{diagrammable} is not diagrammable.")
else
case
when diagram_exists(diagrammable) && k_diagram_exists(diagrammable)
return kind_diagram(diagrammable)
when diagram_exists(diagrammable)
return diagrams(diagrammable)
else
assembly = Diagram.build(diagrammable)
store_diagram(diagrammable, assembly)
return assembly
end
end
end

Gets a diagrammable object's Diagram. In the case none exist, assembles one. Takes a diagrammable argument, diagrammable. Returns the existing Diagram or the assembled Diagram.

diagram_update(publisher = nil)

def diagram_update(publisher = nil)
return reassemble(publisher)
end

Reassembles a diagrammable's Diagram. Updates publisher's inventory. Takes a diagrammable argument, publisher. Returns the reassembled Diagram.

diagram_exists(diagrammable = nil)

def diagram_exists(diagrammable = nil)
inventory().each { |model|
if (model.key?(diagrammable)) then return(true) end
}
return false
end

Predicate. Verifies a diagrammable object's Diagram was assembled. Takes a diagrammable object argument, diagrammable. Returns true in the case inventory contains an identical Hash key. false otherwise.

k_diagram_exists(diagrammable = nil)

def k_diagram_exists(diagrammable = nil)
case
when diagrammable.instance_of?(Node)
kind = diagrammable.kind()
if (diagram_exists(diagrammable))
diagram_set = diagrams(diagrammable)
kind_key_exists = false
diagram_set.to_a().each { |model|
if (model.key?(kind)) then kind_key_exists = true end
}
return (diagram_set.instance_of?(Set) && kind_key_exists)
else
return false
end
end
end

Predicate. Verifies the diagrammable's kind Diagram exists. Takes a diagrammable object argument, diagrammable. Returns true in the case diagrammable's kind Diagram exists. false otherwise.

inventory() private

def inventory()
return @inventory
end

Gets inventory. Returns inventory's Set reference.

inventory=(s = nil) private

def inventory=(s = nil)
@inventory = Set[]
end

Sets inventory. DiagramFactory calls the method once, during the singleton's initialization. Returns the empty Set.

reassemble(diagrammable = nil) private

def reassemble(diagrammable = nil)
if (!Diagram.verify_diagrammable(diagrammable))
raise(ArgumentError, "#{diagrammable} is not diagrammable.")
else
assembly = Diagram.build(diagrammable)
if (k_diagram_exists(diagrammable))
d_kind = diagrammable.kind()
diagram_set = diagrams(diagrammable)
diagram_set.to_a().each { |diagram|
if (diagram.key?(d_kind))
diagram_set.delete(diagram)
store_diagram(diagrammable, assembly)
end
}
else
store_diagram(diagrammable, assembly)
end
return assembly
end
end

Reassembles a Diagram. Takes a diagrammable object argument, diagrammable. Locates the appropriate Diagram, removes the stale Diagram from diagrammable's inventory, and adds the reassembly. Returns the Diagram reassembly. In the case the argument is not diagrammable, raises an ArgumentError.

diagrams(diagrammable = nil) private

def diagrams(diagrammable = nil)
if (diagram_exists(diagrammable))
inventory().to_a().each { |model|
if (model.key?(diagrammable)) then return model[diagrammable] end
}
else
raise(ArgumentError,
"#{diagrammable}'s diagram was never assembled.'")
end
end

Gets a diagrammable's Diagrams or Diagram. Takes a diagrammable object argument, diagrammable. Returns diagrammable's diagram Set or Diagram instance. Raises an ArgumentError in the case diagrammable is not an inventory key.

kind_diagram(diagrammable = nil) private

def kind_diagram(diagrammable = nil)
if (k_diagram_exists(diagrammable))
d_set = diagrams(diagrammable)
case
when diagrammable.instance_of?(Node)
arg_kind = diagrammable.kind()
d_set.to_a().each { |diagram|
if (diagram.key?(arg_kind)) then return diagram[arg_kind] end
}
end
else
raise(ArgumentError,
"#{diagrammable}'s kind diagram was never assembled.'")
end
end

Gets a diagrammable's kind Diagram. Takes any diagrammable object argument, diagrammable. Returns diagrammable's kind Diagram. In the case the kind Diagram is not an inventory value, raises an ArgumentError.

store_diagram(diagrammable = nil, diagram = nil) private

def store_diagram(diagrammable = nil, diagram = nil)
case
when diagrammable.instance_of?(Node)
kind = diagrammable.kind()
kind_h = {}
kind_h[kind] = diagram
if (!diagram_exists(diagrammable))
model = {}
model[diagrammable] = Set[kind_h]
inventory().add(model)
elsif (!k_diagram_exists(diagrammable))
d_set = diagrams(diagrammable)
d_set.add(kind_h)
end
end
return nil
end

Stores a diagrammable's Diagram. Takes a diagrammable object argument, diagrammable, and a Diagram instance argument, diagram. In the case diagrammable was never assembled, adds a model Hash enclosing a kind Set. model's key is diagrammable, and its value is the kind Set. The Set's elements are kind Diagrams. In the case diagrammable was previously assembled, the method finds the existing Set and adds a kind Hash. The kind Hash's key is diagrammable's kind String identifier, and its value is diagram.

initialize() private

def initialize()
self.inventory = ()
end

Initializes the singleton instance.

DiagramFactory.instance=(singleton = nil) private

def DiagramFactory.instance=(singleton = nil)
@instance = singleton
end

Sets instance. instance refers DiagramFactory's singleton instance. Returns the DiagramFactory singleton instance.

DiagramFactory.new() private, undefined