This commit is contained in:
Oleksandr Petrov 2011-10-16 16:04:55 +02:00
commit edbaec0a3b
5 changed files with 45 additions and 7 deletions

View file

@ -6,4 +6,5 @@
[com.novemberain/validateur "1.0.0-SNAPSHOT"]] [com.novemberain/validateur "1.0.0-SNAPSHOT"]]
:dev-dependencies [[org.clojure/data.json "0.1.1"] :dev-dependencies [[org.clojure/data.json "0.1.1"]
[clj-time "0.3.1"]] [clj-time "0.3.1"]]
:dev-resources-path "test/resources"
:warn-on-reflection true) :warn-on-reflection true)

25
src/monger/js.clj Normal file
View file

@ -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)))))

View file

@ -8,7 +8,8 @@
[clojure stacktrace] [clojure stacktrace]
[monger.collection :as mgcol] [monger.collection :as mgcol]
[monger.result :as mgres] [monger.result :as mgres]
[monger.conversion :as mgcnv]) [monger.conversion :as mgcnv]
[monger.js :as js])
(:use [clojure.test])) (:use [clojure.test]))
(monger.core/connect!) (monger.core/connect!)
@ -504,14 +505,12 @@
;; ;;
(let [collection "widgets" (let [collection "widgets"
mapper "function() { mapper (js/load-resource "resources/mongo/js/mapfun1.js")
emit(this.state, this.price * this.quantity)
}"
reducer "function(key, values) { reducer "function(key, values) {
var result = 0; var result = 0;
values.forEach(function(v) { result += v }); values.forEach(function(v) { result += v });
return result; return result;
}" }"
batch [{ :state "CA" :quantity 1 :price 199.00 } batch [{ :state "CA" :quantity 1 :price 199.00 }
{ :state "NY" :quantity 2 :price 199.00 } { :state "NY" :quantity 2 :price 199.00 }

10
test/monger/test/js.clj Normal file
View file

@ -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"))

View file

@ -0,0 +1,3 @@
function() {
emit(this.state, this.price * this.quantity);
}