Sha256: 1d4026d6857e73ce78454b7a587fe8417732c0c83f03f467783b8c09f28e9b43

Contents?: true

Size: 679 Bytes

Versions: 5

Compression:

Stored size: 679 Bytes

Contents

# encoding: UTF-8
module MongoMapper
  module Extensions
    module Time
      def to_mongo(value)
        if value.nil? || value == ''
          nil
        else
          time_class = ::Time.try(:zone).present? ? ::Time.zone : ::Time
          time = value.is_a?(::Time) ? value : time_class.parse(value.to_s)
          at(time.to_f).utc if time # ensure milliseconds are preserved with to_f (issue #308)
        end
      end

      def from_mongo(value)
        if ::Time.try(:zone).present? && value.present?
          value.in_time_zone(::Time.zone)
        else
          value
        end
      end
    end
  end
end

class Time
  extend MongoMapper::Extensions::Time
end

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
mongo_mapper-0.12.0 lib/mongo_mapper/extensions/time.rb
lookout-mongo_mapper-0.11.3 lib/mongo_mapper/extensions/time.rb
mongo_mapper-0.11.2 lib/mongo_mapper/extensions/time.rb
jamieorc-mongo_mapper-0.11.1.1 lib/mongo_mapper/extensions/time.rb
mongo_mapper-0.11.1 lib/mongo_mapper/extensions/time.rb