class Rbgraph::Edge

Attributes

data[RW]
graph[RW]
id[RW]
kind[RW]
node1[RW]
node2[RW]
weight[RW]

Public Class Methods

new(graph, node1, node2, weight, kind, data = {}) click to toggle source
# 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

Public Instance Methods

==(node) click to toggle source
# File lib/rbgraph/edge.rb, line 24
def ==(node)
  self.id == node.id
end
as_json(options = {}) click to toggle source
# File lib/rbgraph/edge.rb, line 76
def as_json(options = {})
  attributes.reject { |k, v| v.nil? || (v.respond_to?(:empty?) && v.empty?) }
end
attributes() click to toggle source
# File lib/rbgraph/edge.rb, line 33
def attributes
  {id: id, weight: weight, kind: kind, data: data}
end
different_node(node) click to toggle source
# File lib/rbgraph/edge.rb, line 46
def different_node(node)
  ([node1, node2] - [node]).first
end
has_node?(node) click to toggle source
# File lib/rbgraph/edge.rb, line 37
def has_node?(node)
  node == node1 || node == node2
end
hash() click to toggle source
# File lib/rbgraph/edge.rb, line 29
def hash
  id.hash
end
in_for?(node) click to toggle source
# File lib/rbgraph/edge.rb, line 62
def in_for?(node)
  graph.directed? ? node == node2 : has_node?(node)
end
inspect() click to toggle source
# File lib/rbgraph/edge.rb, line 71
def inspect
  "<Rbgraph::Edge:##{id} #{attributes.inspect}>"
end
merge!(edge, &block) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/rbgraph/edge.rb, line 58
def out_for?(node)
  graph.directed? ? node == node1 : has_node?(node)
end
to_json(options = {}) click to toggle source
# 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_s() click to toggle source
# File lib/rbgraph/edge.rb, line 66
def to_s
  descr = graph.directed? ? ["=", "=>>"] : ["==", "=="]
  "[#{node1.id} %s(#{attributes[:kind]}(#{weight}))%s #{node2.id}]" % descr
end