Sha256: 6d4b698ce863ab0a1160363b0ebb9f9865f45a9c08ea487f5ca2793ba3df9f27

Contents?: true

Size: 1.77 KB

Versions: 9

Compression:

Stored size: 1.77 KB

Contents

# encoding: utf-8
module Mongoid
  module Extensions
    module Date

      # Convert the date into a time.
      #
      # @example Convert the date to a time.
      #   date.__mongoize_time__
      #
      # @return [ Time ] The converted time.
      #
      # @since 3.0.0
      def __mongoize_time__
        ::Time.configured.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.
      #
      # @since 3.0.0
      def mongoize
        ::Date.mongoize(self)
      end

      module ClassMethods

        # Convert the object from it's mongo friendly ruby type to this type.
        #
        # @example Demongoize the object.
        #   Date.demongoize(object)
        #
        # @param [ Time ] object The time from Mongo.
        #
        # @return [ Date ] The object as a date.
        #
        # @since 3.0.0
        def demongoize(object)
          ::Date.new(object.year, object.month, object.day) if object
        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 ] The object mongoized.
        #
        # @since 3.0.0
        def mongoize(object)
          unless object.blank?
            time = object.__mongoize_time__
            ::Time.utc(time.year, time.month, time.day)
          end
        end
      end
    end
  end
end

::Date.__send__(:include, Mongoid::Extensions::Date)
::Date.__send__(:extend, Mongoid::Extensions::Date::ClassMethods)

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mongoid-3.0.9 lib/mongoid/extensions/date.rb
mongoid-3.0.6 lib/mongoid/extensions/date.rb
mongoid-3.0.5 lib/mongoid/extensions/date.rb
mongoid-3.0.4 lib/mongoid/extensions/date.rb
mongoid-3.0.3 lib/mongoid/extensions/date.rb
mongoid-3.0.2 lib/mongoid/extensions/date.rb
mongoid-3.0.1 lib/mongoid/extensions/date.rb
mongoid-3.0.0 lib/mongoid/extensions/date.rb
mongoid-3.0.0.rc lib/mongoid/extensions/date.rb