Class: Graphdown::Node
- Inherits:
-
Object
- Object
- Graphdown::Node
- 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
-
#child_edges ⇒ Object
readonly
Returns the value of attribute child_edges.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#level ⇒ Object
Returns the value of attribute level.
-
#parent_edges ⇒ Object
readonly
Returns the value of attribute parent_edges.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #ancestors ⇒ Object
- #children ⇒ Object
- #connect(child, direction = :forward) ⇒ Object
- #descendants ⇒ Object
- #height ⇒ Object
-
#initialize(label) ⇒ Node
constructor
A new instance of Node.
- #parents ⇒ Object
- #width ⇒ Object
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_edges ⇒ Object (readonly)
Returns the value of attribute child_edges.
4 5 6 |
# File 'lib/graphdown/node.rb', line 4 def child_edges @child_edges end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
4 5 6 |
# File 'lib/graphdown/node.rb', line 4 def label @label end |
#level ⇒ Object
Returns the value of attribute level.
3 4 5 |
# File 'lib/graphdown/node.rb', line 3 def level @level end |
#parent_edges ⇒ Object (readonly)
Returns the value of attribute parent_edges.
4 5 6 |
# File 'lib/graphdown/node.rb', line 4 def parent_edges @parent_edges end |
#x ⇒ Object
Returns the value of attribute x.
3 4 5 |
# File 'lib/graphdown/node.rb', line 3 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
3 4 5 |
# File 'lib/graphdown/node.rb', line 3 def y @y end |
Instance Method Details
#ancestors ⇒ Object
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 |
#children ⇒ Object
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 |
#descendants ⇒ Object
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 |
#height ⇒ Object
29 30 31 |
# File 'lib/graphdown/node.rb', line 29 def height WORD_HEIGHT + PADDING_TOP * 2 end |
#parents ⇒ Object
48 49 50 |
# File 'lib/graphdown/node.rb', line 48 def parents parent_edges.map(&:parent) end |
#width ⇒ Object
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 |