Skip to content

Instantly share code, notes, and snippets.

@robertpfeiffer
Created January 25, 2009 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertpfeiffer/51776 to your computer and use it in GitHub Desktop.
Save robertpfeiffer/51776 to your computer and use it in GitHub Desktop.
(defn y [a b] (* 3 a b))
;weave an aspect to function in current Namespace
(weave #(= 'y (:name (meta %))) [*ns*]
(before-advice (fn [& args] (print args) args)))
(def io-aspect [(around-advice (fn [proceed]
(fn [& args] (io! (apply proceed args)))))
(after-advice (fn [result] (flush) result))])
(apply weave #(:output (meta %)) [*ns*] io-aspect)
(def in-my-layer nil)
(deflayer my-layer
in-my-layer true
;restrict weaving to this layer
weave (fn [pointcut namespaces & advice]
(proceed pointcut namespaces (fn [fun] (fn [& args]
(apply ((if in-my-layer (apply comp advice) identity) fun) args))))))
(defn xyz [& args] 12)
(with-layer my-layer
(weave #(= 'xyz (:name (meta %))) [*ns*]
(before-advice (fn [& args] (print args) args)))
(xyz 1 2 3))
(xyz 1 2 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment