Skip to content

Instantly share code, notes, and snippets.

@swlkr
swlkr / .bash_profile
Created February 20, 2016 21:19
.bash_profile
#!/usr/bin/env bash
# Get the Git branch
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
# Custom bash prompt
#
# Includes custom character for the prompt, path, and Git branch name.
(defn split [sep s]
(clojure.string/split s sep))
(defn lines [sep contents]
(->> contents
(split #"\n")
(map (partial split sep))))
(defn maps [sep contents]
(let [lines (lines sep contents)
cols (first lines)
rows (rest lines)]
(map (partial zipmap cols) rows)))
(defn read-csv [filepath]
(let [contents (slurp filepath)]
(maps #"," contents)))
@swlkr
swlkr / test-side-effects.clj
Last active February 4, 2017 22:04
create-record-test
(ns proj.records-test
(:require [proj.records :refer :all]
[clojure.test :refer :all]))
(deftest create-record-test
(with-redefs [db/insert-record<! (fn [arg] (assoc arg :id 1))]
(testing "with valid record data"
(let [arg {:content "hello world"}]
(is (= {:id 1 :content "hello world"} (create! arg)))))))
@swlkr
swlkr / create.clj
Last active February 4, 2017 21:44
create
(ns proj.records
(:require [yesql.core :refer [defqueries]]))
(defqueries "records.sql")
(defn create! [body]
(-> body
validate
db/insert-record<!))
@swlkr
swlkr / init.sh
Last active February 20, 2017 01:23
A Majestic Monolith in Clojure
#!/bin/bash
lein new majestic monolith
cd monolith
git init
echo .idea >> .gitignore
git commit -am "Initial commit" # good practice to commit early and often
idea . # or use your favorite editor here
(ns monolith.core
(:require [monolith.server :as server]
[monolith.env :as env]
[monolith.utils :as utils])
(:gen-class))
(def start server/start)
(def stop server/stop)
(def restart server/restart)
(ns monolith.server
(:require [org.httpkit.server :as http]
[clojure.tools.namespace.repl :as tn]
[ring.middleware.json :refer [wrap-json-body wrap-json-response]]
[ring.middleware.defaults :refer [wrap-defaults api-defaults]]
[monolith.env :as env]
[monolith.routes :refer [routes]]
[monolith.http :refer [response]]
[monolith.utils :refer [throw+]]
[monolith.logic.tokens :as tokens]))