/
date.rb
90 lines (81 loc) · 2.48 KB
/
date.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# frozen_string_literal: true
# rubocop:todo all
module Mongoid
module Extensions
# Adds type-casting behavior to Date class.
module Date
# Convert the date into a time.
#
# @example Convert the date to a time.
# Date.new(2018, 11, 1).__mongoize_time__
# # => Thu, 01 Nov 2018 00:00:00 EDT -04:00
#
# @return [ Time | ActiveSupport::TimeWithZone ] Local time in the
# configured default time zone corresponding to local midnight of
# this date.
def __mongoize_time__
::Time.zone.local(year, month, day)
end
# Turn the object from the ruby type we deal with to a Mongo friendly
# type.
#
# @example Mongoize the object.
# date.mongoize
#
# @return [ Time ] The object mongoized.
def mongoize
::Date.mongoize(self)
end
module ClassMethods
# Convert the object from its mongo friendly ruby type to this type.
#
# @example Demongoize the object.
# Date.demongoize(object)
#
# @param [ Time ] object The time from Mongo.
#
# @return [ Date | nil ] The object as a date or nil.
def demongoize(object)
return if object.nil?
if object.is_a?(String)
object = begin
object.__mongoize_time__
rescue ArgumentError
nil
end
end
if object.acts_like?(:time) || object.acts_like?(:date)
::Date.new(object.year, object.month, object.day)
end
end
# Turn the object from the ruby type we deal with to a Mongo friendly
# type.
#
# @example Mongoize the object.
# Date.mongoize("2012-1-1")
#
# @param [ Object ] object The object to mongoize.
#
# @return [ Time | nil ] The object mongoized or nil.
def mongoize(object)
return if object.blank?
begin
if object.is_a?(String)
# https://jira.mongodb.org/browse/MONGOID-4460
time = ::Time.parse(object)
else
time = object.__mongoize_time__
end
rescue ArgumentError
nil
end
if time.acts_like?(:time)
::Time.utc(time.year, time.month, time.day)
end
end
end
end
end
end
::Date.__send__(:include, Mongoid::Extensions::Date)
::Date.extend(Mongoid::Extensions::Date::ClassMethods)