Class: Graphdown::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/graphdown/node.rb

Constant Summary collapse

FONT_SIZE =
18
FONT_FAMILY =
"monospace"
WORD_HEIGHT =
25
PADDING_TOP =
5
PADDING_LEFT =
10
MARGIN_RIGHT =
20
MARGIN_BOTTOM =
50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label) ⇒ Node

Returns a new instance of Node.



15
16
17
18
19
20
21
22
# File 'lib/graphdown/node.rb', line 15

def initialize(label)
  @x = 0
  @y = 0
  @level = 0
  @label = label
  @parent_edges = []
  @child_edges = []
end

Instance Attribute Details

#child_edgesObject (readonly)

Returns the value of attribute child_edges.



4
5
6
# File 'lib/graphdown/node.rb', line 4

def child_edges
  @child_edges
end

#labelObject (readonly)

Returns the value of attribute label.



4
5
6
# File 'lib/graphdown/node.rb', line 4

def label
  @label
end

#levelObject

Returns the value of attribute level.



3
4
5
# File 'lib/graphdown/node.rb', line 3

def level
  @level
end

#parent_edgesObject (readonly)

Returns the value of attribute parent_edges.



4
5
6
# File 'lib/graphdown/node.rb', line 4

def parent_edges
  @parent_edges
end

#xObject

Returns the value of attribute x.



3
4
5
# File 'lib/graphdown/node.rb', line 3

def x
  @x
end

#yObject

Returns the value of attribute y.



3
4
5
# File 'lib/graphdown/node.rb', line 3

def y
  @y
end

Instance Method Details

#ancestorsObject



52
53
54
55
56
57
58
59
60
# File 'lib/graphdown/node.rb', line 52

def ancestors
  ancestors = []
  ascend = ->(node) do
    ancestors << node
    node.parents.each { |parent| ascend.call(parent) }
  end
  parents.each { |parent| ascend.call(parent) }
  ancestors
end

#childrenObject



62
63
64
# File 'lib/graphdown/node.rb', line 62

def children
  child_edges.map(&:child)
end

#connect(child, direction = :forward) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/graphdown/node.rb', line 33

def connect(child, direction = :forward)
  child.level = 1

  # Prevent closed path
  direction = ancestors.include?(child) ? :backward : direction
  edge = Edge.new(self, child, direction)
  if direction == :backward
    self.parent_edges << edge
    child.child_edges << edge
  else
    self.child_edges << edge
    child.parent_edges << edge
  end
end

#descendantsObject



66
67
68
69
70
71
72
73
74
# File 'lib/graphdown/node.rb', line 66

def descendants
  descendants = []
  descend = ->(node) do
    descendants << node
    node.children.each { |child| descend.call(child) }
  end
  children.each { |child| descend.call(child) }
  descendants
end

#heightObject



29
30
31
# File 'lib/graphdown/node.rb', line 29

def height
  WORD_HEIGHT + PADDING_TOP  * 2
end

#parentsObject



48
49
50
# File 'lib/graphdown/node.rb', line 48

def parents
  parent_edges.map(&:parent)
end

#widthObject



24
25
26
27
# File 'lib/graphdown/node.rb', line 24

def width
  text_width = 13 + (@label.length - 1) * 9
  text_width + PADDING_LEFT * 2
end