Implementation

Class: NodeDiagram.

Constants

BASE_LABEL private

A 'base' frozen String label.

DATA_LABEL private

A 'data' frozen String label.

BL_LENGTH private

The 'base' and 'lone' body length. The length is 31 characters.

CP_LENGTH private

The 'common' and 'pioneer' diagram body length. The length is 26 characters.

NEWLINE private

The newline character, frozen.

Attributes

node_string

The initializing Node's to_s() return.

data_text

The lower row's body text.

dl_padding

The data text's left padding.

dr_padding

The data text's right padding.

kind

The Node kind. Either lone, base, common, or pioneer.

Methods

NodeDiagram.base_label()

def NodeDiagram.base_label()
return BASE_LABEL
end

Gets BASE_LABEL. Returns BASE_LABEL's String reference. Mutating BASE_LABEL's reference raises a FrozenError.

NodeDiagram.data_label()

def NodeDiagram.data_label()
return DATA_LABEL
end

Gets DATA_LABEL. Returns DATA_LABEL's String reference. Mutating DATA_LABEL's reference raises a FrozenError.

NodeDiagram.bl_length()

def NodeDiagram.bl_length()
return BL_LENGTH
end

Gets BL_LENGTH. Returns BL_LENGTH's String reference. Mutating BL_LENGTH's reference raises a FrozenError.

NodeDiagram.cp_length()

def NodeDiagram.cp_length()
return CP_LENGTH
end

Gets CP_LENGTH. Returns CP_LENGTH's Integer.

NodeDiagram.newline()

def NodeDiagram.newline()
return NEWLINE
end

Gets NEWLINE. Returns NEWLINE's String reference. Mutating the reference raises a FrozenError.

initialize(n = nil)

def initialize(n = nil)
self.node_string = n.to_s()
self.data_text = n.data()
self.kind = kind_strategy(n)
self.dl_padding = Diagram.space() * dlp_quantity()
self.dr_padding = Diagram.space() * drp_quantity()
end

Initializes a NodeDiagram. Takes any Node instance, n. Sets node_string the Node's to_s() return, data_text the Node's data() return, kind the Node's kind String, dl_padding a space multiplied, and dr_padding a space multiplied. Returns the initialized NodeDiagram.

node_string()

def node_string()
return @node_string
end

Gets node_string. Returns the String reference.

data_text()

def data_text()
return @data_text
end

Gets data_text. Returns the String reference.

dl_padding()

def dl_padding()
return @dl_padding
end

Gets dl_padding. Returns the String reference.

dr_padding()

def dr_padding()
return @dr_padding
end

Gets dr_padding. Returns the String reference.

kind()

def kind()
return @kind
end

Gets kind. Returns the String reference.

lower_text(data = nil)

def lower_text(data = nil)
text = NodeDiagram.data_label() + NodeDiagram.space() + "#{data}"
return text
end

Concatenates the lower row text. Takes the initializing Node's data argument, data. Concatenates the data label, a space character, and the data's String conversion. Returns the concatenation.

upper_text()

def upper_text()
if (kind().eql?('base') || kind().eql?('lone'))
return (NodeDiagram.base_label() + NodeDiagram.space() +
node_string())
else
return node_string()
end
end

Concatenates the upper row's text. In the case the initializing Node's kind is base or lone, concatenates the base label, a space character, and node_string. Otherwise, returns node_string. Returns the concatenation or node_string.

dlp_quantity()

def dlp_quantity()
quantity = 0
dt_length = data_text().length()
if (kind().eql?('base') || kind().eql?('lone'))
quantity = (NodeDiagram.bl_length() - dt_length) / 2
else
quantity = (NodeDiagram.cp_length() - dt_length) / 2
end
return quantity
end

Computes the lower body's left padding quantity. In the case the initializing Node's kind is base or lone, the quantity is half the base-lone length less data_text's length. Otherwise, the quantity is half the common-pioneer length less data_text's length. Returns the Integer quantity.

drp_quantity()

def drp_quantity()
quantity = 0
remaining_space = 0
dt_length = data_text().length()
if (kind().eql?('base') || kind().eql?('lone'))
remaining_space = NodeDiagram.bl_length() - dt_length
quantity = remaining_space / 2
if (remaining_space.odd?()) then quantity += 1 end
else
remaining_space = NodeDiagram.cp_length() - dt_length
quantity = remaining_space / 2
if (remaining_space.odd?()) then quantity += 1 end
end
return quantity
end

Computes the lower body's right padding quantity. The quantity is half the available padding space. In the case the available space is odd, then the quantity is one greater than half the available padding space. The beginning available space quantity is 31 for bases and loners, and 26 for commoners and pioneers.

lower_body()

def lower_body()
return (dl_padding() + data_text() + dr_padding())
end

Concatenates the lower row body's parts. Concatenates dl_padding, data_text, and dr_padding. Returns the concatenation.

row(body = nil)

def row(body = nil)
return (NodeDiagram.pipe() + NodeDiagram.space() + body +
NodeDiagram.space() + NodeDiagram.pipe())
end

Forms a row. Takes a body String, the String between two pipes and spaces. Concatenates a pipe character, a space character, body, a space character, and a pipe character. Returns the concatenation.

kind_strategy(n = nil)

def kind_strategy(n = nil)
case
when n.base()
return 'base'.freeze()
when n.no_attachments()
return 'lone'.freeze()
when n.back_attached() && n.front_attached()
return 'common'.freeze()
when n.pioneer()
return 'pioneer'.freeze()
end
end

Discerns the initializing Node's kind. Takes an argument n, the initializing instance. Verifies the Node's state, and returns the corresponding kind String. The return is either 'base', 'lone', 'common', or 'pioneer'. Kind irrespective, the return reference is frozen.

form_lower()

def form_lower()
return row(lower_body())
end

Forms the lower row. Argues row(body = nil) the lower body String. Returns the lower row String.

form_upper()

def form_upper()
return row(upper_text())
end

Forms the upper row. Argues row(body = nil) the upper body text String. Returns the upper row String.

form()

def form()
return (form_upper() + NEWLINE + form_lower())
end

Forms a diagram String. Concatenates the upper row formation, a newline character, and the lower row formation. Returns the concatenation.

node_string=(n_str = nil) private

def node_string=(n_str = nil)
@node_string = n_str
end

Sets node_string. Takes a Node's to_s() String argument, n_str. n_str becomes node_string's setting. Returns the String argument.

data_text=(data = nil) private

def data_text=(data = nil)
data_string = "#{data}"
dt_length = data_string.length()
case
when (kind().eql?('base') || kind().eql?('lone')) &&
dt_length > 25
@data_text = lower_text(data_string.slice(0, 25))
when (kind().eql?('common') || kind().eql?('pioneer')) &&
dt_length > 20
@data_text = lower_text(data_string.slice(0, 20))
else
@data_text = lower_text(data)
end
end

Sets data_text. Takes a DataType type instance argument, data. Interpolates the data, conditionally formats the length, and returns a data label, space, and data interpolation String.

dl_padding=(padding = '') private

def dl_padding=(padding = '')
@dl_padding = padding
end

Sets dl_padding. Takes a padding String argument, padding. padding is at least zero space characters. padding becomes dl_padding's setting. Returns the argument String.

dr_padding=(padding = '') private

def dr_padding=(padding = '')
@dr_padding = padding
end

Sets dr_padding. Takes a padding String argument, padding. padding is at least zero space characters. padding becomes dr_padding's setting. Returns the argument String.

kind=(n_k = nil) private

def kind=(n_k = nil)
@kind = n_k
end

Sets kind. Takes a kind String argument, n_k. n_k is either 'lone', 'base', 'common', or 'pioneer'. Returns the argument String.