Permalink
Browse files

Refactoring: clean up, remove duplication

  • Loading branch information...
dnaumov committed May 17, 2012
1 parent 29dfa5c commit ebb9f402cbe834ac49911a3277149b1b2185ccba
Showing with 27 additions and 25 deletions.
  1. +1 −2 project.clj
  2. +25 −22 src/robert/hooke.clj
  3. +1 −1 test/robert/test_hooke.clj
View
@@ -2,5 +2,4 @@
:description "Hooke your functions!"
:dev-dependencies [[org.clojure/clojure "1.4.0"]]
:license {:name "Eclipse Public License"
- :url "http://www.eclipse.org/legal/epl-v10.html"}
- :test-selectors {:default (complement :skip)})
+ :url "http://www.eclipse.org/legal/epl-v10.html"})
View
@@ -28,26 +28,32 @@
Use the add-hook function to wrap a function in your a hook.")
+(defn- hooks [v]
+ (-> @v meta ::hooks))
+
+(defn- original [v]
+ (-> @v meta ::original))
+
(defn- compose-hooks [f1 f2]
(fn [& args]
(apply f2 f1 args)))
(defn- join-hooks [original hooks]
(reduce compose-hooks original hooks))
-(defn- run-hooks [hook original args]
- (apply (join-hooks original (vals @hook)) args))
+(defn- run-hooks [hooks original args]
+ (apply (join-hooks original hooks) args))
(defn- prepare-for-hooks [v]
- (when-not (:robert.hooke/hook (meta @v))
- (let [hook (atom {})]
+ (when-not (hooks v)
+ (let [hooks (atom {})]
(alter-var-root v (fn [original]
(with-meta
(fn [& args]
- (run-hooks hook original args))
+ (run-hooks (vals @hooks) original args))
(assoc (meta original)
- :robert.hooke/hook hook
- :robert.hooke/original original)))))))
+ ::hooks hooks
+ ::original original)))))))
(defn add-hook
"Add a hook function f to target-var. Hook functions are passed the
@@ -57,29 +63,26 @@
(add-hook target-var f f))
([target-var key f]
(prepare-for-hooks target-var)
- (swap! (:robert.hooke/hook (meta @target-var)) assoc key f)))
+ (swap! (hooks target-var) assoc key f)))
(defn- clear-hook-mechanism [target-var]
(alter-var-root target-var
- (constantly (:robert.hooke/original
- (meta @target-var)))))
+ (constantly (original target-var))))
(defn remove-hook
"Remove hook identified by key from target-var."
[target-var key]
- (when (:robert.hooke/hook (meta @target-var))
- (swap! (:robert.hooke/hook (meta @target-var))
- dissoc key)
- (when (empty? @(:robert.hooke/hook (meta @target-var)))
+ (when-let [hooks (hooks target-var)]
+ (swap! hooks dissoc key)
+ (when (empty? @hooks)
(clear-hook-mechanism target-var))))
(defn clear-hooks
"Remove all hooks from target-var."
[target-var]
- (when (:robert.hooke/hook (meta @target-var))
- (swap! (:robert.hooke/hook (meta @target-var)) empty)
- (when (empty? @(:robert.hooke/hook (meta @target-var)))
- (clear-hook-mechanism target-var))))
+ (when-let [hooks (hooks target-var)]
+ (swap! hooks empty)
+ (clear-hook-mechanism target-var)))
(defmacro prepend [target-var & body]
`(add-hook (var ~target-var) (fn [f# & args#]
@@ -92,8 +95,8 @@
~@body
val#))))
-(defmacro with-hooks-disabled [v & body]
- `(do (when-not (:robert.hooke/hook (meta ~v))
- (throw (Exception. (str "No hooks on " ~v))))
- (with-redefs [~v (:robert.hooke/original (meta ~v))]
+(defmacro with-hooks-disabled [f & body]
+ `(do (when-not (#'hooks (var ~f))
+ (throw (Exception. (str "No hooks on " ~f))))
+ (with-redefs [~f (#'original (var ~f))]
~@body)))
@@ -33,7 +33,7 @@
(deftest test-clear-hooks
(letfn [(hooked? [v]
- (contains? (meta @v) :robert.hooke/hook))]
+ (contains? (meta @v) :robert.hooke/hooks))]
(is (not (hooked? #'hooked)))
(add-hook #'hooked #'asplode)
(is (hooked? #'hooked))

0 comments on commit ebb9f40

Please sign in to comment.