class MGraph::Builder

Public Class Methods

new(neighbor_retriever) click to toggle source
# File lib/mgraph/builder.rb, line 3
def initialize neighbor_retriever
  @neighbor_retriever = neighbor_retriever
  freeze
end

Public Instance Methods

build(root) click to toggle source
# File lib/mgraph/builder.rb, line 8
def build root
  graph = Graph.new
  traverse_and_build graph, root
end

Private Instance Methods

traverse_and_build(graph, vertex) click to toggle source
# File lib/mgraph/builder.rb, line 15
def traverse_and_build graph, vertex
  neighbors = @neighbor_retriever.(vertex)

  neighbors.reject do |neighbor|
    graph.has_edge? vertex, neighbor
  end.each do |neighbor|
    graph.add_edge vertex, neighbor
    traverse_and_build graph, neighbor
  end

  graph
end