Class: Parelation::Criteria::Where::Caster

Inherits:
Object
  • Object
show all
Defined in:
lib/parelation/criteria/where/caster.rb

Constant Summary collapse

TRUTHY_VALUES =

Returns an array of values that are considered true.

Returns:

  • (String)

    an array of values that are considered true

["1", "t", "true"]
FALSY_VALUES =

Returns an array of values that are considered false.

Returns:

  • (String)

    an array of values that are considered false

["0", "f", "false"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, value, klass) ⇒ Caster

Returns a new instance of Caster.

Parameters:

  • field (Symbol)

    the name of the attribute that needs to be casted

  • value (String)

    the value of the attribute that needs to be casted

  • klass (Class)

    the corresponding field’s class



27
28
29
30
31
# File 'lib/parelation/criteria/where/caster.rb', line 27

def initialize(field, value, klass)
  @field = field.to_s
  @value = value
  @klass = klass
end

Instance Attribute Details

#fieldString (readonly)

Returns:

  • (String)


13
14
15
# File 'lib/parelation/criteria/where/caster.rb', line 13

def field
  @field
end

#klassClass (readonly)

Returns:

  • (Class)


21
22
23
# File 'lib/parelation/criteria/where/caster.rb', line 21

def klass
  @klass
end

#valueString (readonly)

Returns:

  • (String)


17
18
19
# File 'lib/parelation/criteria/where/caster.rb', line 17

def value
  @value
end

Instance Method Details

#castString, ...

Returns:

  • (String, Boolean, Time, nil)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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