Class: MiniGraph::DSL::GraphContext

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_graph/dsl/graph_context.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.evaluate(*vertices, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/mini_graph/dsl/graph_context.rb', line 7

def self.evaluate(*vertices, &block)
  unless block_given?
    raise ArgumentError, "cannot call .create without a block"
  end

  graph_context = new(vertices)
  graph_context.instance_eval(&block)
  graph_context.resolve
end

Instance Method Details

#resolveObject


Public Methods




21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mini_graph/dsl/graph_context.rb', line 21

def resolve
  if @edges.any? { |edge| edge.any?(&:nil?) }
    raise MiniGraph::Core::Error::InvalidEdgeError,
          'One or more invalid edges were specified'
  end

  MiniGraph::Core::Graph.new(@vertices, directed: @directed).tap do |g|
    @edges.each do |edge|
      g.add_edge(*edge)
    end
  end
end