Class: Tangle::Vertex

Inherits:
SimpleDelegator
  • Object
show all
Includes:
PP::ObjectMixin
Defined in:
lib/tangle/vertex.rb

Overview

A named vertex in a graph

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph: nil, name: nil, delegate: nil, vertex_id: object_id) ⇒ Vertex

Create a new vertex

Vertex.new(…) => Vertex

Named arguments:

graph: a Graph or nil for an orphaned vertex
name: anything that's hashable and unique within the graph
delegate: delegate object for missing methods


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tangle/vertex.rb', line 20

def initialize(graph: nil,
               name: nil,
               delegate: nil,
               vertex_id: object_id)
  super(delegate) unless delegate.nil?

  @graph = graph
  @name = name
  @delegate = delegate
  @vertex_id = vertex_id
end

Instance Attribute Details

#delegateObject (readonly)

Returns the value of attribute delegate.



78
79
80
# File 'lib/tangle/vertex.rb', line 78

def delegate
  @delegate
end

#graphObject (readonly)

Returns the value of attribute graph.



76
77
78
# File 'lib/tangle/vertex.rb', line 76

def graph
  @graph
end

#nameObject (readonly)

Returns the value of attribute name.



77
78
79
# File 'lib/tangle/vertex.rb', line 77

def name
  @name
end

#vertex_idObject (readonly)

Returns the value of attribute vertex_id.



79
80
81
# File 'lib/tangle/vertex.rb', line 79

def vertex_id
  @vertex_id
end

Instance Method Details

#!=(other) ⇒ Object

If two vertices have the same vertex_id, they have the same value



66
67
68
# File 'lib/tangle/vertex.rb', line 66

def !=(other)
  @vertex_id != other.vertex_id
end

#==(other) ⇒ Object

If two vertices have the same vertex_id, they have the same value



60
61
62
# File 'lib/tangle/vertex.rb', line 60

def ==(other)
  @vertex_id == other.vertex_id
end

#dup_into(graph) ⇒ Object

Duplicate a vertex in a new graph, keeping all other contained attributes End users should probably use Graph#subgrap instead.

dup_into(new_graph) => Vertex

Raises an ArgumentError if graph would remain the same.

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
# File 'lib/tangle/vertex.rb', line 39

def dup_into(graph)
  raise ArgumentError if graph == @graph

  Vertex.new(
    graph:     graph,
    name:      @name,
    delegate:  @delegate,
    vertex_id: @vertex_id
  )
end

#edgesObject

Return all edges that touch this vertex



52
53
54
55
56
# File 'lib/tangle/vertex.rb', line 52

def edges
  return [] if @graph.nil?

  @graph.edges { |edge| edge.include? self }
end

#eql?(other) ⇒ Boolean

If two vertices have the same object_id, they are identical

Returns:

  • (Boolean)


72
73
74
# File 'lib/tangle/vertex.rb', line 72

def eql?(other)
  @object_id == other.object_id
end