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

Defined in:
build/mongoid-8.1/lib/mongoid/extensions/time.rb

Instance Method Summary collapse

Instance Method Details

#configuredTime

Deprecated.

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

Examples:

Get the configured time.

::Time.configured

Returns:

  • (Time)

    The configured time.



39
40
41
# File 'build/mongoid-8.1/lib/mongoid/extensions/time.rb', line 39

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

#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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'build/mongoid-8.1/lib/mongoid/extensions/time.rb', line 51

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
  end

  return if time.nil?

  if Mongoid::Config.use_activesupport_time_zone?
    time.in_time_zone(Mongoid.time_zone)
  else
    time
  end
end

#mongoize(object) ⇒ Time | nil

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 | nil)

    The object mongoized or nil.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'build/mongoid-8.1/lib/mongoid/extensions/time.rb', line 83

def mongoize(object)
  return if object.blank?
  begin
    time = object.__mongoize_time__
  rescue ArgumentError
    return
  end

  if time.acts_like?(: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
  end
end