Module: Mongoid::Extensions::Time::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#configuredObject

Get the configured time to use when converting - either the time zone or the time.

Examples:

Get the configured time.

::Time.configured

Since:

  • 3.0.0


33
34
35
# File 'lib/mongoid/extensions/time.rb', line 33

def configured
  Mongoid.use_activesupport_time_zone? ? (::Time.zone || ::Time) : ::Time
end

#demongoize(object) ⇒ Time

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)

    The object as a date.

Since:

  • 3.0.0


47
48
49
50
51
52
53
54
# File 'lib/mongoid/extensions/time.rb', line 47

def demongoize(object)
  return nil if object.blank?
  object = object.getlocal unless Mongoid::Config.use_utc?
  if Mongoid::Config.use_activesupport_time_zone?
    object = object.in_time_zone(Mongoid.time_zone)
  end
  object
end

#mongoize(object) ⇒ Time

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

Examples:

Mongoize the object.

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

Parameters:

  • object (Object)

    The object to mongoize.

Returns:

  • (Time)

    The object mongoized.

Since:

  • 3.0.0


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mongoid/extensions/time.rb', line 67

def mongoize(object)
  return nil if object.blank?
  begin
    time = object.__mongoize_time__
    if object.respond_to?(:sec_fraction)
      ::Time.at(time.to_i, object.sec_fraction * 10**6).utc
    elsif time.respond_to?(:subsec)
      ::Time.at(time.to_i, time.subsec * 10**6).utc
    else
      ::Time.at(time.to_i, time.usec).utc
    end
  rescue ArgumentError
    EPOCH
  end
end