Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add useful.config

  • Loading branch information...
commit 36b368c98c7889fcb272ca1eb81412349836b798 1 parent 4e6dd72
@amalloy amalloy authored
View
19 src/useful/config.clj
@@ -0,0 +1,19 @@
+(ns useful.config
+ (:require [clojure.java.io :as io]))
+
+(defn read-config [filename]
+ (with-open [in (java.io.PushbackReader. (io/reader (io/resource filename)))]
+ (let [eof (Object.)
+ forms (take-while (complement #{eof})
+ (repeatedly #(binding [*read-eval* false]
+ (read in false eof))))]
+ (cond (not (seq forms))
+ (throw (IllegalArgumentException. (format "No config data in %s" filename)))
+
+ (next forms)
+ (throw (IllegalArgumentException. (format "Too many forms in %s" filename)))
+
+ :else (first forms)))))
+
+(defn load-config [filename]
+ (eval (read-config filename)))
View
1  test/config1.clj
@@ -0,0 +1 @@
+{size 1}
View
2  test/config2.clj
@@ -0,0 +1,2 @@
+(let [point [1 1]]
+ {:x point, :y point})
View
10 test/useful/config_test.clj
@@ -0,0 +1,10 @@
+(ns useful.config-test
+ (:use clojure.test useful.config))
+
+(deftest reading
+ (is (= '{size 1} (read-config "config1.clj"))))
+
+(deftest loading
+ (is (= {:x [1 1]
+ :y [1 1]}
+ (load-config "config2.clj"))))
Please sign in to comment.
Something went wrong with that request. Please try again.