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

Defined in:
build/mongoid-7.3/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



57
58
59
# File 'build/mongoid-7.3/lib/mongoid/extensions/range.rb', line 57

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



72
73
74
75
76
77
78
79
80
81
# File 'build/mongoid-7.3/lib/mongoid/extensions/range.rb', line 72

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