Class: ActiveRecord::Refined::AST::Comparison

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

Constant Summary collapse

OPERATOR_MAP =
{
  :== => :eq, :!= => :not_eq, :=~ => :matches, :!~ => :does_not_match,
  :> => :gt, :>= => :gteq, :< => :lt, :<= => :lteq
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Predicate

#!, #&, #|

Methods inherited from Node

#as, #asc, #desc

Constructor Details

#initialize(column, operator, value) ⇒ Comparison

Returns a new instance of Comparison.



150
151
152
153
154
# File 'lib/active_record/refined/ast.rb', line 150

def initialize(column, operator, value)
  @column = column
  @operator = operator
  @value = value
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



148
149
150
# File 'lib/active_record/refined/ast.rb', line 148

def column
  @column
end

#operatorObject (readonly)

Returns the value of attribute operator.



148
149
150
# File 'lib/active_record/refined/ast.rb', line 148

def operator
  @operator
end

#valueObject (readonly)

Returns the value of attribute value.



148
149
150
# File 'lib/active_record/refined/ast.rb', line 148

def value
  @value
end

Instance Method Details

#to_arel(table) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/active_record/refined/ast.rb', line 156

def to_arel(table)
  arel_column = case column
                when Node then column.to_arel(table)
                else table[column]
                end
  arel_value = case value
               when Node then value.to_arel(table)
               else value
               end
  case
  when operator == :== && Range === value
    arel_column.between(value)
  when operator == :!= && Range === value
    arel_column.not_between(value)
  when operator == :== && Array === value
    arel_column.in(value)
  when operator == :!= && Array === value
    arel_column.not_in(value)
  else
    arel_column.public_send(OPERATOR_MAP.fetch(operator), arel_value)
  end
end