hy/hy/contrib/meth.hy
Kodi Arfer 14fddbe6c3 Give require the same features as import (#1142)
Give `require` the same features as `import`

You can now do (require foo), (require [foo [a b c]]), (require [foo [*]]), and (require [foo :as bar]). The first and last forms get you macros named foo.a, foo.b, etc. or bar.a, bar.b, etc., respectively. The second form only gets the macros in the list.

Implements #1118 and perhaps partly addresses #277.

N.B. The new meaning of (require foo) will cause all existing code that uses macros to break. Simply replace these forms with (require [foo [*]]) to get your code working again.

There's a bit of a hack involved in the forms (require foo) or (require [foo :as bar]). When you call (foo.a ...) or (bar.a ...), Hy doesn't actually look inside modules. Instead, these (require ...) forms give the macros names that have periods in them, which happens to work fine with the way Hy finds and interprets macro calls.

* Make `require` syntax stricter and add tests

* Update documentation for `require`

* Documentation wording improvements

* Allow :as in `require` name lists
2016-11-03 09:35:58 +02:00

32 lines
1001 B
Hy

;;; Hy on Meth
;;; based on paultag's meth library to access a Flask based application
(defmacro route-with-methods [name path methods params &rest code]
"Same as route but with an extra methods array to specify HTTP methods"
`(let [deco (apply app.route [~path]
{"methods" ~methods})]
(with-decorator deco
(defn ~name ~params
(do ~@code)))))
(defn rwm [name path method params code]
`(do (require hy.contrib.meth)
(hy.contrib.meth.route-with-methods ~name ~path ~method ~params ~@code)))
;; Some macro examples
(defmacro route [name path params &rest code]
"Get request"
(rwm name path ["GET"] params code))
(defmacro post-route [name path params &rest code]
"Post request"
(rwm name path ["POST"] params code))
(defmacro put-route [name path params &rest code]
"Put request"
(rwm name path ["PUT"] params code))
(defmacro delete-route [name path params &rest code]
"Delete request"
(rwm name path ["DELETE"] params code))