Class: AdvancedSearch::Adapters::PG::Visitor
- Inherits:
-
Object
- Object
- AdvancedSearch::Adapters::PG::Visitor
- Defined in:
- lib/advanced_search/adapters/pg/visitor.rb
Instance Method Summary collapse
-
#initialize(placeholder_style) ⇒ Visitor
constructor
A new instance of Visitor.
- #result ⇒ Object
- #visit_and(node) ⇒ Object
- #visit_eq(node) ⇒ Object
- #visit_id(node) ⇒ Object
- #visit_lt(node) ⇒ Object
- #visit_or(node) ⇒ Object
- #visit_value(node) ⇒ Object
Constructor Details
#initialize(placeholder_style) ⇒ Visitor
Returns a new instance of Visitor.
7 8 9 10 11 |
# File 'lib/advanced_search/adapters/pg/visitor.rb', line 7 def initialize(placeholder_style) @placeholder_style = placeholder_style @sql = [] @binds = [] end |
Instance Method Details
#result ⇒ Object
13 14 15 |
# File 'lib/advanced_search/adapters/pg/visitor.rb', line 13 def result Query.new(@sql.join(' '), @binds) end |
#visit_and(node) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/advanced_search/adapters/pg/visitor.rb', line 17 def visit_and(node) @sql << '(' node.edges.each_with_index do |child, i| unless i.zero? @sql << 'and' end child.accept(self) end @sql << ')' end |
#visit_eq(node) ⇒ Object
28 29 30 31 32 |
# File 'lib/advanced_search/adapters/pg/visitor.rb', line 28 def visit_eq(node) node.edges[0].accept(self) @sql << '=' node.edges[1].accept(self) end |
#visit_id(node) ⇒ Object
34 35 36 |
# File 'lib/advanced_search/adapters/pg/visitor.rb', line 34 def visit_id(node) @sql << node.id end |
#visit_lt(node) ⇒ Object
38 39 40 41 42 |
# File 'lib/advanced_search/adapters/pg/visitor.rb', line 38 def visit_lt(node) node.edges[0].accept(self) @sql << '<' node.edges[1].accept(self) end |
#visit_or(node) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/advanced_search/adapters/pg/visitor.rb', line 44 def visit_or(node) @sql << '(' node.edges.each_with_index do |child, i| unless i.zero? @sql << 'or' end child.accept(self) end @sql << ')' end |
#visit_value(node) ⇒ Object
55 56 57 58 |
# File 'lib/advanced_search/adapters/pg/visitor.rb', line 55 def visit_value(node) @sql << placeholder(@binds.length + 1) @binds << node.value end |