Skip to content

Instantly share code, notes, and snippets.

@naoty
Created January 23, 2014 10:16
Show Gist options
  • Select an option

  • Save naoty/8576162 to your computer and use it in GitHub Desktop.

Select an option

Save naoty/8576162 to your computer and use it in GitHub Desktop.
DOT言語のDSL
class Digraph
attr_reader :name
def initialize(name)
@name = name
@nodes = []
end
def method_missing(name, *args)
node = @nodes.select { |node| node.name == name }.first
if node.nil?
node = Node.new(name)
@nodes << node
end
node
end
end
class Node
attr_reader :name
def initialize(name)
@name = name
end
def >>(other)
puts "#{self.name} -> #{other.name};"
other
end
def <=>(other)
puts "#{self.name} -> #{other.name} [dir = both];"
other
end
end
def digraph(name, &block)
digraph = Digraph.new(name)
puts "digraph #{digraph.name} {"
digraph.instance_eval(&block)
puts "}"
digraph
end
digraph "sample" do
a >> b >> c >> d
b <=> d
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment