Skip to content

Instantly share code, notes, and snippets.

@tolitius
Last active October 17, 2015 03:12
Show Gist options
  • Save tolitius/abd83eaf48a5a77974e1 to your computer and use it in GitHub Desktop.
Save tolitius/abd83eaf48a5a77974e1 to your computer and use it in GitHub Desktop.
top level "Component" functions
(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))))
(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}))
(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))
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