Module: Mongoid::Extensions::Date::ClassMethods
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Extended In:
| |
Defined in: | lib/mongoid/extensions/date.rb |
Instance Method Summary
-
#demongoize(object) ⇒ Date | nil
Convert the object from its mongo friendly ruby type to this type.
-
#mongoize(object) ⇒ Time | nil
Turn the object from the ruby type we deal with to a Mongo friendly type.
Instance Method Details
#demongoize(object) ⇒ Date | nil
Convert the object from its mongo friendly ruby type to this type.
# File 'lib/mongoid/extensions/date.rb', line 44
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.
# File 'lib/mongoid/extensions/date.rb', line 68
def mongoize(object) return if object.blank? begin if object.is_a?(String) # https://jira.mongodb.org/browse/MONGOID-4460 time = ::Time.parse(object) elsif object.respond_to?(:__mongoize_time__) time = object.__mongoize_time__ else nil end rescue ArgumentError nil end if time.acts_like?(:time) ::Time.utc(time.year, time.month, time.day) end end