Class: Rbgraph::Edge

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, node1, node2, weight, kind, data = {}) ⇒ Edge

Returns a new instance of Edge.



13
14
15
16
17
18
19
20
21
22
# File 'lib/rbgraph/edge.rb', line 13

def initialize(graph, node1, node2, weight, kind, data = {})
  self.graph  = graph
  self.node1  = node1
  self.node2  = node2
  self.weight = weight.nil? ? 1 : weight.to_i
  self.kind   = kind
  nodes_ids   = graph.directed? ? [node1.id, node2.id] : [node1.id, node2.id].sort
  self.id     = "%s=#{kind}=%s" % nodes_ids
  self.data   = data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



11
12
13
# File 'lib/rbgraph/edge.rb', line 11

def data
  @data
end

#graphObject

Returns the value of attribute graph.



6
7
8
# File 'lib/rbgraph/edge.rb', line 6

def graph
  @graph
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#kindObject

Returns the value of attribute kind.



10
11
12
# File 'lib/rbgraph/edge.rb', line 10

def kind
  @kind
end

#node1Object

Returns the value of attribute node1.



7
8
9
# File 'lib/rbgraph/edge.rb', line 7

def node1
  @node1
end

#node2Object

Returns the value of attribute node2.



8
9
10
# File 'lib/rbgraph/edge.rb', line 8

def node2
  @node2
end

#weightObject

Returns the value of attribute weight.



9
10
11
# File 'lib/rbgraph/edge.rb', line 9

def weight
  @weight
end

Instance Method Details

#as_json(options = {}) ⇒ Object



76
77
78
# File 'lib/rbgraph/edge.rb', line 76

def as_json(options = {})
  attributes.reject { |k, v| v.nil? || (v.respond_to?(:empty?) && v.empty?) }
end

#attributesObject



33
34
35
# File 'lib/rbgraph/edge.rb', line 33

def attributes
  {id: id, weight: weight, kind: kind, data: data}
end

#different_node(node) ⇒ Object



46
47
48
# File 'lib/rbgraph/edge.rb', line 46

def different_node(node)
  ([node1, node2] - [node]).first
end

#has_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/rbgraph/edge.rb', line 37

def has_node?(node)
  node == node1 || node == node2
end

#hashObject



29
30
31
# File 'lib/rbgraph/edge.rb', line 29

def hash
  id.hash
end

#in_for?(node) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/rbgraph/edge.rb', line 62

def in_for?(node)
  graph.directed? ? node == node2 : has_node?(node)
end

#inspectObject



71
72
73
# File 'lib/rbgraph/edge.rb', line 71

def inspect
  "<Rbgraph::Edge:##{id} #{attributes.inspect}>"
end

#merge!(edge, &block) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/rbgraph/edge.rb', line 50

def merge!(edge, &block)
  self.weight += edge.weight unless edge.weight.nil?
  raise "Cannot merging edges of different kind!" if kind != edge.kind
  data.merge!(edge.data, &block)
  graph.remove_edge!(edge)
  self
end

#other_node(node) ⇒ Object



41
42
43
44
# File 'lib/rbgraph/edge.rb', line 41

def other_node(node)
  # ([node1, node2] - [node]).first ! Fails for edge connecting a node to itself
  node == node1 ? node2 : node1
end

#out_for?(node) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rbgraph/edge.rb', line 58

def out_for?(node)
  graph.directed? ? node == node1 : has_node?(node)
end

#to_json(options = {}) ⇒ Object



80
81
82
# File 'lib/rbgraph/edge.rb', line 80

def to_json(options = {})
  JSON.generate(attributes.reject { |k, v| v.nil? || (v.respond_to?(:empty?) && v.empty?) })
end

#to_sObject



66
67
68
69
# File 'lib/rbgraph/edge.rb', line 66

def to_s
  descr = graph.directed? ? ["=", "=>>"] : ["==", "=="]
  "[#{node1.id} %s(#{attributes[:kind]}(#{weight}))%s #{node2.id}]" % descr
end