Module: ActiveRecord::Refined::AST::Arithmetics

Included in:
Aggregate, Arithmetic, Bitwise, BitwiseNot, Case, Cast, Column, DatetimeValueFunction, Extract, Function, JsonPath, Operation, Over, Sql, Value, BlockSyntax
Defined in:
lib/active_record/refined/ast.rb

Overview

The arithmetic and the bitwise operators on a column or an expression. Ruby puts all of them above the comparisons, so :price * :quantity > 100 groups the way it reads, and a number on the left -- 20 - :quantity -- builds the same expression.

Arithmetic builders shared by symbols, qualified columns and expressions. Imported into the Symbol refinement like Predications, so every method must be defined with def.

Examples:

LineItem.where { :price * :quantity > 1000 }
LineItem.select { (:flags & 4).as(:featured) }

Instance Method Summary collapse

Instance Method Details

#&(other) ⇒ AST::Bitwise

Bitwise AND. & between two conditions is AND, which leaves this free to mean the SQL operator. SQL's bitwise operators. & and | are AND and OR between conditions and are defined there, which is what leaves them free to mean here what SQL means by them. Ruby's precedence puts all six above the comparisons, so :flags & 4 > 0 groups the way it reads.

Returns:



466
467
468
# File 'lib/active_record/refined/ast.rb', line 466

def &(other)
  Bitwise.new(self, :&, other)
end

#*(other) ⇒ AST::Arithmetic

*.

Returns:



449
450
451
# File 'lib/active_record/refined/ast.rb', line 449

def *(other)
  Arithmetic.new(self, :*, other)
end

#+(other) ⇒ AST::Arithmetic

+; with an Active Support duration on the right, a date moved: :due_on + 3.days.

Returns:



437
438
439
# File 'lib/active_record/refined/ast.rb', line 437

def +(other)
  Arithmetic.new(self, :+, other)
end

#-(other) ⇒ AST::Arithmetic

-; with a duration on the right, a date moved back.

Returns:



443
444
445
# File 'lib/active_record/refined/ast.rb', line 443

def -(other)
  Arithmetic.new(self, :-, other)
end

#/(other) ⇒ AST::Arithmetic

/.

Returns:



455
456
457
# File 'lib/active_record/refined/ast.rb', line 455

def /(other)
  Arithmetic.new(self, :/, other)
end

#<<(other) ⇒ AST::Bitwise

A shift left.

Returns:



484
485
486
# File 'lib/active_record/refined/ast.rb', line 484

def <<(other)
  Bitwise.new(self, :<<, other)
end

#>>(other) ⇒ AST::Bitwise

A shift right.

Returns:



490
491
492
# File 'lib/active_record/refined/ast.rb', line 490

def >>(other)
  Bitwise.new(self, :>>, other)
end

#^(other) ⇒ AST::Bitwise

Bitwise XOR: # on PostgreSQL, ^ on MySQL, and the two operations it is made of on SQLite.

Returns:



478
479
480
# File 'lib/active_record/refined/ast.rb', line 478

def ^(other)
  Bitwise.new(self, :^, other)
end

#|(other) ⇒ AST::Bitwise

Bitwise OR.

Returns:



472
473
474
# File 'lib/active_record/refined/ast.rb', line 472

def |(other)
  Bitwise.new(self, :|, other)
end

#~AST::BitwiseNot

Bitwise NOT.

Returns:



496
497
498
# File 'lib/active_record/refined/ast.rb', line 496

def ~
  BitwiseNot.new(self)
end