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 operations on a column or an expression. Ruby puts the operators above the comparisons, so :price * :quantity > 100 groups the way it reads, and a number on the left -- 20 - :quantity -- builds the same expression.

Bitwise AND and OR are named, not spelled & and |: those two are AND and OR between conditions and mean nothing else anywhere. The rest keep their operators, with #bitwise_xor and #bitwise_not beside ^ and ~ for a reader who would rather have the name. Oracle, which has no bitwise operators at all, refuses every one.

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.bitwise_and(4).as(:featured) }

Instance Method Summary collapse

Instance Method Details

#&(other) ⇒ Object

& is AND, and a column is not a condition, so this refuses: #bitwise_and is the SQL operator, and .true? makes a boolean column a condition.

Raises:

  • (ArgumentError)


512
513
514
# File 'lib/active_record/refined/ast.rb', line 512

def &(other)
  AST.refuse_logical(:&, "AND", "bitwise_and", other)
end

#*(other) ⇒ AST::Arithmetic

*.

Returns:



498
499
500
# File 'lib/active_record/refined/ast.rb', line 498

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:



486
487
488
# File 'lib/active_record/refined/ast.rb', line 486

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

#-(other) ⇒ AST::Arithmetic

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

Returns:



492
493
494
# File 'lib/active_record/refined/ast.rb', line 492

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

#/(other) ⇒ AST::Arithmetic

/.

Returns:



504
505
506
# File 'lib/active_record/refined/ast.rb', line 504

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

#<<(other) ⇒ AST::Bitwise

A shift left.

Returns:



552
553
554
# File 'lib/active_record/refined/ast.rb', line 552

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

#>>(other) ⇒ AST::Bitwise

A shift right.

Returns:



558
559
560
# File 'lib/active_record/refined/ast.rb', line 558

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:



540
541
542
# File 'lib/active_record/refined/ast.rb', line 540

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

#bitwise_and(other) ⇒ AST::Bitwise

Bitwise AND: "flags" & 4. It is spelled as a name because & is AND, and a method binds tighter than any comparison, so nothing has to be parenthesised to be compared.

Examples:

Post.where { :flags.bitwise_and(4) > 0 }

Returns:



528
529
530
# File 'lib/active_record/refined/ast.rb', line 528

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

#bitwise_notAST::BitwiseNot

#~ under a name. ! is not the one to reach for: it is Ruby's own on a column and answers false, which no query ever wanted.

Returns:



571
572
573
# File 'lib/active_record/refined/ast.rb', line 571

def bitwise_not
  BitwiseNot.new(self)
end

#bitwise_or(other) ⇒ AST::Bitwise

Bitwise OR.

Returns:



534
535
536
# File 'lib/active_record/refined/ast.rb', line 534

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

#bitwise_xor(other) ⇒ AST::Bitwise

#^ under a name.

Returns:



546
547
548
# File 'lib/active_record/refined/ast.rb', line 546

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

#|(other) ⇒ Object

| is OR, and refuses here as & does.

Raises:

  • (ArgumentError)


518
519
520
# File 'lib/active_record/refined/ast.rb', line 518

def |(other)
  AST.refuse_logical(:|, "OR", "bitwise_or", other)
end

#~AST::BitwiseNot

Bitwise NOT.

Returns:



564
565
566
# File 'lib/active_record/refined/ast.rb', line 564

def ~
  BitwiseNot.new(self)
end