class Arel::Visitors::Dot

Public Class Methods

new() click to toggle source
# File lib/arel/visitors/dot.rb, line 17
def initialize
  @nodes      = []
  @edges      = []
  @node_stack = []
  @edge_stack = []
  @seen       = {}
end

Public Instance Methods

accept(object) click to toggle source
Calls superclass method Arel::Visitors::Visitor#accept
# File lib/arel/visitors/dot.rb, line 25
def accept object
  super
  to_dot
end

Private Instance Methods

edge(name) { || ... } click to toggle source
# File lib/arel/visitors/dot.rb, line 234
def edge name
  edge = Edge.new(name, @node_stack.last)
  @edge_stack.push edge
  @edges << edge
  yield
  @edge_stack.pop
end
extract(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 105
def extract o, a
  visit_edge o, a, "expressions"
  visit_edge o, a, "alias"
end
Also aliased as: visit_Arel_Nodes_Extract
function(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 94
def function o, a
  visit_edge o, a, "expressions"
  visit_edge o, a, "distinct"
  visit_edge o, a, "alias"
end
named_window(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 87
def named_window o, a
  visit_edge o, a, "orders"
  visit_edge o, a, "framing"
  visit_edge o, a, "name"
end
Also aliased as: visit_Arel_Nodes_NamedWindow
nary(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 159
def nary o, a
  o.children.each_with_index do |x,i|
    edge(i) { visit x, a }
  end
end
Also aliased as: visit_Arel_Nodes_And
quote(string) click to toggle source
# File lib/arel/visitors/dot.rb, line 252
def quote string
  string.to_s.gsub('"', '\"')
end
to_dot() click to toggle source
# File lib/arel/visitors/dot.rb, line 256
def to_dot
  "digraph \"Arel\" {\nnode [width=0.375,height=0.25,shape=record];\n" +
    @nodes.map { |node|
      label = "<f0>#{node.name}"

      node.fields.each_with_index do |field, i|
        label << "|<f#{i + 1}>#{quote field}"
      end

      "#{node.id} [label=\"#{label}\"];"
    }.join("\n") + "\n" + @edges.map { |edge|
      "#{edge.from.id} -> #{edge.to.id} [label=\"#{edge.name}\"];"
    }.join("\n") + "\n}"
end
visit(o, a = nil) click to toggle source
Calls superclass method Arel::Visitors::Visitor#visit
# File lib/arel/visitors/dot.rb, line 220
def visit o, a = nil
  if node = @seen[o.object_id]
    @edge_stack.last.to = node
    return
  end

  node = Node.new(o.class.name, o.object_id)
  @seen[node.id] = node
  @nodes << node
  with_node node do
    super
  end
end
visit_Arel_Attribute(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 148
def visit_Arel_Attribute o, a
  visit_edge o, a, "relation"
  visit_edge o, a, "name"
end
visit_Arel_Attributes_Attribute(o, a)
visit_Arel_Attributes_Boolean(o, a)
visit_Arel_Attributes_Float(o, a)
visit_Arel_Attributes_Integer(o, a)
visit_Arel_Attributes_String(o, a)
visit_Arel_Attributes_Time(o, a)
visit_Arel_Nodes_And(o, a)
Alias for: nary
visit_Arel_Nodes_As(o, a)
Alias for: binary
visit_Arel_Nodes_Assignment(o, a)
Alias for: binary
visit_Arel_Nodes_Avg(o, a)
Alias for: function
visit_Arel_Nodes_Between(o, a)
Alias for: binary
visit_Arel_Nodes_BindParam(o, a)
Alias for: visit_String
visit_Arel_Nodes_Count(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 40
def visit_Arel_Nodes_Count o, a
  visit_edge o, a, "expressions"
  visit_edge o, a, "distinct"
end
visit_Arel_Nodes_DeleteStatement(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 59
def visit_Arel_Nodes_DeleteStatement o, a
  visit_edge o, a, "relation"
  visit_edge o, a, "wheres"
end
visit_Arel_Nodes_DoesNotMatch(o, a)
Alias for: binary
visit_Arel_Nodes_Equality(o, a)
Alias for: binary
visit_Arel_Nodes_Exists(o, a)
Alias for: function
visit_Arel_Nodes_Extract(o, a)
Alias for: extract
visit_Arel_Nodes_Following(o, a)
Alias for: unary
visit_Arel_Nodes_GreaterThan(o, a)
Alias for: binary
visit_Arel_Nodes_GreaterThanOrEqual(o, a)
Alias for: binary
visit_Arel_Nodes_Group(o, a)
Alias for: unary
visit_Arel_Nodes_Grouping(o, a)
Alias for: unary
visit_Arel_Nodes_Having(o, a)
Alias for: unary
visit_Arel_Nodes_In(o, a)
Alias for: binary
visit_Arel_Nodes_InnerJoin(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 53
def visit_Arel_Nodes_InnerJoin o, a
  visit_edge o, a, "left"
  visit_edge o, a, "right"
end
Also aliased as: visit_Arel_Nodes_OuterJoin
visit_Arel_Nodes_InsertStatement(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 118
def visit_Arel_Nodes_InsertStatement o, a
  visit_edge o, a, "relation"
  visit_edge o, a, "columns"
  visit_edge o, a, "values"
end
visit_Arel_Nodes_JoinSource(o, a)
Alias for: binary
visit_Arel_Nodes_LessThan(o, a)
Alias for: binary
visit_Arel_Nodes_LessThanOrEqual(o, a)
Alias for: binary
visit_Arel_Nodes_Limit(o, a)
Alias for: unary
visit_Arel_Nodes_Matches(o, a)
Alias for: binary
visit_Arel_Nodes_Max(o, a)
Alias for: function
visit_Arel_Nodes_Min(o, a)
Alias for: function
visit_Arel_Nodes_NamedFunction(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 111
def visit_Arel_Nodes_NamedFunction o, a
  visit_edge o, a, "name"
  visit_edge o, a, "expressions"
  visit_edge o, a, "distinct"
  visit_edge o, a, "alias"
end
visit_Arel_Nodes_NamedWindow(o, a)
Alias for: named_window
visit_Arel_Nodes_Not(o, a)
Alias for: unary
visit_Arel_Nodes_NotEqual(o, a)
Alias for: binary
visit_Arel_Nodes_NotIn(o, a)
Alias for: binary
visit_Arel_Nodes_Offset(o, a)
Alias for: unary
visit_Arel_Nodes_On(o, a)
Alias for: unary
visit_Arel_Nodes_Or(o, a)
Alias for: binary
visit_Arel_Nodes_Ordering(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 31
def visit_Arel_Nodes_Ordering o, a
  visit_edge o, a, "expr"
end
visit_Arel_Nodes_OuterJoin(o, a)
visit_Arel_Nodes_Over(o, a)
Alias for: binary
visit_Arel_Nodes_Preceding(o, a)
Alias for: unary
visit_Arel_Nodes_Range(o, a)
Alias for: unary
visit_Arel_Nodes_Rows(o, a)
Alias for: unary
visit_Arel_Nodes_SelectCore(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 124
def visit_Arel_Nodes_SelectCore o, a
  visit_edge o, a, "source"
  visit_edge o, a, "projections"
  visit_edge o, a, "wheres"
  visit_edge o, a, "windows"
end
visit_Arel_Nodes_SelectStatement(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 131
def visit_Arel_Nodes_SelectStatement o, a
  visit_edge o, a, "cores"
  visit_edge o, a, "limit"
  visit_edge o, a, "orders"
  visit_edge o, a, "offset"
end
visit_Arel_Nodes_SqlLiteral(o, a)
Alias for: visit_String
visit_Arel_Nodes_StringJoin(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 49
def visit_Arel_Nodes_StringJoin o, a
  visit_edge o, a, "left"
end
visit_Arel_Nodes_Sum(o, a)
Alias for: function
visit_Arel_Nodes_TableAlias(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 35
def visit_Arel_Nodes_TableAlias o, a
  visit_edge o, a, "name"
  visit_edge o, a, "relation"
end
visit_Arel_Nodes_Top(o, a)
Alias for: unary
visit_Arel_Nodes_UnqualifiedColumn(o, a)
Alias for: unary
visit_Arel_Nodes_UpdateStatement(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 138
def visit_Arel_Nodes_UpdateStatement o, a
  visit_edge o, a, "relation"
  visit_edge o, a, "wheres"
  visit_edge o, a, "values"
end
visit_Arel_Nodes_Values(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 45
def visit_Arel_Nodes_Values o, a
  visit_edge o, a, "expressions"
end
visit_Arel_Nodes_Window(o, a)
Alias for: window
visit_Arel_SqlLiteral(o, a)
Alias for: visit_String
visit_Arel_Table(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 144
def visit_Arel_Table o, a
  visit_edge o, a, "name"
end
visit_Array(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 210
def visit_Array o, a
  o.each_with_index do |x,i|
    edge(i) { visit x, a }
  end
end
visit_BigDecimal(o, a)
Alias for: visit_String
visit_Date(o, a)
Alias for: visit_String
visit_DateTime(o, a)
Alias for: visit_String
visit_FalseClass(o, a)
Alias for: visit_String
visit_Fixnum(o, a)
Alias for: visit_String
visit_Float(o, a)
Alias for: visit_String
visit_Hash(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 204
def visit_Hash o, a
  o.each_with_index do |pair, i|
    edge("pair_#{i}")   { visit pair, a }
  end
end
visit_NilClass(o, a)
Alias for: visit_String
visit_String(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 187
def visit_String o, a
  @node_stack.last.fields << o
end
visit_Symbol(o, a)
Alias for: visit_String
visit_Time(o, a)
Alias for: visit_String
visit_TrueClass(o, a)
Alias for: visit_String
visit_edge(o, a, method) click to toggle source
# File lib/arel/visitors/dot.rb, line 216
def visit_edge o, a, method
  edge(method) { visit o.send(method), a }
end
window(o, a) click to toggle source
# File lib/arel/visitors/dot.rb, line 81
def window o, a
  visit_edge o, a, "orders"
  visit_edge o, a, "framing"
end
Also aliased as: visit_Arel_Nodes_Window
with_node(node) { || ... } click to toggle source
# File lib/arel/visitors/dot.rb, line 242
def with_node node
  if edge = @edge_stack.last
    edge.to = node
  end

  @node_stack.push node
  yield
  @node_stack.pop
end