Class: Babik::Selection::Operation::Between

Inherits:
Base
  • Object
show all
Defined in:
lib/babik/queryset/lib/selection/operation/operations.rb

Overview

Between comparison (check the value is between two different values)

Instance Attribute Summary

Attributes inherited from Base

#field, #sql_operation, #sql_operation_template, #value

Instance Method Summary collapse

Methods inherited from Base

date_special_cases, #db_engine, escape, factory, initialize_operation, initialize_operators, operator?, special_cases, #to_s

Constructor Details

#initialize(field, value) ⇒ Between

Returns a new instance of Between.



107
108
109
# File 'lib/babik/queryset/lib/selection/operation/operations.rb', line 107

def initialize(field, value)
  super(field, '?field BETWEEN ?value1 AND ?value2', value)
end

Instance Method Details

#_init_sql_operationObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/babik/queryset/lib/selection/operation/operations.rb', line 111

def _init_sql_operation
  if @value.class == Array
    if [@value[0], @value[1]].map { |v| [DateTime, Date, Time].include?(v.class) } == [true, true]
      value1 = "'#{@value[0].utc.to_s(:db)}'"
      value2 = "'#{@value[1].utc.to_s(:db)}'"
    else
      value1 = self.class.escape(@value[0])
      value2 = self.class.escape(@value[1])
    end
    @sql_operation = @sql_operation_template.sub('?field', @field).sub('?value1', value1).sub('?value2', value2)
  else
    raise 'Array is needed if operator is between'
  end
end