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

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

Instance Method Summary collapse

Instance Method Details

#demongoize(object) ⇒ Range | nil

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.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mongoid/extensions/range.rb', line 54

def demongoize(object)
  return if object.nil?
  if object.is_a?(Hash)
    hash = object.slice('min', 'max', 'exclude_end', :min, :max, :exclude_end)
    unless hash.blank?
      begin
        ::Range.new(hash["min"] || hash[:min],
                    hash["max"] || hash[:max],
                    hash["exclude_end"] || hash[:exclude_end])
      rescue ArgumentError
        nil
      end
    end
  end
end

#mongoize(object) ⇒ Hash | nil

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 (Object)

    The object to mongoize.

Returns:

  • (Hash | nil)

    The object mongoized or nil.



79
80
81
82
83
84
85
# File 'lib/mongoid/extensions/range.rb', line 79

def mongoize(object)
  return if object.nil?
  case object
  when Hash then __mongoize_hash__(object)
  when Range then __mongoize_range__(object)
  end
end