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.
Instance Method Summary collapse
-
#&(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. -
#*(other) ⇒ AST::Arithmetic
*. -
#+(other) ⇒ AST::Arithmetic
+; with an Active Support duration on the right, a date moved::due_on + 3.days. -
#-(other) ⇒ AST::Arithmetic
-; with a duration on the right, a date moved back. -
#/(other) ⇒ AST::Arithmetic
/. -
#<<(other) ⇒ AST::Bitwise
A shift left.
-
#>>(other) ⇒ AST::Bitwise
A shift right.
-
#^(other) ⇒ AST::Bitwise
Bitwise XOR:
#on PostgreSQL,^on MySQL, and the two operations it is made of on SQLite. -
#bitwise_and(other) ⇒ AST::Bitwise
Bitwise AND:
"flags" & 4. -
#bitwise_not ⇒ AST::BitwiseNot
#~ under a name.
-
#bitwise_or(other) ⇒ AST::Bitwise
Bitwise OR.
-
#bitwise_xor(other) ⇒ AST::Bitwise
#^ under a name.
-
#|(other) ⇒ Object
|is OR, and refuses here as&does. -
#~ ⇒ AST::BitwiseNot
Bitwise NOT.
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.
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
*.
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.
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.
492 493 494 |
# File 'lib/active_record/refined/ast.rb', line 492 def -(other) Arithmetic.new(self, :-, other) end |
#/(other) ⇒ AST::Arithmetic
/.
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.
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.
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.
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.
528 529 530 |
# File 'lib/active_record/refined/ast.rb', line 528 def bitwise_and(other) Bitwise.new(self, :&, other) end |
#bitwise_not ⇒ AST::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.
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.
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.
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.
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.
564 565 566 |
# File 'lib/active_record/refined/ast.rb', line 564 def ~ BitwiseNot.new(self) end |