Class: HierarchicalGraph::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, id, data = {}) ⇒ Node

Returns a new instance of Node.



6
7
8
9
10
# File 'lib/hierarchical_graph/node.rb', line 6

def initialize(graph, id, data={})
  @graph = graph
  @id = id
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

Instance Method Details

#[](key) ⇒ Object



12
13
14
# File 'lib/hierarchical_graph/node.rb', line 12

def [](key)
  data[key]
end

#[]=(key, value) ⇒ Object



16
17
18
# File 'lib/hierarchical_graph/node.rb', line 16

def []=(key, value)
  data[key] = value
end

#ancestorsObject



28
29
30
# File 'lib/hierarchical_graph/node.rb', line 28

def ancestors
  graph.ancestors_of id
end

#childrenObject



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

def children
  graph.children_of id
end

#descendantsObject



32
33
34
# File 'lib/hierarchical_graph/node.rb', line 32

def descendants
  graph.descendants_of id
end

#parentsObject



20
21
22
# File 'lib/hierarchical_graph/node.rb', line 20

def parents
  graph.parents_of id
end

#root?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/hierarchical_graph/node.rb', line 36

def root?
  parents.empty?
end

#to_sObject Also known as: inspect



40
41
42
# File 'lib/hierarchical_graph/node.rb', line 40

def to_s
  "<#{self.class.name} #{id} parents:[#{parents.map(&:id).join(', ')}] children:[#{children.map(&:id).join(', ')}]>"
end