Skip to content

Instantly share code, notes, and snippets.

@MichaelDrogalis
Forked from XPherior/core.clj
Last active December 11, 2015 06:48
Show Gist options
  • Save MichaelDrogalis/4562107 to your computer and use it in GitHub Desktop.
Save MichaelDrogalis/4562107 to your computer and use it in GitHub Desktop.
(ns dire-examples.bad-core
(:require [dire.core :refer [with-precondition! with-postcondition!]]))
(defn save-user [& {:keys [username email password state]}]
(if (and (and (> (count username) 4) (< (count username) 11))
(and (> (count password) 6) (re-matches #".*\d.*" password))
(re-matches #"\w+@\w+\.\w+" "mjd3089@rit.edu")
(re-matches #"[A-Z]{2}" state))
(let [result :ok] ; Or :not-ok
"Persist the user to a database here."
(if (not= result :ok) (throw (Throwable. "Save unsuccessful")) :ok))
(throw (Throwable. "Something terrible has gone wrong"))))
(save-user :username "XPherior" :email "mjd3089@rit.edu" :password "mikemike4" :state "PA")
(ns dire-examples.core
(:require [dire.core :refer [with-precondition! with-postcondition!]]))
(defn save-user [& {:keys [username email password state]}]
"Persist the user to a database here."
:ok ;Or :not-ok
)
(with-precondition! #'save-user
:legal-username-length
(fn [& {:keys [username]}]
(let [length (count username)]
(and (> length 4) (< length 11)))))
(with-precondition! #'save-user
:well-formed-password
(fn [& {:keys [password]}]
(and (> (count password) 6) (re-matches #".*\d.*" password))))
(with-precondition! #'save-user
:well-formed-email
(fn [& {:keys [email]}]
(re-matches #"\w+@\w+\.\w+" "mjd3089@rit.edu")))
(with-precondition! #'save-user
:legal-state-abbreviation
(fn [& {:keys [state]}]
(re-matches #"[A-Z]{2}" state)))
(with-postcondition! #'save-user
:successful-save
(fn [result]
(= result :ok)))
(save-user :username "XPherior" :email "mjd3089@rit.edu" :password "mikemike4" :state "PA")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment