module ModelX::Boolean

Adds boolean attribute accessors to any object, allowing boolean-ish values to be set as well.

Usage

class MyObject
  include ModelX::Boolean

  attr_accessor :my_attribute
  boolean :my_attribute
end

Now, the following can be used:

object = MyObject.new
object.my_attribute = false
object.my_attribute? # => false

object.my_attribute = '0'
object.my_attribute? # => false
object.my_attribute = '1'
object.my_attribute? # => true
object.my_attribute = 'false'
object.my_attribute? # => false

Note that an existing attribute writer must exist.

The values '0', 0, 'off', 'no' and 'false', and all values that Ruby considers false are deemed to be false. All other values are true.

Public Class Methods

convert(value) click to toggle source

Converts a boolean attribute. This is used mostly for toggle buttons that enable or disable an input section.

# File lib/model_x/boolean.rb, line 62
def self.convert(value)
  value.present? && value != '0' && value != 0 && value != 'off' && value != 'no' && value != 'false'
end