Class: Squeel::Visitors::PredicateVisitor

Inherits:
Visitor
  • Object
show all
Includes:
PredicateVisitation
Defined in:
lib/squeel/visitors/predicate_visitor.rb

Direct Known Subclasses

HavingVisitor, WhereVisitor

Constant Summary

Constants included from PredicateVisitation

Squeel::Visitors::PredicateVisitation::FALSE_SQL, Squeel::Visitors::PredicateVisitation::TRUE_SQL

Constants inherited from Visitor

Visitor::DISPATCH

Instance Attribute Summary

Attributes inherited from Visitor

#context

Instance Method Summary collapse

Methods included from PredicateVisitation

#arel_predicate_for, #attribute_in_array, #attribute_not_in_array, #quote_for_node, #visit_Squeel_Nodes_Predicate, #visit_Squeel_Nodes_Sifter

Methods inherited from Visitor

#accept, #can_visit?, can_visit?, #hash_context_shifted?, #initialize, #quote, #quoted?, #visit, #visit_ActiveRecord_Base, #visit_ActiveRecord_Relation, #visit_Array, #visit_Squeel_Nodes_And, #visit_Squeel_Nodes_As, #visit_Squeel_Nodes_Function, #visit_Squeel_Nodes_Grouping, #visit_Squeel_Nodes_KeyPath, #visit_Squeel_Nodes_Literal, #visit_Squeel_Nodes_Not, #visit_Squeel_Nodes_Operation, #visit_Squeel_Nodes_Or, #visit_Squeel_Nodes_Stub, #visit_Symbol, #visit_passthrough, #visit_with_hash_context_shift

Constructor Details

This class inherits a constructor from Squeel::Visitors::Visitor

Instance Method Details

#implies_hash_context_shift?(v) ⇒ Boolean (private)

Returns Whether the given value implies a context change.

Parameters:

  • v

    The value to consider

Returns:

  • (Boolean)

    Whether the given value implies a context change



30
31
32
33
34
35
36
37
38
39
# File 'lib/squeel/visitors/predicate_visitor.rb', line 30

def implies_hash_context_shift?(v)
  case v
  when Hash, Nodes::Predicate, Nodes::Unary, Nodes::Binary, Nodes::Nary, Nodes::Sifter
    true
  when Nodes::KeyPath
    can_visit?(v.endpoint) && !(Nodes::Stub === v.endpoint)
  else
    false
  end
end

#visit_Hash(o, parent) ⇒ Array (private)

Visit a Hash. This entails iterating through each key and value and visiting each value in turn.

Parameters:

  • o (Hash)

    The Hash to visit

  • parent

    The current parent object in the context

Returns:

  • (Array)

    An array of values for use in a where or having clause



17
18
19
20
21
22
23
24
25
# File 'lib/squeel/visitors/predicate_visitor.rb', line 17

def visit_Hash(o, parent)
  predicates = super

  if predicates.size > 1
    Arel::Nodes::Grouping.new(Arel::Nodes::And.new predicates)
  else
    predicates.first
  end
end

#visit_without_hash_context_shift(k, v, parent) ⇒ Object (private)

Create a predicate for a given key/value pair. If the value is a Symbol, Stub, or KeyPath, it’s converted to a table.column for the predicate value.

Parameters:

  • k

    The hash key

  • v

    The hash value

  • parent

    The current parent object in the context

Returns:

  • An ARel predicate



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/squeel/visitors/predicate_visitor.rb', line 49

def visit_without_hash_context_shift(k, v, parent)
  case v
  when Nodes::Stub, Symbol
    v = contextualize(parent)[v.to_s]
  when Nodes::KeyPath # If we could visit the endpoint, we wouldn't be here
    v = contextualize(traverse(v, parent))[v.endpoint.to_s]
  end

  case k
  when Nodes::Predicate
    visit(k % quote_for_node(k.expr, v), parent)
  when Nodes::Function, Nodes::Literal
    arel_predicate_for(visit(k, parent), quote(v), parent)
  when Nodes::KeyPath
    visit(k % quote_for_node(k.endpoint, v), parent)
  else
    attr_name = k.to_s
    attribute = if !hash_context_shifted? && attr_name.include?('.')
        table_name, attr_name = attr_name.split(/\./, 2)
        Arel::Table.new(table_name.to_s, :engine => engine)[attr_name.to_s]
      else
        contextualize(parent)[attr_name]
      end
    arel_predicate_for(attribute, v, parent)
  end
end