Class: Squeel::Nodes::Predicate

Inherits:
Node
  • Object
show all
Includes:
Aliasing, 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 Aliasing

#as

Methods included from PredicateOperators

#&, #[email protected], #|

Methods inherited from Node

#each, #grep

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.


27
28
29
# File 'lib/squeel/nodes/predicate.rb', line 27

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.


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

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.


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

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.


14
15
16
# File 'lib/squeel/nodes/predicate.rb', line 14

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


53
54
55
56
# File 'lib/squeel/nodes/predicate.rb', line 53

def %(val)
  @value = val
  self
end

#eql?(other) ⇒ Boolean

Object comparison

Returns:

  • (Boolean)

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

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


40
41
42
# File 'lib/squeel/nodes/predicate.rb', line 40

def hash
  [@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


62
63
64
# File 'lib/squeel/nodes/predicate.rb', line 62

def to_sym
  nil
end

#value?Boolean

Whether the value has been assigned yet.

Returns:

  • (Boolean)

    Has the value been set?


46
47
48
# File 'lib/squeel/nodes/predicate.rb', line 46

def value?
  @value != :__undefined__
end