Class: Mongoid::Fields::Internal::Boolean

Inherits:
Object
  • Object
show all
Includes:
Serializable
Defined in:
lib/mongoid/fields/internal/boolean.rb

Overview

Defines the behaviour for boolean fields.

Direct Known Subclasses

FalseClass, TrueClass

Constant Summary collapse

MAPPINGS =
{
  true => true,
  "true" => true,
  "TRUE" => true,
  "1" => true,
  1 => true,
  1.0 => true,
  false => false,
  "false" => false,
  "FALSE" => false,
  "0" => false,
  0 => false,
  0.0 => false
}

Instance Attribute Summary

Attributes included from Serializable

#default_val, #label, #localize, #name, #options

Instance Method Summary collapse

Methods included from Serializable

#constraint, #deserialize, #eval_default, #foreign_key?, #localized?, #metadata, #object_id_field?, #resizable?, #type, #versioned?

Instance Method Details

#selection(object) ⇒ Object

Special case to serialize the object.

Examples:

Convert to a selection.

field.selection(object)

Parameters:

  • The (Object)

    object to convert.

Returns:

  • (Object)

    The converted object.

Since:

  • 2.4.0



35
36
37
38
# File 'lib/mongoid/fields/internal/boolean.rb', line 35

def selection(object)
  return object if object.is_a?(::Hash)
  serialize(object)
end

#serialize(object) ⇒ true, false

Serialize the object from the type defined in the model to a MongoDB compatible object to store.

Examples:

Serialize the field.

field.serialize(object)

Parameters:

  • object (Object)

    The object to cast.

Returns:

  • (true, false)

    The converted boolean.

Since:

  • 2.1.0



51
52
53
54
# File 'lib/mongoid/fields/internal/boolean.rb', line 51

def serialize(object)
  object = MAPPINGS[object]
  object.nil? ? nil : object
end