Method: Mongoid::Extensions::Time::ClassMethods#demongoize

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

#demongoize(object) ⇒ Time | nil

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

Examples:

Demongoize the object.

Time.demongoize(object)

Parameters:

  • object (Time)

    The time from Mongo.

Returns:

  • (Time | nil)

    The object as a time.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mongoid/extensions/time.rb', line 41

def demongoize(object)
  return if object.blank?
  time = if object.acts_like?(:time)
    Mongoid::Config.use_utc? ? object : object.getlocal
  elsif object.acts_like?(:date)
    ::Date.demongoize(object).to_time
  elsif object.is_a?(String)
    begin
      object.__mongoize_time__
    rescue ArgumentError
      nil
    end
  elsif object.is_a?(BSON::Timestamp)
    ::Time.at(object.seconds)
  end

  return if time.nil?

  time.in_time_zone(Mongoid.time_zone)
end