Last active
October 17, 2015 03:12
-
-
Save tolitius/abd83eaf48a5a77974e1 to your computer and use it in GitHub Desktop.
top level "Component" functions
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
(ns raffle | |
(:require [raffle.system :refer [find-by-id]] | |
[clojure.tools.logging :refer [info]])) | |
(defn raffle-it [] | |
(let [id (rand-int 42)] | |
(info "and the winner is... " | |
(find-by-id id)))) |
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
(ns raffle.database | |
(:require [com.stuartsierra.component :as component] | |
[clojure.tools.logging :refer [info]])) | |
(defrecord Database [conf] | |
component/Lifecycle | |
(start [this] | |
;; create db, connection, etc.. | |
(assoc this :find-by-id (fn [id] | |
(info "database: looking by id.. [" id "]") | |
"Jimi Hendrix"))) | |
(stop [this] | |
(dissoc this :db))) | |
(defn new-database [conf] | |
(map->Database {:conf conf})) |
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
(ns raffle.system | |
(:require [com.stuartsierra.component :as component] | |
[raffle.database :refer [new-database]])) | |
(defn raffle-system [] | |
(let [conf {:db {:foo 42 :bar 34}}] | |
(component/system-map | |
:database (new-database conf)))) | |
(def system nil) | |
;; defined in other, more related namespace | |
(defn find-by-id [id] | |
((-> system :database :find-by-id) id)) | |
(defn rock-n-roll! [] | |
(alter-var-root #'system (constantly (raffle-system))) | |
(alter-var-root #'system component/start)) |
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
user=> (use 'raffle 'raffle.system) | |
nil | |
user=> (rock-n-roll!) | |
#<SystemMap> | |
user=> (raffle-it) | |
21:21:38.290 [nREPL-worker-4] INFO raffle.database - database: looking by id.. [ 1 ] | |
21:21:38.290 [nREPL-worker-4] INFO raffle - and the winner is... Jimi Hendrix |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment