Class: Mongoid::Fields::Internal::Range
- 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
-
#deserialize(object) ⇒ Range
Deserialize this field from the type stored in MongoDB to the type defined on the model.
-
#selection(object) ⇒ Object
Convert the provided object to a Mongoid criteria friendly value.
-
#serialize(object) ⇒ Hash
Serialize the object from the type defined in the model to a MongoDB compatible object to store.
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.
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.
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.
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 |