Module: Tangle::GraphVertices

Included in:
Graph
Defined in:
lib/tangle/graph_vertices.rb

Overview

Vertex related methods in a graph

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object

Return a named vertex



12
13
14
# File 'lib/tangle/graph_vertices.rb', line 12

def [](name)
  @vertices_by_name[name]
end

#add_vertex(vertex, name: nil) ⇒ Object Also known as: <<

Add a vertex into the graph

If a name: is given, or the vertex responds to :name, it will be registered by name in the graph



30
31
32
33
34
35
# File 'lib/tangle/graph_vertices.rb', line 30

def add_vertex(vertex, name: nil)
  name ||= callback(vertex, :name)
  insert_vertex(vertex, name)
  callback(vertex, :added_to_graph, self)
  self
end

#fetch(name) ⇒ Object

Fetch a vertex by its name



7
8
9
# File 'lib/tangle/graph_vertices.rb', line 7

def fetch(name)
  @vertices_by_name.fetch(name)
end

#remove_vertex(vertex) ⇒ Object

Remove a vertex from the graph



39
40
41
42
43
44
45
# File 'lib/tangle/graph_vertices.rb', line 39

def remove_vertex(vertex)
  @vertices[vertex].each do |edge|
    remove_edge(edge) if edge.include?(vertex)
  end
  delete_vertex(vertex)
  callback(vertex, :removed_from_graph, self)
end

#select(&selector) ⇒ Object

Select vertices in the graph



22
23
24
# File 'lib/tangle/graph_vertices.rb', line 22

def select(&selector)
  @vertices.each_key.select(&selector)
end

#verticesObject

Return all vertices in the graph



17
18
19
# File 'lib/tangle/graph_vertices.rb', line 17

def vertices
  @vertices.keys
end