Class: ActiveRecord::Refined::AST::ArrayPredicate

Inherits:
Predicate
  • Object
show all
Defined in:
lib/active_record/refined/ast.rb

Overview

Comparisons against a PostgreSQL array column, named after the Ruby methods that mean the same thing: member? is Enumerable's element test, superset? and subset? are Set's whole-array containment, and intersect? is Array's "any element in common". Each name maps to one operator; the elements are rendered as an array literal, which PostgreSQL coerces to the column's element type, so any expression works as the operand and no schema lookup is needed.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Predicate

#!, #&, #|

Methods inherited from Node

#as, #asc, #desc

Constructor Details

#initialize(operand, operator, elements) ⇒ ArrayPredicate

Returns a new instance of ArrayPredicate.



1355
1356
1357
1358
1359
# File 'lib/active_record/refined/ast.rb', line 1355

def initialize(operand, operator, elements)
  @operand = operand
  @operator = operator
  @elements = elements
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



1353
1354
1355
# File 'lib/active_record/refined/ast.rb', line 1353

def elements
  @elements
end

#operandObject (readonly)

Returns the value of attribute operand.



1353
1354
1355
# File 'lib/active_record/refined/ast.rb', line 1353

def operand
  @operand
end

#operatorObject (readonly)

Returns the value of attribute operator.



1353
1354
1355
# File 'lib/active_record/refined/ast.rb', line 1353

def operator
  @operator
end

Class Method Details

.elements(arg, method_name) ⇒ Object

The whole-array comparisons take the collection kinds their namesakes compare against: an Array, or a Set for the Set methods.



1344
1345
1346
1347
1348
1349
1350
1351
# File 'lib/active_record/refined/ast.rb', line 1344

def self.elements(arg, method_name)
  case arg
  when ::Array then arg
  when ::Set then arg.to_a
  else
    raise ArgumentError, "#{method_name} takes an Array or Set of elements"
  end
end

Instance Method Details

#to_arel(table, model) ⇒ Object



1361
1362
1363
1364
1365
1366
1367
1368
1369
# File 'lib/active_record/refined/ast.rb', line 1361

def to_arel(table, model)
  arel_operand = to_arel_operand(operand, table, model)
  quoted = Arel::Nodes.build_quoted(array_literal)
  case operator
  when :"@>" then Arel::Nodes::Contains.new(arel_operand, quoted)
  when :"&&" then Arel::Nodes::Overlaps.new(arel_operand, quoted)
  else Arel::Nodes::InfixOperation.new(operator, arel_operand, quoted)
  end
end