From 8bd4378f935761550f4726553180adf2d206d17a Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Mon, 15 Aug 2011 01:43:02 +0400 Subject: [PATCH] Introduce monger.core/Countable protocol --- src/monger/core.clj | 10 ++++++++++ test/monger/test/collection.clj | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/monger/core.clj b/src/monger/core.clj index 83cc57b..d5d784b 100644 --- a/src/monger/core.clj +++ b/src/monger/core.clj @@ -40,3 +40,13 @@ (.getDB *mongodb-connection* name)) ([^Mongo connection, ^String name] (.getDB connection name))) + + + +(defprotocol Countable + (count [this] "Returns size of the object")) + +(extend-protocol Countable + com.mongodb.DBCursor + (count [^com.mongodb.DBCursor this] + (.count this))) diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index 2f736ec..af8e66b 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -138,7 +138,7 @@ (monger.collection/insert collection { :language "Clojure", :name "langohr" }) (monger.collection/insert collection { :language "Clojure", :name "incanter" }) (monger.collection/insert collection { :language "Scala", :name "akka" }) - (is (= 1 (.count (monger.collection/find collection { :language "Scala" })))) + (is (= 1 (monger.core/count (monger.collection/find collection { :language "Scala" })))) (is (= 3 (.count (monger.collection/find collection { :language "Clojure" })))) (is (empty? (monger.collection/find collection { :language "Java" })))))