Class: ActiveAttr::Typecasting::BooleanTypecaster

Inherits:
Object
  • Object
show all
Defined in:
lib/active_attr/typecasting/boolean_typecaster.rb

Overview

Typecasts an Object to true or false

Examples:

Usage

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

Since:

Constant Summary

FALSE_VALUES =

Values which force a false result for typecasting

These values are based on the YAML language.

Since:

  • 0.5.0

["n", "N", "no", "No", "NO", "false", "False", "FALSE", "off", "Off", "OFF", "f", "F"]

Instance Method Summary (collapse)

Instance Method Details

- (true, false) call(value)

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.

Examples:

Typecast a Fixnum

typecaster.call(1) #=> true

Since:

  • 0.5.0



34
35
36
37
38
39
40
# File 'lib/active_attr/typecasting/boolean_typecaster.rb', line 34

def call(value)
  case value
  when *FALSE_VALUES then false
  when Numeric, /^\-?[0-9]/ then !value.to_f.zero?
  else value.present?
  end
end