diff --git a/src/uxbox/util/mixins.clj b/src/uxbox/util/mixins.clj new file mode 100644 index 000000000..662469b7c --- /dev/null +++ b/src/uxbox/util/mixins.clj @@ -0,0 +1,40 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) 2016 Andrey Antukh + +(ns uxbox.util.mixins + (:require [rum.core :as rum] + [sablono.compiler :as s])) + +(defn- parse-defc + [args] + (loop [r {} + s 0 + v (first args) + n (rest args)] + (case s + 0 (if (symbol? v) + (recur (assoc r :name v) (inc s) (first n) (rest n)) + (throw (ex-info "Invalid" {}))) + 1 (if (string? v) + (recur (assoc r :doc v) (inc s) (first n) (rest n)) + (recur r (inc s) v n)) + 2 (if (map? v) + (if-let [mixins (:mixins v)] + (let [spec (dissoc v :mixins) + mixins (if (empty? spec) + mixins + (conj mixins spec))] + (recur (assoc r :mixins mixins) (inc s) (first n) (rest n))) + (recur (assoc r :mixins [v]) (inc s) (first n) (rest n))) + (recur r (inc s) v n)) + 3 (let [sym (:name r) + func `(fn ~sym ~v ~(s/compile-html `(do ~@n)))] + [func (:doc r) (:mixins r) sym])))) + +(defmacro defc + [& args] + (let [[render doc mixins cname] (parse-defc args)] + `(def ~cname ~doc (rum/build-defc ~render ~mixins ~(str cname)))))