Public Vars

Back

get-method (clj)

(source)

function

(get-method multifn dispatch-val)
Given a multimethod and a dispatch value, returns the dispatch fn that would apply to that value, or nil if none apply and no default

Examples

emil0r/reverie
(ns reverie.render
  "Render protocol and Renderer implementation"
  (:require [clojure.core.match :refer [match]]
            [hiccup.compiler]
            [reverie.system :as sys]
            [reverie.util :as util]
            [taoensso.timbre :as log]))


(def get-method-fn nil)
(defmulti ^:private get-method-fn (fn [renderer method meta] (::type meta)))
(defmethod get-method-fn :page/routes [renderer method {:keys [route-name]}]
  (assert (util/namespaced-kw? route-name) "Route was not found for renderer when rendering app/raw-page/module")
  (let [override-methods (get-in renderer [:options :override :methods-or-routes route-name])
        methods (get-in renderer [:methods-or-routes route-name])]
    (assert (not (nil? methods)) (str "No methods found for route " (util/kw->str route-name)))
    (or (get override-methods method)
        (:any override-methods)
        (get methods method)
        (:any methods))))
(defmethod get-method-fn :page/no-routes [_ _ _]
  identity)
(defmethod get-method-fn :default [renderer method _]
  (let [override-methods (get-in renderer [:options :override :methods-or-routes])
        methods (get-in renderer [:methods-or-routes])]
    (or (get override-methods method)
        (:any override-methods)
        (get methods method)
        (:any methods))))

;; Renderer takes data that has already been computed
;; and runs it through one of it's
(defrecord Renderer [name options methods-or-routes]
  IRender
  (render [this data]
    ;; render the data that's come in
    ((get-render-fn (:render-fn options)) data))
  (render [this method data]
    ;; pick out the method to render with
    (let [[meta data] (get-data data)
          method-fn (get-method-fn this method meta)
          resp (if method-fn (method-fn data) data)
          render-fn (get-render-fn (:render-fn options))]
      ;; if a method does exist, fetch the render-fn and then render against
      ;; what we get back from running the method fn with the data