Commits

Anonymous committed 543c719

refactor - move kv-to-sv into internal.clj

  • Participants
  • Parent commits 3a3d0d9

Comments (0)

Files changed (4)

     (let [view   (get-view-from-classpath "hello")
           filled (fill-view! view "name" "Henry")]
       (println (render-view filled)))
-
-
-## Transform keyword-key/value map to string-key/value map
-
-    (kv-to-sv {:a 10 :b 20 :c 30})
-
-The snippet above will produce the output {"a" 10 "b" 20 "c" 30}. Such
-transformations may be useful to convert typical Clojure maps to a
-format suitable for StringTemplate view templates.

File src/main/clj/org/bituf/clj_stringtemplate.clj

 (ns org.bituf.clj-stringtemplate
   (:import org.antlr.stringtemplate.StringTemplateGroup)
-  (:import org.antlr.stringtemplate.StringTemplate))
+  (:import org.antlr.stringtemplate.StringTemplate)
+  (:use org.bituf.clj-stringtemplate.internal))
 
 (defn get-view-from-classpath "Return the view template from classpath"
   [^String view-name]
   ;;;
   ;; Fill template with key/value from map
   ([^StringTemplate template ^Map kv-map]
-    (.setAttributes template kv-map)
+    (.setAttributes template (kv-to-sv kv-map))
     template))
 
 (defn render-view "Return rendered view for the template"
   [^StringTemplate template]
   (.toString template))
-
-(defn kv-to-sv
-  "Transforms keyword-value map {:a 10 :b 20 :c 30}
-   to string-value map {\"a\" 10 \"b\" 20 \"c\" 30}"
-  [mp]
-  (let [m (into {} mp)
-        k (keys m)
-        v (vals m)]
-    (zipmap (map #(if (keyword? %) (name %) (str %)) k) v)))

File src/main/clj/org/bituf/clj_stringtemplate/internal.clj

+(ns org.bituf.clj-stringtemplate.internal)
+
+(defn kv-to-sv
+  "Transforms keyword-value map {:a 10 :b 20 :c 30}
+   to string-value map {\"a\" 10 \"b\" 20 \"c\" 30}"
+  [mp]
+  (let [m (into {} mp)
+        k (keys m)
+        v (vals m)]
+    (zipmap (map #(if (keyword? %) (name %)
+                    (str %)) k) v)))

File src/test/clj/org/bituf/test_clj_stringtemplate.clj

 (ns org.bituf.test-clj-stringtemplate
   (:use org.bituf.clj-stringtemplate)
+  (:use org.bituf.clj-stringtemplate.internal)
   (:use clojure.test))
 
 
           (kv-to-sv {:a 10 "b" 20 :c 30})))))
 
 (defn test-ns-hook []
+  (test-kv-to-sv)
   (test-get-view-from-classpath)
   (test-get-view-from-dir)
   (test-populate-attr-values-in-view)
-  (test-render-view)
-  (test-kv-to-sv))
+  (test-render-view))