Introduce monger.js/load-resource, a helper that loads JS files from the classpath
Yes, to be used with map/reduce and so on.
This commit is contained in:
parent
ab99184914
commit
4336b6f414
3 changed files with 38 additions and 0 deletions
25
src/monger/js.clj
Normal file
25
src/monger/js.clj
Normal 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)))))
|
||||
10
test/monger/test/js.clj
Normal file
10
test/monger/test/js.clj
Normal 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"))
|
||||
3
test/resources/mongo/js/mapfun1.js
Normal file
3
test/resources/mongo/js/mapfun1.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
function() {
|
||||
emit(this.state, this.price * this.quantity);
|
||||
}
|
||||
Loading…
Reference in a new issue