Module: Mongoid::Extensions::Date::ClassMethods

Defined in:
build/mongoid-master/lib/mongoid/extensions/date.rb

Instance Method Summary collapse

Instance Method Details

#demongoize(object) ⇒ Date | nil

Convert the object from its mongo friendly ruby type to this type.

Examples:

Demongoize the object.

Date.demongoize(object)

Parameters:

  • object (Time)

    The time from Mongo.

Returns:

  • (Date | nil)

    The object as a date or nil.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'build/mongoid-master/lib/mongoid/extensions/date.rb', line 41

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

#mongoize(object) ⇒ Time | nil

Turn the object from the ruby type we deal with to a Mongo friendly type.

Examples:

Mongoize the object.

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

Parameters:

  • object (Object)

    The object to mongoize.

Returns:

  • (Time | nil)

    The object mongoized or nil.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'build/mongoid-master/lib/mongoid/extensions/date.rb', line 65

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