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
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
实施日期
def demongoize(object)
::Date.new(object.year, object.month, object.day) if object
end
def mongoize(object)
unless object.blank?
begin
time = object.__mongoize_time__
::Time.utc(time.year, time.month, time.day)
rescue ArgumentError
EPOCH
end
end
end
1条答案
按热度按时间rqmkfv5c1#
它们之间几乎没有什么区别,都是封装时间类型的,你可以改变DateTime,Date或者TimeWithZone,在从mongo反序列化之后得到这种类型的示例。
Mongoid扩展了这个类,为数据绑定添加了demongoize/mongoize方法,所以唯一的区别在于实现。
所以时间实现
实施日期
您可以检查其他实现
https://github.com/mongoid/mongoid/blob/master/lib/mongoid/extensions/date.rb#L46https://github.com/mongoid/mongoid/blob/master/lib/mongoid/extensions/date_time.rb#L49https://github.com/mongoid/mongoid/blob/master/lib/mongoid/extensions/time.rb#L48https://github.com/mongoid/mongoid/blob/master/lib/mongoid/extensions/time_with_zone.rb#L32
UPD:
对不起链接是过时的,因为他们是指向主分支