forked from mongodb/mongoid
-
Notifications
You must be signed in to change notification settings - Fork 22
/
time_with_zone.rb
56 lines (51 loc) · 1.49 KB
/
time_with_zone.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# encoding: utf-8
module Mongoid
module Extensions
module TimeWithZone
# Turn the object from the ruby type we deal with to a Mongo friendly
# type.
#
# @example Mongoize the object.
# date_time.mongoize
#
# @return [ Time ] The object mongoized.
#
# @since 3.0.0
def mongoize
::ActiveSupport::TimeWithZone.mongoize(self)
end
module ClassMethods
# Convert the object from its mongo friendly ruby type to this type.
#
# @example Demongoize the object.
# TimeWithZone.demongoize(object)
#
# @param [ Time ] object The time from Mongo.
#
# @return [ TimeWithZone ] The object as a date.
#
# @since 3.0.0
def demongoize(object)
return nil if object.blank?
::Time.demongoize(object).in_time_zone
end
# Turn the object from the ruby type we deal with to a Mongo friendly
# type.
#
# @example Mongoize the object.
# TimeWithZone.mongoize("2012-1-1")
#
# @param [ Object ] object The object to convert.
#
# @return [ Time ] The object mongoized.
#
# @since 3.0.0
def mongoize(object)
::Time.mongoize(object)
end
end
end
end
end
::ActiveSupport::TimeWithZone.__send__(:include, Mongoid::Extensions::TimeWithZone)
::ActiveSupport::TimeWithZone.extend(Mongoid::Extensions::TimeWithZone::ClassMethods)