Class: Squeel::Visitors::EnumerationVisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/squeel/visitors/enumeration_visitor.rb

Overview

The Enumeration visitor class, used to implement Node#each

Constant Summary collapse

DISPATCH =

A hash that caches the method name to use for a visitor for a given class

Hash.new do |hash, klass|
  hash[klass] = "visit_#{(klass.name || '').gsub('::', '_')}"
end

Instance Method Summary collapse

Constructor Details

#initialize(block = Proc.new) ⇒ EnumerationVisitor

Create a new EnumerationVisitor.



12
13
14
# File 'lib/squeel/visitors/enumeration_visitor.rb', line 12

def initialize(block = Proc.new)
  @block = block
end

Instance Method Details

#accept(object) ⇒ Object

Accept an object.



21
22
23
# File 'lib/squeel/visitors/enumeration_visitor.rb', line 21

def accept(object)
  visit(object)
end

#visit(object) ⇒ Object (private)

Visit the object.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/squeel/visitors/enumeration_visitor.rb', line 35

def visit(object)
  send(DISPATCH[object.class], object)
  @block.call(object)
rescue NoMethodError => e
  raise e if respond_to?(DISPATCH[object.class], true)

  superklass = object.class.ancestors.find { |klass|
    respond_to?(DISPATCH[klass], true)
  }
  raise(TypeError, "Cannot visit #{object.class}") unless superklass
  DISPATCH[object.class] = DISPATCH[superklass]
  retry
end

#visit_Array(o) ⇒ Object (private)



53
54
55
# File 'lib/squeel/visitors/enumeration_visitor.rb', line 53

def visit_Array(o)
  o.map { |v| visit(v) }
end

#visit_Hash(o) ⇒ Object (private)



57
58
59
# File 'lib/squeel/visitors/enumeration_visitor.rb', line 57

def visit_Hash(o)
  o.each { |k, v| visit(k); visit(v) }
end

#visit_Squeel_Nodes_Binary(o) ⇒ Object (private)



65
66
67
68
# File 'lib/squeel/visitors/enumeration_visitor.rb', line 65

def visit_Squeel_Nodes_Binary(o)
  visit(o.left)
  visit(o.right)
end

#visit_Squeel_Nodes_Function(o) ⇒ Object (private)



78
79
80
# File 'lib/squeel/visitors/enumeration_visitor.rb', line 78

def visit_Squeel_Nodes_Function(o)
  visit(o.args)
end

#visit_Squeel_Nodes_Join(o) ⇒ Object (private)



91
92
93
# File 'lib/squeel/visitors/enumeration_visitor.rb', line 91

def visit_Squeel_Nodes_Join(o)
  visit(o._join)
end

#visit_Squeel_Nodes_KeyPath(o) ⇒ Object (private)



87
88
89
# File 'lib/squeel/visitors/enumeration_visitor.rb', line 87

def visit_Squeel_Nodes_KeyPath(o)
  visit(o.path)
end

#visit_Squeel_Nodes_Literal(o) ⇒ Object (private)



95
96
97
# File 'lib/squeel/visitors/enumeration_visitor.rb', line 95

def visit_Squeel_Nodes_Literal(o)
  visit(o.expr)
end

#visit_Squeel_Nodes_Nary(o) ⇒ Object (private)



61
62
63
# File 'lib/squeel/visitors/enumeration_visitor.rb', line 61

def visit_Squeel_Nodes_Nary(o)
  visit(o.children)
end

#visit_Squeel_Nodes_Order(o) ⇒ Object (private)



74
75
76
# File 'lib/squeel/visitors/enumeration_visitor.rb', line 74

def visit_Squeel_Nodes_Order(o)
  visit(o.expr)
end

#visit_Squeel_Nodes_Predicate(o) ⇒ Object (private)



82
83
84
85
# File 'lib/squeel/visitors/enumeration_visitor.rb', line 82

def visit_Squeel_Nodes_Predicate(o)
  visit(o.expr)
  visit(o.value)
end

#visit_Squeel_Nodes_Unary(o) ⇒ Object (private)



70
71
72
# File 'lib/squeel/visitors/enumeration_visitor.rb', line 70

def visit_Squeel_Nodes_Unary(o)
  visit(o.expr)
end

#visit_terminal(o) ⇒ Object (private) Also known as: visit_Object



49
50
# File 'lib/squeel/visitors/enumeration_visitor.rb', line 49

def visit_terminal(o)
end