Module: Mongoid::Extensions::Date::ClassMethods

Defined in:
build/mongoid-7.3/lib/mongoid/extensions/date.rb

Instance Method Summary collapse

Instance Method Details

#demongoize(object) ⇒ Date

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

Examples:

Demongoize the object.

Date.demongoize(object)

Parameters:

  • object (Time)

    The time from Mongo.

Returns:

  • (Date)

    The object as a date.

Since:

  • 3.0.0



54
55
56
# File 'build/mongoid-7.3/lib/mongoid/extensions/date.rb', line 54

def demongoize(object)
  ::Date.new(object.year, object.month, object.day) if object
end

#mongoize(object) ⇒ Time

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

Examples:

Mongoize the object.

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

Parameters:

  • object (Object)

    The object to mongoize.

Returns:

  • (Time)

    The object mongoized.

Since:

  • 3.0.0



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'build/mongoid-7.3/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