class ActiveAttr::Typecasting::BooleanTypecaster

Typecasts an Object to true or false

@example Usage

BooleanTypecaster.new.call(1) #=> true

@since 0.5.0

Constants

FALSE_VALUES

Values which force a false result for typecasting

These values are based on the YAML language.

@since 0.5.0

NIL_VALUES

Values which force a nil result for typecasting

These values are based on the behavior of ActiveRecord

@since 0.14.0

Public Instance Methods

call(value) click to toggle source

Typecasts an object to true or false

Similar to ActiveRecord, when the attribute is a zero value or is a string that represents false, typecasting returns false. Otherwise typecasting just checks the presence of a value.

@example Typecast an Integer

typecaster.call(1) #=> true

@param [Object] value The object to typecast

@return [true, false] The result of typecasting

@since 0.5.0

# File lib/active_attr/typecasting/boolean_typecaster.rb, line 41
def call(value)
  case value
  when *FALSE_VALUES then false
  when *NIL_VALUES then nil
  when Numeric, /\A[-+]?(0++\.?0*|0*+\.?0+)\z/ then !value.to_f.zero?
  else value.present?
  end
end