Class: Squeel::Nodes::Predicate

Inherits:
Object
  • Object
show all
Includes:
PredicateOperators
Defined in:
lib/squeel/nodes/predicate.rb

Overview

This node is essentially a container that will result in ARel predicate nodes once visited. It stores the expression (normally an attribute name, function, or operation), the ARel predicate method name, and a value. these are then interpreted when visited by the PredicateVisitor to generate a condition against the appropriate columns.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PredicateOperators

#&, #-@, #|

Constructor Details

#initialize(expr, method_name = :eq, value = :__undefined__) ⇒ Predicate

Create a new Predicate node with the given expression, method name, and value

Parameters:

  • expr

    The expression for the left hand side of the predicate.

  • method_name (Symbol) (defaults to: :eq)

    The ARel predication method

  • value (defaults to: :__undefined__)

    An optional value. If not given, one will need to be supplied before the node can be visited properly.



29
30
31
# File 'lib/squeel/nodes/predicate.rb', line 29

def initialize(expr, method_name = :eq, value = :__undefined__)
  @expr, @method_name, @value = expr, method_name, value
end

Instance Attribute Details

#exprObject (readonly)

Returns The expression on the left side of this predicate.

Returns:

  • The expression on the left side of this predicate.



19
20
21
# File 'lib/squeel/nodes/predicate.rb', line 19

def expr
  @expr
end

#method_nameSymbol (readonly)

Returns The ARel “predication” method name, such as eq, matches, etc.

Returns:

  • (Symbol)

    The ARel “predication” method name, such as eq, matches, etc.



22
23
24
# File 'lib/squeel/nodes/predicate.rb', line 22

def method_name
  @method_name
end

#valueObject

Returns The right-hand value being considered in this predicate.

Returns:

  • The right-hand value being considered in this predicate.



16
17
18
# File 'lib/squeel/nodes/predicate.rb', line 16

def value
  @value
end

Instance Method Details

#%(val) ⇒ Predicate

Set the value for this predicate

Parameters:

  • val

    The value to be set

Returns:

  • (Predicate)

    This predicate, with its new value set



56
57
58
59
# File 'lib/squeel/nodes/predicate.rb', line 56

def %(val)
  @value = val
  self
end

#eql?(other) ⇒ Boolean Also known as: ==

Object comparison

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/squeel/nodes/predicate.rb', line 34

def eql?(other)
  self.class.eql?(other.class) &&
  self.expr.eql?(other.expr) &&
  self.method_name.eql?(other.method_name) &&
  self.value.eql?(other.value)
end

#hashObject

Implemented for equality testing



43
44
45
# File 'lib/squeel/nodes/predicate.rb', line 43

def hash
  [self.class, expr, method_name, value].hash
end

#to_symNilClass

expand_hash_conditions_for_aggregates assumes our hash keys can be converted to symbols, so this has to be implemented, but it doesn’t really have to do anything useful.

Returns:

  • (NilClass)

    Just to avoid bombing out on expand_hash_conditions_for_aggregates



65
66
67
# File 'lib/squeel/nodes/predicate.rb', line 65

def to_sym
  nil
end

#value?Boolean

Whether the value has been assigned yet.

Returns:

  • (Boolean)

    Has the value been set?



49
50
51
# File 'lib/squeel/nodes/predicate.rb', line 49

def value?
  @value != :__undefined__
end