模块:Mongoid::Extensions::Time::ClassMethods

定义于:
lib/mongoid/extensions/time.rb

实例方法摘要折叠

实例方法详细信息

# demogoize (对象) ⇒时间| nil

将对象从其 mongo 友好的 Ruby 类型转换为此类型。

例子:

将对象去妖化。

Time.demongoize(object)

参数:

  • 对象 时间

    来自mongo的时间。

返回:

  • (时间| nil )

    作为时间的对象。



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mongoid/extensions/time.rb', line 41

def demogoize(对象)
  return if 对象.空白?
  时间 = if 对象.acts_like?(:time)
    Mongoid::配置.use_utc? ? 对象 : 对象.getlocal
  elsif 对象.acts_like?(:date)
    ::Date.demogoize(对象).to_time
  elsif 对象.is_a?(字符串)
    开始
      对象.__mongoize_time__
    救援 ArgumentError
      nil
    end
  elsif 对象.is_a?(BSON::时间戳)
    ::时间.at(对象.秒数)
  end

  return if 时间.nil?

  时间.in_time_zone(Mongoid.time_zone)
end

# mongoize (对象) ⇒时间| nil

将对象从我们处理的Ruby类型转换为mongo友好类型。

例子:

将对象 Mongoize。

Time.mongoize("2012-1-1")

参数:

  • 对象 ( Object )

    要 mongoize 的对象。

返回:

  • (时间| nil )

    对象mongoized 或 nil。



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mongoid/extensions/time.rb', line 71

def mongoize(对象)
  return if 对象.空白?
  开始
    时间 = 对象.respond_to?(:__mongoize_time__) ? 对象.__mongoize_time__ : nil
  救援 ArgumentError
    return
  end

  if 时间.acts_like?(:time)
    if 对象.respond_to?(:sec_fraction)
      ::时间.at(时间.to_i, 对象.sec_fraction * 10**6).utc
    elsif 时间.respond_to?(:subsec)
      ::时间.at(时间.to_i, 时间.亚秒数 * 10**6).utc
    else
      ::时间.at(时间.to_i, 时间.usec).utc
    end
  end
end