Module: Valideizer::Caster

Included in:
Core
Defined in:
lib/valideizer/caster.rb

Instance Method Summary collapse

Instance Method Details

#cast_from_json(value) ⇒ Object



5
6
7
# File 'lib/valideizer/caster.rb', line 5

def cast_from_json(value)
  JSON.parse(value) rescue nil
end

#cast_to_boolean(value) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/valideizer/caster.rb', line 25

def cast_to_boolean(value)
  if %w(1 true).include?(value.to_s.downcase)
    true
  elsif %w(0 false).include?(value.to_s.downcase)
    false
  end
end

#cast_to_float(value) ⇒ Object



13
14
15
# File 'lib/valideizer/caster.rb', line 13

def cast_to_float(value)
  value.to_f rescue nil
end

#cast_to_integer(value) ⇒ Object



9
10
11
# File 'lib/valideizer/caster.rb', line 9

def cast_to_integer(value)
  value.to_i rescue nil
end

#cast_to_time(value) ⇒ Object



17
18
19
# File 'lib/valideizer/caster.rb', line 17

def cast_to_time(value)
  Time.parse(value) rescue nil
end

#cast_to_time_with_format(value, format) ⇒ Object



21
22
23
# File 'lib/valideizer/caster.rb', line 21

def cast_to_time_with_format(value, format)
  Time.strptime(value, format) rescue nil
end