module Mongoid::Extensions::Date::ClassMethods

Public Instance Methods

demongoize(object) click to toggle source

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

@example Demongoize the object.

Date.demongoize(object)

@param [ Time ] object The time from Mongo.

@return [ Date ] The object as a date.

@since 3.0.0

# File lib/mongoid/extensions/date.rb, line 54
def demongoize(object)
  ::Date.new(object.year, object.month, object.day) if object
end
mongoize(object) click to toggle source

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

@example Mongoize the object.

Date.mongoize("2012-1-1")

@param [ Object ] object The object to mongoize.

@return [ Time ] The object mongoized.

@since 3.0.0

# File lib/mongoid/extensions/date.rb, line 69
def mongoize(object)
  unless object.blank?
    begin
      if object.is_a?(String)
        # https://jira.mongodb.org/browse/MONGOID-4460
        time = ::Time.parse(object)
      else
        time = object.__mongoize_time__
      end
      ::Time.utc(time.year, time.month, time.day)
    rescue ArgumentError
      nil
    end
  end
end