Module: Mongoid::Fields::Internal::Timekeeping

Included in:
Date, DateTime, Time, TimeWithZone
Defined in:
lib/mongoid/fields/internal/timekeeping.rb

Overview

This module contains shared behaviour for date conversions.

Instance Method Summary collapse

Instance Method Details

#cast_on_read?true

When reading the field do we need to cast the value? This holds true when times are stored or for big decimals which are stored as strings.

Examples:

Typecast on a read?

field.cast_on_read?

Returns:

  • (true)

    Date fields cast on read.

Since:

  • 2.1.0



18
# File 'lib/mongoid/fields/internal/timekeeping.rb', line 18

def cast_on_read?; true; end

#convert_to_time(value) ⇒ Time

Convert the provided object to a UTC time to store in the database.

Examples:

Set the time.

Time.convert_to_time(Date.today)

Parameters:

Returns:

  • (Time)

    The object as a UTC time.

Since:

  • 1.0.0



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mongoid/fields/internal/timekeeping.rb', line 87

def convert_to_time(value)
  time = Mongoid::Config.use_activesupport_time_zone? ? ::Time.zone : ::Time
  case value
    when ::String
      time.parse(value)
    when ::DateTime
      return value if value.utc? && Mongoid.use_utc?
      time.local(value.year, value.month, value.day, value.hour, value.min, value.sec)
    when ::Date
      time.local(value.year, value.month, value.day)
    when ::Array
      time.local(*value)
    else
      value
  end
end

#deserialize(object) ⇒ Time

Deserialize this field from the type stored in MongoDB to the type defined on the model.

Examples:

Deserialize the field.

field.deserialize(object)

Parameters:

  • object (Object)

    The object to cast.

Returns:

  • (Time)

    The converted time.

Since:

  • 2.1.0



31
32
33
34
35
36
37
38
39
# File 'lib/mongoid/fields/internal/timekeeping.rb', line 31

def deserialize(object)
  return nil if object.blank?
  object = object.getlocal unless Mongoid::Config.use_utc?
  if Mongoid::Config.use_activesupport_time_zone?
    time_zone = Mongoid::Config.use_utc? ? "UTC" : ::Time.zone
    object = object.in_time_zone(time_zone)
  end
  object
end

#selection(object) ⇒ Object

Special case to serialize the object.

Examples:

Convert to a selection.

field.selection(object)

Parameters:

  • The (Object)

    object to convert.

Returns:

  • (Object)

    The converted object.

Since:

  • 2.4.0



51
52
53
54
# File 'lib/mongoid/fields/internal/timekeeping.rb', line 51

def selection(object)
  return object if object.is_a?(::Hash)
  serialize(object)
end

#serialize(object) ⇒ Time

Serialize the object from the type defined in the model to a MongoDB compatible object to store.

Examples:

Serialize the field.

field.serialize(object)

Parameters:

  • object (Object)

    The object to cast.

Returns:

  • (Time)

    The converted UTC time.

Since:

  • 2.1.0



67
68
69
70
71
72
73
74
75
# File 'lib/mongoid/fields/internal/timekeeping.rb', line 67

def serialize(object)
  return nil if object.blank?
  begin
    time = convert_to_time(object)
    strip_milliseconds(time).utc
  rescue ArgumentError
    raise Errors::InvalidTime.new(object)
  end
end

#strip_milliseconds(time) ⇒ Time

TODO:

Durran: Why is this here? Still need time refactoring.

Strip the milliseconds off the time.

Examples:

Strip.

Time.strip_millseconds(Time.now)

Parameters:

  • time (Time)

    The time to strip.

Returns:

  • (Time)

    The time without millis.

Since:

  • 2.1.0



116
117
118
# File 'lib/mongoid/fields/internal/timekeeping.rb', line 116

def strip_milliseconds(time)
  ::Time.at(time.to_i)
end