From 1b4fa0fa6f5bdb1034e66af4386156fcad625368 Mon Sep 17 00:00:00 2001 From: gered Date: Sat, 22 Mar 2014 18:54:33 -0400 Subject: [PATCH] add utils namespace. move common utility functions over --- src/clj_jtwig/core.clj | 20 ++------------------ src/clj_jtwig/utils.clj | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 18 deletions(-) create mode 100644 src/clj_jtwig/utils.clj diff --git a/src/clj_jtwig/core.clj b/src/clj_jtwig/core.clj index 2655bc3..496c043 100644 --- a/src/clj_jtwig/core.clj +++ b/src/clj_jtwig/core.clj @@ -5,7 +5,8 @@ (java.io File FileNotFoundException ByteArrayOutputStream) (java.net URL)) (:require [clojure.walk :refer [stringify-keys]]) - (:use [clj-jtwig.functions])) + (:use [clj-jtwig.functions] + [clj-jtwig.utils])) ; global options (defonce options (atom {; true/false to enable/disable compiled template caching when using templates from @@ -59,17 +60,6 @@ (new JtwigTemplate) (.compile))) -(defn- inside-jar? [^File file] - (-> file - (.getPath) - ; the path of a file inside a jar looks something like "jar:file:/path/to/file.jar!/path/to/file" - (.contains "jar!"))) - -(defn- get-file-last-modified [^File file] - (if (inside-jar? file) - 0 - (.lastModified file))) - (defn- newer? [^File file other-timestamp] (let [file-last-modified (get-file-last-modified file)] ; a time of 0 means the modification time couldn't be read or the file is inside a JAR container. if it's an I/O @@ -129,12 +119,6 @@ [] (reset! compiled-templates {})) -(defn- get-resource-path - (^URL [^String filename] - (-> (Thread/currentThread) - (.getContextClassLoader) - (.getResource filename)))) - (defn- make-model-map [model-map-values {:keys [skip-model-map-stringify?] :as options}] (let [model-map-obj (new JtwigModelMap) values (if-not skip-model-map-stringify? diff --git a/src/clj_jtwig/utils.clj b/src/clj_jtwig/utils.clj new file mode 100644 index 0000000..930e4c7 --- /dev/null +++ b/src/clj_jtwig/utils.clj @@ -0,0 +1,28 @@ +(ns clj-jtwig.utils + "various helper / utility functions" + (:import (java.net URL) + (java.io File))) + +(defn inside-jar? [^File file] + (-> file + (.getPath) + ; the path of a file inside a jar looks something like "jar:file:/path/to/file.jar!/path/inside/jar/to/file" + (.contains "jar!"))) + +(defn get-file-last-modified [^File file] + (if (inside-jar? file) + 0 + (.lastModified file))) + +(defn get-resource-path + (^URL [^String filename] + (-> (Thread/currentThread) + (.getContextClassLoader) + (.getResource filename)))) + +(defn get-resource-modification-date [^String filename] + (when-let [resource-filename (get-resource-path filename)] + (->> resource-filename + (.getPath) + (new File) + (get-file-last-modified)))) \ No newline at end of file