Class: Acfs::Resource::Attributes::Boolean

Inherits:
Base
  • Object
show all
Defined in:
lib/acfs/resource/attributes/boolean.rb

Overview

Boolean attribute type. Use it in your model as an attribute type:

Given objects will be converted to string. The following strings are considered true, everything else false:

true, on, yes

Examples:

class User < Acfs::Resource
  attribute :name, :boolean
end

Constant Summary collapse

FALSE_VALUES =
[false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF', 'no', 'NO'].to_set

Instance Attribute Summary

Attributes inherited from Base

#default

Instance Method Summary collapse

Methods inherited from Base

#cast, #default_value, #initialize

Constructor Details

This class inherits a constructor from Acfs::Resource::Attributes::Base

Instance Method Details

#cast_value(value) ⇒ TrueClass, FalseClass

Cast given object to boolean.

Parameters:

  • value (Object)

    Object to cast.

Returns:

  • (TrueClass, FalseClass)

    Casted boolean.



28
29
30
31
32
33
34
35
36
37
# File 'lib/acfs/resource/attributes/boolean.rb', line 28

def cast_value(value)
  return true if value == true
  return false if value == false

  if value.blank?
    nil
  else
    !FALSE_VALUES.include?(value)
  end
end