diff --git a/project.clj b/project.clj index 075be59..0b27c81 100644 --- a/project.clj +++ b/project.clj @@ -6,4 +6,5 @@ [com.novemberain/validateur "1.0.0-SNAPSHOT"]] :dev-dependencies [[org.clojure/data.json "0.1.1"] [clj-time "0.3.1"]] + :dev-resources-path "test/resources" :warn-on-reflection true) diff --git a/src/monger/js.clj b/src/monger/js.clj new file mode 100644 index 0000000..b443ef5 --- /dev/null +++ b/src/monger/js.clj @@ -0,0 +1,25 @@ +(ns monger.js + (:require [clojure.java.io])) + +;; +;; Implementation +;; + +(defn- normalize-resource + [^String path] + (if (.endsWith path ".js") + path + (str path ".js"))) + + + +;; +;; API +;; + +(defn load-resource + (^String [^String path] + (with-open [rdr (clojure.java.io/reader (-> (Thread/currentThread) + .getContextClassLoader + (.getResourceAsStream (normalize-resource path))))] + (reduce str "" (line-seq rdr))))) \ No newline at end of file diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index 17db479..88daa1e 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -8,7 +8,8 @@ [clojure stacktrace] [monger.collection :as mgcol] [monger.result :as mgres] - [monger.conversion :as mgcnv]) + [monger.conversion :as mgcnv] + [monger.js :as js]) (:use [clojure.test])) (monger.core/connect!) @@ -504,14 +505,12 @@ ;; (let [collection "widgets" - mapper "function() { - emit(this.state, this.price * this.quantity) - }" + mapper (js/load-resource "resources/mongo/js/mapfun1.js") reducer "function(key, values) { - var result = 0; - values.forEach(function(v) { result += v }); + var result = 0; + values.forEach(function(v) { result += v }); - return result; + return result; }" batch [{ :state "CA" :quantity 1 :price 199.00 } { :state "NY" :quantity 2 :price 199.00 } diff --git a/test/monger/test/js.clj b/test/monger/test/js.clj new file mode 100644 index 0000000..8308bbf --- /dev/null +++ b/test/monger/test/js.clj @@ -0,0 +1,10 @@ +(ns monger.test.js + (:require [monger js] + [clojure.java.io :only [reader]]) + (:use [clojure.test])) + + +(deftest load-js-resource-using-path-on-the-classpath + (are [c path] (= c (count (monger.js/load-resource path))) + 60 "resources/mongo/js/mapfun1.js" + 60 "resources/mongo/js/mapfun1")) diff --git a/test/resources/mongo/js/mapfun1.js b/test/resources/mongo/js/mapfun1.js new file mode 100644 index 0000000..0c417df --- /dev/null +++ b/test/resources/mongo/js/mapfun1.js @@ -0,0 +1,3 @@ +function() { + emit(this.state, this.price * this.quantity); +} \ No newline at end of file