Support conversion of Clojure ratios to MongoDB data types

We go with doubles because it is the only realistic solution that is
interoperable with all other technologies. Plus, Clojure ratios are just
lazily evaluated doubles anyway.
This commit is contained in:
Michael S. Klishin 2012-04-02 12:14:24 +04:00
parent 59b32e119c
commit f0d190cff7
3 changed files with 17 additions and 1 deletions

View file

@ -23,7 +23,7 @@
(ns monger.conversion
(:import [com.mongodb DBObject BasicDBObject BasicDBList DBCursor]
[clojure.lang IPersistentMap Keyword]
[clojure.lang IPersistentMap Keyword Ratio]
[java.util List Map Date]
[org.bson.types ObjectId]))
@ -39,6 +39,10 @@
(to-db-object [input]
input)
Ratio
(to-db-object [^Ratio input]
(double input))
Keyword
(to-db-object [^Keyword input] (.getName input))

View file

@ -26,6 +26,11 @@
output (cnv/to-db-object input)]
(is (= input output))))
(deftest convert-rationale-to-dbobject
(let [input 11/2
output (cnv/to-db-object input)]
(is (= 5.5 output))))
(deftest convert-string-to-dbobject
(let [input "MongoDB"
output (cnv/to-db-object input)]

View file

@ -55,6 +55,13 @@
result (mgcol/insert "people" doc)]
(is (= id (monger.util/get-id doc)))))
(deftest insert-a-document-with-clojure-ratio-in-it
(let [collection "widgets"
id (ObjectId.)
doc { :ratio 11/2 "_id" id }
result (mgcol/insert "widgets" doc)]
(is (= 5.5 (:ratio (mgcol/find-map-by-id collection id))))))
;;