Class: SearchCop::Visitors::Visitor

Inherits:
Object
  • Object
show all
Defined in:
lib/search_cop/visitors/visitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Visitor

Returns a new instance of Visitor.



8
9
10
11
12
13
14
# File 'lib/search_cop/visitors/visitor.rb', line 8

def initialize(connection)
  @connection = connection

  extend(SearchCop::Visitors::Mysql) if @connection.adapter_name =~ /mysql|trilogy/i
  extend(SearchCop::Visitors::Postgres) if @connection.adapter_name =~ /postgres|postgis/i
  extend(SearchCop::Visitors::Sqlite) if @connection.adapter_name =~ /sqlite/i
end

Instance Attribute Details

#connectionObject

rubocop:disable Naming/MethodName



6
7
8
# File 'lib/search_cop/visitors/visitor.rb', line 6

def connection
  @connection
end

Instance Method Details

#quote(value) ⇒ Object Also known as: visit_TrueClass, visit_FalseClass, visit_String, visit_Time, visit_Date, visit_Float, visit_Fixnum, visit_Symbol, visit_Integer



88
89
90
# File 'lib/search_cop/visitors/visitor.rb', line 88

def quote(value)
  connection.quote value
end

#quote_column_name(name) ⇒ Object



68
69
70
# File 'lib/search_cop/visitors/visitor.rb', line 68

def quote_column_name(name)
  connection.quote_column_name name
end

#quote_table_name(name) ⇒ Object



64
65
66
# File 'lib/search_cop/visitors/visitor.rb', line 64

def quote_table_name(name)
  connection.quote_table_name name
end

#visit(visit_node = node) ⇒ Object



16
17
18
# File 'lib/search_cop/visitors/visitor.rb', line 16

def visit(visit_node = node)
  send "visit_#{visit_node.class.name.gsub("::", "_")}", visit_node
end

#visit_attribute(attribute) ⇒ Object Also known as: visit_SearchCopGrammar_Attributes_String, visit_SearchCopGrammar_Attributes_Text, visit_SearchCopGrammar_Attributes_Float, visit_SearchCopGrammar_Attributes_Integer, visit_SearchCopGrammar_Attributes_Decimal, visit_SearchCopGrammar_Attributes_Datetime, visit_SearchCopGrammar_Attributes_Timestamp, visit_SearchCopGrammar_Attributes_Timestamptz, visit_SearchCopGrammar_Attributes_Date, visit_SearchCopGrammar_Attributes_Time, visit_SearchCopGrammar_Attributes_Boolean



72
73
74
# File 'lib/search_cop/visitors/visitor.rb', line 72

def visit_attribute(attribute)
  "#{quote_table_name attribute.table_alias}.#{quote_column_name attribute.column_name}"
end

#visit_SearchCopGrammar_Nodes_And(node) ⇒ Object



20
21
22
# File 'lib/search_cop/visitors/visitor.rb', line 20

def visit_SearchCopGrammar_Nodes_And(node)
  "(#{node.nodes.collect { |n| visit n }.join(" AND ")})"
end

#visit_SearchCopGrammar_Nodes_Equality(node) ⇒ Object



44
45
46
# File 'lib/search_cop/visitors/visitor.rb', line 44

def visit_SearchCopGrammar_Nodes_Equality(node)
  "#{visit node.left} = #{visit node.right}"
end

#visit_SearchCopGrammar_Nodes_Generator(node) ⇒ Object



60
61
62
# File 'lib/search_cop/visitors/visitor.rb', line 60

def visit_SearchCopGrammar_Nodes_Generator(node)
  instance_exec visit(node.left), node.right[:value], &node.right[:generator]
end

#visit_SearchCopGrammar_Nodes_GreaterThan(node) ⇒ Object



28
29
30
# File 'lib/search_cop/visitors/visitor.rb', line 28

def visit_SearchCopGrammar_Nodes_GreaterThan(node)
  "#{visit node.left} > #{visit node.right}"
end

#visit_SearchCopGrammar_Nodes_GreaterThanOrEqual(node) ⇒ Object



32
33
34
# File 'lib/search_cop/visitors/visitor.rb', line 32

def visit_SearchCopGrammar_Nodes_GreaterThanOrEqual(node)
  "#{visit node.left} >= #{visit node.right}"
end

#visit_SearchCopGrammar_Nodes_LessThan(node) ⇒ Object



36
37
38
# File 'lib/search_cop/visitors/visitor.rb', line 36

def visit_SearchCopGrammar_Nodes_LessThan(node)
  "#{visit node.left} < #{visit node.right}"
end

#visit_SearchCopGrammar_Nodes_LessThanOrEqual(node) ⇒ Object



40
41
42
# File 'lib/search_cop/visitors/visitor.rb', line 40

def visit_SearchCopGrammar_Nodes_LessThanOrEqual(node)
  "#{visit node.left} <= #{visit node.right}"
end

#visit_SearchCopGrammar_Nodes_Matches(node) ⇒ Object



52
53
54
# File 'lib/search_cop/visitors/visitor.rb', line 52

def visit_SearchCopGrammar_Nodes_Matches(node)
  "(#{visit node.left} IS NOT NULL AND #{visit node.left} LIKE #{visit node.right} ESCAPE #{visit "\\"})"
end

#visit_SearchCopGrammar_Nodes_Not(node) ⇒ Object



56
57
58
# File 'lib/search_cop/visitors/visitor.rb', line 56

def visit_SearchCopGrammar_Nodes_Not(node)
  "NOT (#{visit node.object})"
end

#visit_SearchCopGrammar_Nodes_NotEqual(node) ⇒ Object



48
49
50
# File 'lib/search_cop/visitors/visitor.rb', line 48

def visit_SearchCopGrammar_Nodes_NotEqual(node)
  "#{visit node.left} != #{visit node.right}"
end

#visit_SearchCopGrammar_Nodes_Or(node) ⇒ Object



24
25
26
# File 'lib/search_cop/visitors/visitor.rb', line 24

def visit_SearchCopGrammar_Nodes_Or(node)
  "(#{node.nodes.collect { |n| visit n }.join(" OR ")})"
end