Class: Tangle::Edge
- Inherits:
-
Object
- Object
- Tangle::Edge
- Extended by:
- Forwardable
- Includes:
- Mixin::Initialize
- Defined in:
- lib/tangle/edge.rb
Overview
An edge in a graph, connecting two vertices
Direct Known Subclasses
Instance Attribute Summary collapse
-
#graph ⇒ Object
readonly
Returns the value of attribute graph.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#vertices ⇒ Object
readonly
Returns the value of attribute vertices.
Instance Method Summary collapse
-
#clone_into(graph) ⇒ Object
Clone an edge into another graph, replacing original vertices with their already prepared duplicates in the other graph.
- #eql?(other) ⇒ Boolean (also: #==, #===, #equal?)
-
#initialize(vertex1, vertex2 = vertex1, graph: nil, name: nil, **kwargs) ⇒ Edge
constructor
Create a new edge between vertices.
- #inspect ⇒ Object
-
#other(for_vertex) ⇒ Object
Return the other vertex for a vertex of this edge.
-
#walk(from_vertex, selector: :other) ⇒ Object
Follow the edge from a vertex to the other end.
Constructor Details
#initialize(vertex1, vertex2 = vertex1, graph: nil, name: nil, **kwargs) ⇒ Edge
Create a new edge between vertices
Edge.new(vtx1) => Edge (loop) Edge.new(vtx1, vtx2) => Edge
End users should probably use Graph#add_edge instead.
19 20 21 22 23 24 25 26 27 |
# File 'lib/tangle/edge.rb', line 19 def initialize(vertex1, vertex2 = vertex1, graph: nil, name: nil, **kwargs) @name = name with_graph(graph) with_vertices(vertex1, vertex2) initialize_mixins(**kwargs) validate_edge end |
Instance Attribute Details
#graph ⇒ Object (readonly)
Returns the value of attribute graph.
78 79 80 |
# File 'lib/tangle/edge.rb', line 78 def graph @graph end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
77 78 79 |
# File 'lib/tangle/edge.rb', line 77 def name @name end |
#vertices ⇒ Object (readonly)
Returns the value of attribute vertices.
79 80 81 |
# File 'lib/tangle/edge.rb', line 79 def vertices @vertices end |
Instance Method Details
#clone_into(graph) ⇒ Object
Clone an edge into another graph, replacing original vertices with their already prepared duplicates in the other graph. Returns nil if any of the vertices does not exist in the other graph. End users should probably use Graph#subgraph instead.
clone_into(graph) => Edge or nil
Raises an ArgumentError if graph would remain the same.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/tangle/edge.rb', line 54 def clone_into(graph) raise ArgumentError if graph == @graph vertices = @vertices.map do |vertex| graph.get_vertex(vertex.vertex_id) end clone.with_graph(graph).with_vertices(*vertices) rescue KeyError nil end |
#eql?(other) ⇒ Boolean Also known as: ==, ===, equal?
70 71 72 |
# File 'lib/tangle/edge.rb', line 70 def eql?(other) @graph == other.graph && @vertices == other.vertices end |
#inspect ⇒ Object
66 67 68 |
# File 'lib/tangle/edge.rb', line 66 def inspect "#<#{self.class}: #{@vertices}>" end |
#other(for_vertex) ⇒ Object
Return the other vertex for a vertex of this edge
39 40 41 42 43 |
# File 'lib/tangle/edge.rb', line 39 def other(for_vertex) raise RuntimeError unless @vertices.include?(for_vertex) @vertices.find { |other| other != for_vertex } || for_vertex end |
#walk(from_vertex, selector: :other) ⇒ Object
Follow the edge from a vertex to the other end
walk(vertex) => Vertex
33 34 35 |
# File 'lib/tangle/edge.rb', line 33 def walk(from_vertex, selector: :other) send(selector, from_vertex) end |