Sha256: 01d38cc6a4ecec32a8ca0b5a0c56408b94dc0704ccb55e8906738cb5426ac6d3

Contents?: true

Size: 1.77 KB

Versions: 15

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 its 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

15 entries across 15 versions & 2 rubygems

Version Path
mongoid-3.0.23 lib/mongoid/extensions/date.rb
mongoid-3.0.22 lib/mongoid/extensions/date.rb
mongoid-3.0.21 lib/mongoid/extensions/date.rb
mongoid-3.0.20 lib/mongoid/extensions/date.rb
mongoid-3.0.19 lib/mongoid/extensions/date.rb
mongoid-3.0.18 lib/mongoid/extensions/date.rb
mongoid-3.0.17 lib/mongoid/extensions/date.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.16/lib/mongoid/extensions/date.rb
mongoid-3.0.16 lib/mongoid/extensions/date.rb
mongoid-3.0.15 lib/mongoid/extensions/date.rb
mongoid-3.0.14 lib/mongoid/extensions/date.rb
mongoid-3.0.13 lib/mongoid/extensions/date.rb
mongoid-3.0.12 lib/mongoid/extensions/date.rb
mongoid-3.0.11 lib/mongoid/extensions/date.rb
mongoid-3.0.10 lib/mongoid/extensions/date.rb