Class: Mongoid::Fields::Internal::Range

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

Overview

Defines the behaviour for range fields.

Instance Attribute Summary

Attributes included from Serializable

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

Instance Method Summary collapse

Methods included from Serializable

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

Instance Method Details

#deserialize(object) ⇒ Range

Deserialize this field from the type stored in MongoDB to the type defined on the model.

Examples:

Deserialize the field.

field.deserialize(object)

Parameters:

  • object (Object)

    The object to cast.

Returns:

  • (Range)

    The converted range.

Since:

  • 2.1.0



21
22
23
# File 'lib/mongoid/fields/internal/range.rb', line 21

def deserialize(object)
  object.nil? ? nil : ::Range.new(object["min"], object["max"])
end

#selection(object) ⇒ Object

Convert the provided object to a Mongoid criteria friendly value. For ranges this will look for something between the min and max values.

Examples:

Convert the field.

field.selection(object)

Parameters:

  • The (Object)

    object to convert.

Returns:

  • (Object)

    The converted object.

Since:

  • 2.4.0



36
37
38
39
40
41
42
# File 'lib/mongoid/fields/internal/range.rb', line 36

def selection(object)
  return object if object.is_a?(::Hash)
  {
    "min" => { "$gte" => object.first },
    "max" => { "$lte" => object.last }
  }
end

#serialize(object) ⇒ Hash

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:

  • (Hash)

    The converted hash.

Since:

  • 2.1.0



55
56
57
# File 'lib/mongoid/fields/internal/range.rb', line 55

def serialize(object)
  object.nil? ? nil : { "min" => object.first, "max" => object.last }
end