Module: Mongoid::Extensions::Range::ClassMethods

Defined in:
lib/mongoid/extensions/range.rb

Instance Method Summary collapse

Instance Method Details

#demongoize(object) ⇒ Range

Convert the object from its mongo friendly ruby type to this type.

Examples:

Demongoize the object.

Range.demongoize({ "min" => 1, "max" => 5 })

Parameters:

  • object (Hash)

    The object to demongoize.

Returns:

Since:

  • 3.0.0


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

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

#mongoize(object) ⇒ Hash

Turn the object from the ruby type we deal with to a Mongo friendly type.

Examples:

Mongoize the object.

Range.mongoize(1..3)

Parameters:

  • object (Range)

    The object to mongoize.

Returns:

  • (Hash)

    The object mongoized.

Since:

  • 3.0.0


70
71
72
73
74
75
76
77
78
# File 'lib/mongoid/extensions/range.rb', line 70

def mongoize(object)
  return nil if object.nil?
  return object if object.is_a?(::Hash)
  hash = { "min" => object.first, "max" => object.last }
  if object.respond_to?(:exclude_end?) && object.exclude_end?
    hash.merge!("exclude_end" => true)
  end
  hash
end