Class: ActiveRecord::Refined::AST::Arithmetic

Inherits:
Node
  • Object
show all
Includes:
Arithmetics, Predications
Defined in:
lib/active_record/refined/ast.rb

Overview

Arithmetic on columns and expressions. Ruby's precedence puts these above the comparison operators, so :price * :quantity > 100 groups the way it reads.

A Duration on the right moves a date: :due_on + 3.days. No two families spell the move alike, so the dialect writes it, a part of the duration at a time.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Arithmetics

#&, #*, #+, #-, #/, #<<, #>>, #^, #|, #~

Methods included from Predications

#!=, #!~, #<, #<=, #==, #=~, #>, #>=, #between?, #bury, #casecmp?, #contains?, #dig, #dig_text, #distinct_from?, #end_with?, #except, #false?, #ilike?, #in?, #include?, #intersect?, #key?, #keys, #like?, #member?, #not_between?, #not_distinct_from?, #not_false?, #not_ilike?, #not_in?, #not_like?, #not_null?, #not_true?, #null?, #start_with?, #subset?, #superset?, #true?, #when

Methods inherited from Node

#as, #asc, #collate, #desc

Constructor Details

#initialize(left, operator, right) ⇒ Arithmetic

Returns a new instance of Arithmetic.



1332
1333
1334
1335
1336
# File 'lib/active_record/refined/ast.rb', line 1332

def initialize(left, operator, right)
  @left = left
  @operator = operator
  @right = right
end

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.



1330
1331
1332
# File 'lib/active_record/refined/ast.rb', line 1330

def left
  @left
end

#operatorObject (readonly)

Returns the value of attribute operator.



1330
1331
1332
# File 'lib/active_record/refined/ast.rb', line 1330

def operator
  @operator
end

#rightObject (readonly)

Returns the value of attribute right.



1330
1331
1332
# File 'lib/active_record/refined/ast.rb', line 1330

def right
  @right
end

Class Method Details

.date_operand?(operand, model) ⇒ Boolean

SQLite has no date type, and its datetime() gives whatever it is handed a time of day, so a date column moved by a day would come back a midnight and sort past the same day written bare. Its dialect has date() for what is a date to begin with, and this is what says so: a column the model declares a date, CURRENT_DATE, or one of those already moved by a date's units. The other families keep the type themselves and never ask.

Returns:

  • (Boolean)


1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
# File 'lib/active_record/refined/ast.rb', line 1354

def self.date_operand?(operand, model)
  case operand
  when ::Symbol then model.type_for_attribute(operand).type == :date
  when DatetimeValueFunction then operand.name == "CURRENT_DATE"
  when Arithmetic
    operand.right.is_a?(::ActiveSupport::Duration) &&
      date_operand?(operand.left, model) &&
      operand.right.parts.keys.all? { |part| DATE_UNITS.include?(UNITS[part]) }
  else false
  end
end

Instance Method Details

#to_arel(table, model) ⇒ Object



1338
1339
1340
1341
1342
1343
1344
1345
# File 'lib/active_record/refined/ast.rb', line 1338

def to_arel(table, model)
  arel_left = to_arel_operand(left, table, model)
  return move_date(arel_left, model) if right.is_a?(::ActiveSupport::Duration)
  # The operator dispatches Arel's Math, which a bare number carries
  # none of; quoted, it is a node with the same methods.
  arel_left = Arel::Nodes.build_quoted(arel_left) if arel_left.is_a?(::Numeric)
  arel_left.public_send(operator, to_arel_operand(right, table, model))
end