class Parelation::Criteria::Where::Caster

Constants

FALSY_VALUES

@return [String] an array of values that are considered false

TRUTHY_VALUES

@return [String] an array of values that are considered true

Attributes

field[R]

@return [String]

klass[R]

@return [Class]

value[R]

@return [String]

Public Class Methods

new(field, value, klass) click to toggle source

@param field [Symbol] the name of the attribute that needs to be casted @param value [String] the value of the attribute that needs to be casted @param klass [Class] the corresponding field's class

# File lib/parelation/criteria/where/caster.rb, line 27
def initialize(field, value, klass)
  @field = field.to_s
  @value = value
  @klass = klass
end

Public Instance Methods

cast() click to toggle source

@return [String, Boolean, Time, nil]

# File lib/parelation/criteria/where/caster.rb, line 35
def cast
  case type
  when :boolean
    to_boolean
  when :integer
    to_integer
  when :float
    to_float
  when :datetime
    to_time
  else
    value
  end
end

Private Instance Methods

to_boolean() click to toggle source

@return [true, false, nil]

# File lib/parelation/criteria/where/caster.rb, line 60
def to_boolean
  TRUTHY_VALUES.include?(value) && (return true)
  FALSY_VALUES.include?(value) && (return false)
end
to_float() click to toggle source

@return [Float]

# File lib/parelation/criteria/where/caster.rb, line 73
def to_float
  value.to_f
end
to_integer() click to toggle source

@return [Integer]

# File lib/parelation/criteria/where/caster.rb, line 67
def to_integer
  value.to_i
end
to_time() click to toggle source

@return [Time] the parsed time string

# File lib/parelation/criteria/where/caster.rb, line 79
def to_time
  Time.parse(value)
end
type() click to toggle source

@return [Symbol]

# File lib/parelation/criteria/where/caster.rb, line 54
def type
  klass.columns_hash[field].type
end