Sha256: 5e60b1305c43306ebef9cb1aa116423160e44d528be58fa3d3bb7925943935d7

Contents?: true

Size: 549 Bytes

Versions: 4

Compression:

Stored size: 549 Bytes

Contents

# encoding: UTF-8
module MongoMapper
  module Extensions
    module Date
      def to_mongo(value)
        if value.nil? || (value.instance_of?(String) && '' === value)
          nil
        else
          date = value.instance_of?(::Date) || value.instance_of?(::Time) ? value : ::Date.parse(value.to_s)
          ::Time.utc(date.year, date.month, date.day)
        end
      rescue
        nil
      end

      def from_mongo(value)
        value.to_date if value
      end
    end
  end
end

class Date
  extend MongoMapper::Extensions::Date
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mongo_mapper-0.16.0 lib/mongo_mapper/extensions/date.rb
mongo_mapper-0.15.6 lib/mongo_mapper/extensions/date.rb
mongo_mapper-0.15.5 lib/mongo_mapper/extensions/date.rb
mongo_mapper-0.15.4 lib/mongo_mapper/extensions/date.rb