-
-
Save walterl/67a2690ebea01990b7c64432a0ea10df to your computer and use it in GitHub Desktop.
Integrant demo: swapping out components using Clojure's key hierarchy
This file contains 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
(ns demo | |
(:require [integrant.core :as ig])) | |
(def config | |
{::main {:widget (ig/ref ::base-widget)} | |
::prod-widget {:name "Production widget"}}) | |
(derive ::prod-widget ::base-widget) | |
(derive ::mock-widget ::base-widget) | |
(defmethod ig/init-key ::prod-widget | |
[_ config] | |
(str "Prod: " (:name config))) | |
(defmethod ig/init-key ::mock-widget | |
[_ config] | |
(str "Mock: " (:type config))) | |
(defmethod ig/init-key ::main | |
[_ {:keys [widget]}] | |
(println "Widget:" widget)) | |
(defn prod-system | |
[_] | |
(println "Production system:" (ig/init config))) | |
(defn test-system | |
[_] | |
(let [test-config (-> config | |
(dissoc ::prod-widget) | |
(assoc ::mock-widget {:type "my mock"}))] | |
(println "Test system with mock component:" (ig/init test-config)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment