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"]]
:dev-dependencies [[org.clojure/data.json "0.1.1"]
[clj-time "0.3.1"]]
:dev-resources-path "test/resources"
: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]
[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,9 +505,7 @@
;;
(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 });

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);
}