Class: Rbgraph::Edge
- Inherits:
-
Object
- Object
- Rbgraph::Edge
- Defined in:
- lib/rbgraph/edge.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#graph ⇒ Object
Returns the value of attribute graph.
-
#id ⇒ Object
Returns the value of attribute id.
-
#kind ⇒ Object
Returns the value of attribute kind.
-
#node1 ⇒ Object
Returns the value of attribute node1.
-
#node2 ⇒ Object
Returns the value of attribute node2.
-
#weight ⇒ Object
Returns the value of attribute weight.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
- #attributes ⇒ Object
- #different_node(node) ⇒ Object
- #has_node?(node) ⇒ Boolean
- #hash ⇒ Object
- #in_for?(node) ⇒ Boolean
-
#initialize(graph, node1, node2, weight, kind, data = {}) ⇒ Edge
constructor
A new instance of Edge.
- #inspect ⇒ Object
- #merge!(edge, &block) ⇒ Object
- #other_node(node) ⇒ Object
- #out_for?(node) ⇒ Boolean
- #to_json(options = {}) ⇒ Object
- #to_s ⇒ Object
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
#data ⇒ Object
Returns the value of attribute data.
11 12 13 |
# File 'lib/rbgraph/edge.rb', line 11 def data @data end |
#graph ⇒ Object
Returns the value of attribute graph.
6 7 8 |
# File 'lib/rbgraph/edge.rb', line 6 def graph @graph end |
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/rbgraph/edge.rb', line 5 def id @id end |
#kind ⇒ Object
Returns the value of attribute kind.
10 11 12 |
# File 'lib/rbgraph/edge.rb', line 10 def kind @kind end |
#node1 ⇒ Object
Returns the value of attribute node1.
7 8 9 |
# File 'lib/rbgraph/edge.rb', line 7 def node1 @node1 end |
#node2 ⇒ Object
Returns the value of attribute node2.
8 9 10 |
# File 'lib/rbgraph/edge.rb', line 8 def node2 @node2 end |
#weight ⇒ Object
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( = {}) attributes.reject { |k, v| v.nil? || (v.respond_to?(:empty?) && v.empty?) } end |
#attributes ⇒ Object
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
37 38 39 |
# File 'lib/rbgraph/edge.rb', line 37 def has_node?(node) node == node1 || node == node2 end |
#hash ⇒ Object
29 30 31 |
# File 'lib/rbgraph/edge.rb', line 29 def hash id.hash end |
#in_for?(node) ⇒ Boolean
62 63 64 |
# File 'lib/rbgraph/edge.rb', line 62 def in_for?(node) graph.directed? ? node == node2 : has_node?(node) end |
#inspect ⇒ Object
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
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( = {}) JSON.generate(attributes.reject { |k, v| v.nil? || (v.respond_to?(:empty?) && v.empty?) }) end |
#to_s ⇒ Object
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 |