Created
September 20, 2010 15:51
-
-
Save MayDaniel/588112 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro defcommand | |
{:arglists '([command docstring? attr-map? args & fn-tail])} | |
[command & fn-tail] | |
(let [[command [args & body]] (name-with-attributes command fn-tail)] | |
`(do (defn ~command ~@fn-tail) | |
(meta (var ~command))))) | |
(comment | |
(ns clj-chat.core | |
(:use [clojure.contrib.str-utils2 :only [drop]] | |
[clojure.contrib.def :only [name-with-attributes]] | |
[clojure.string :only [split lower-case]]) | |
(:import clojure.lang.IDeref)) | |
(defn command | |
"\"/foo bar baz\" -> \"foo\"" | |
[input] | |
(-> (split input #"\s+") | |
(first) (lower-case) (drop 1))) | |
(defmulti execute command) | |
(defmacro defcommand | |
{:arglists '([command docstring? attr-map? args & fn-tail])} | |
[command & fn-tail] | |
(let [[command [args & body]] (name-with-attributes command fn-tail) | |
docstring (:doc (meta command)) | |
[args body] (if (vector? args) | |
[args body] | |
[[] (cons args body)])] | |
`(defmethod ~'execute ~(lower-case (str command)) [~'input] ~@body))) | |
(defprotocol PPlugins | |
(load [_] [_ namespace] "Loads all, or a single plug-in.") | |
(unload [_] [_ command] "Unloads all, or a single plug-in.") | |
(update [_] "Reloads the plug-in configuration file.") | |
(reload [_] "Updates, unloads, loads.")) | |
(defrecord Plugins | |
[loaded loadable] | |
IDeref | |
(deref [_] {:loaded @loaded, :loadable @loadable}) | |
PPlugins | |
(load [_ ns] | |
(use (symbol (str "clj-chat.plugins." ns)) :reload) | |
(swap! loaded conj ns)) | |
(load [plugins] | |
(dorun (map #(load plugins %) (keys @loadable)))) | |
(unload [plugins ns] | |
(swap! loaded disj ns) | |
(dorun (map #(remove-method execute %) (@loadable ns)))) | |
(unload [plugins] (doseq [ns @loaded] (unload plugins ns))) | |
(update [_] (reset! loadable (load-file "plugins.clj"))) | |
(reload [plugins] (doto plugins update unload load))) | |
(defonce plugins (update (Plugins. (atom #{}) (atom #{})))) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment