Class: HCast::Casters::BooleanCaster

Inherits:
Object
  • Object
show all
Defined in:
lib/hcast/casters/boolean_caster.rb

Class Method Summary collapse

Class Method Details

.cast(value, attr_name, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/hcast/casters/boolean_caster.rb', line 3

def self.cast(value, attr_name, options = {})
  if [TrueClass, FalseClass].include?(value.class)
    value
  elsif ['1', 'true', 'on', 1].include?(value)
    true
  elsif ['0', 'false', 'off', 0].include?(value)
    false
  else
    raise HCast::Errors::CastingError, "should be a boolean"
  end
end