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

Defined in:
build/mongoid-master/lib/mongoid/extensions/range.rb

Instance Method Summary collapse

Instance Method Details

#demongoize(object) ⇒ Range | nil

Note:

Ruby 2.6 and lower do not support endless ranges that Ruby 2.7+ support.

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:

  • (Range | nil)

    The range, or nil if object cannot be represented as range.



50
51
52
53
54
# File 'build/mongoid-master/lib/mongoid/extensions/range.rb', line 50

def demongoize(object)
  object.nil? ? nil : ::Range.new(object["min"], object["max"], object["exclude_end"])
rescue ArgumentError # can be removed when Ruby version >= 2.7
  nil
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.



65
66
67
68
69
70
71
72
# File 'build/mongoid-master/lib/mongoid/extensions/range.rb', line 65

def mongoize(object)
  case object
  when NilClass then nil
  when String then object
  when Hash then __mongoize_hash__(object)
  else __mongoize_range__(object)
  end
end