Class: GoNodes::Edge

Inherits:
Object
  • Object
show all
Defined in:
lib/gonodes/edge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Edge

Returns a new instance of Edge.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/gonodes/edge.rb', line 7

def initialize(params={})
  
  @start_node = params[:start_node] if params[:start_node]
  @end_node   = params[:end_node]   if params[:end_node]

  @weight     = params[:weight]     if params[:weight]

  @directed   = !!params[:directed] if params[:directed]
  @directed ||= false

  set_direction
  create_name
  
end

Instance Attribute Details

#directionObject

Returns the value of attribute direction.



5
6
7
# File 'lib/gonodes/edge.rb', line 5

def direction
  @direction
end

#end_nodeObject

Returns the value of attribute end_node.



5
6
7
# File 'lib/gonodes/edge.rb', line 5

def end_node
  @end_node
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/gonodes/edge.rb', line 5

def name
  @name
end

#start_nodeObject

Returns the value of attribute start_node.



5
6
7
# File 'lib/gonodes/edge.rb', line 5

def start_node
  @start_node
end

#weightObject

Returns the value of attribute weight.



5
6
7
# File 'lib/gonodes/edge.rb', line 5

def weight
  @weight
end

Instance Method Details

#<=>(other_edge) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/gonodes/edge.rb', line 60

def <=>(other_edge)
  self_nodes  = [self.start_node, self.end_node]
  other_nodes = [other_edge.start_node, other_edge.end_node]
  
  [self_nodes, self.is_directed?, self.weight] <=> 
    [other_nodes, other_edge.is_directed?, other_edge.weight]
end

#==(other_edge) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gonodes/edge.rb', line 46

def ==(other_edge)
  
  self_nodes  = [self.start_node, self.end_node]
  other_nodes = [other_edge.start_node, other_edge.end_node]

  unless self.is_directed? || other_edge.is_directed?
    self_nodes.sort!
    other_nodes.sort!
  end
  
  [self_nodes, self.is_directed?, self.weight] == 
    [other_nodes, other_edge.is_directed?, other_edge.weight]
end

#create_nameObject



38
39
40
41
42
43
44
# File 'lib/gonodes/edge.rb', line 38

def create_name
  return unless @start_node && @end_node
  start_name  = convert_node_name(@start_node)
  end_name    = convert_node_name(@end_node)
  
  @name = "{#{start_name},#{end_name}}"
end

#is_directed?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/gonodes/edge.rb', line 28

def is_directed?
  @directed
end

#set_directionObject



32
33
34
35
36
# File 'lib/gonodes/edge.rb', line 32

def set_direction
  return unless is_directed? && @start_node && @end_node

  @direction = "#{@start_node.name} -> #{@end_node.name}"
end

#to_sObject



22
23
24
25
26
# File 'lib/gonodes/edge.rb', line 22

def to_s
  ["      name: #{@name}",
   "    weight: #{@weight}",
   " direction: #{@direction}"].join("\n")
end