Skip to content

Commit

Permalink
using utilize library for more utility stuff!
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBaranosky committed Nov 9, 2011
1 parent 20387ca commit 5918656
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 51 deletions.
2 changes: 1 addition & 1 deletion project.clj
Expand Up @@ -2,7 +2,7 @@
:description "Periodic email reminders"
:dependencies [[org.clojure/clojure "1.2.0"]
[joda-time "2.0"]
[utilize "0.1.0"]
[utilize "0.1.2"]
[slingshot "0.4.0"]
[org.apache.commons/commons-email "1.2"]
[fs "0.9.0"]]
Expand Down
2 changes: 1 addition & 1 deletion src/fact/reminder_parsing_facts.clj
Expand Up @@ -4,7 +4,7 @@
day-of-month-identifier-regex every-x-days-regex every-x-weeks-regex
ordinal-regex month+day-regex day-of-week-regex date-regex
to-string due?)]
[utility :only (re-match-seq re-captures)]
[utilize.regex :only (re-match-seq re-captures)]
[utilize.testutils :only (do-at)]
slingshot.core
midje.sweet)
Expand Down
26 changes: 2 additions & 24 deletions src/fact/utility_facts.clj
@@ -1,28 +1,6 @@
(ns fact.utility_facts
(:use [utility :only (re-match-seq re-captures lowercase-keyword
third fourth fifth config-file-name config valid-config? )]
midje.sweet)
(:import [org.joda.time DateMidnight]))

(fact "can get a seq of only the matches - not the captures"
(re-match-seq #"(\d+)" "abc 123 xyz 789 rad") => ["123" "789"])

(fact "gives the captures from a given regex"
(re-captures #"(\d) (\d) (\d)" "1 2 4") => ["1" "2" "4"]
(re-captures #"(\d)" "a b c") => [])

(fact "can generate lower-case keywords from a string"
(lowercase-keyword "BobCratchet") => :bobcratchet
(lowercase-keyword "Bob Cratchet") => :bob-cratchet)

(tabular
(fact "extra english ways of getting nth element of a seq"
(?nth [1 2 3 4 5 6]) => ?element)

?nth ?element
third 3
fourth 4
fifth 5)
(:use [utility :only (config-file-name config valid-config?)]
midje.sweet))

(fact "if config file cannot be opened returns nil"
(config) => nil
Expand Down
5 changes: 3 additions & 2 deletions src/reminder_parsing.clj
Expand Up @@ -2,9 +2,10 @@
(:use [date-time :only (first-not-in-past for-display)]
[date-time-streams :only (month+day-stream every-x-days-stream day-of-month-stream
day-of-week-stream today+all-future-dates day-nums)]
[utility :only (re-captures re-match-seq lowercase-keyword re-captures resource read-lines)]
[utility :only (resource read-lines)]
[utilize.seq :only (interleave++ only)]
[utilize.string :only (ordinal-to-int ordinalize)]
[utilize.regex :only (re-captures re-match-seq)]
[utilize.string :only (ordinal-to-int ordinalize lowercase-keyword)]
[clojure.string :only (join split)]
slingshot.core)
(:import [org.joda.time DateMidnight]))
Expand Down
33 changes: 10 additions & 23 deletions src/utility.clj
@@ -1,25 +1,23 @@
(ns utility
(:use [clojure.java.io :only (reader)])
(:require [clojure.string :as str])
(:import [java.io File IOException BufferedReader]))

(def re-captures (comp rest re-matches))

(defn re-match-seq [re s]
(map first (re-seq re s)))
(defn read-lines
"Like clojure.core/line-seq but opens f with reader. Automatically
closes the reader AFTER YOU CONSUME THE ENTIRE SEQUENCE."
[f]
(let [read-line (fn this [^BufferedReader rdr]
(lazy-seq
(if-let [line (.readLine rdr)]
(cons line (this rdr))
(.close rdr))))]
(read-line (reader f))))

(defn resource [file]
(str (File. (File. (System/getProperty "user.dir") "resources") file)))

(def fact-resource resource)

(defn lowercase-keyword [s]
(keyword (.toLowerCase (str/replace s " " "-"))))

(def third (comp first rest rest))
(def fourth (comp first rest rest rest))
(def fifth (comp first rest rest rest rest))

(defn config-file-name [] "config.cljdata")

(defn config []
Expand All @@ -32,14 +30,3 @@
(and (= 2 (count cfg))
(contains? cfg :gmail-address)
(contains? cfg :password))))

(defn read-lines
"Like clojure.core/line-seq but opens f with reader. Automatically
closes the reader AFTER YOU CONSUME THE ENTIRE SEQUENCE."
[f]
(let [read-line (fn this [^BufferedReader rdr]
(lazy-seq
(if-let [line (.readLine rdr)]
(cons line (this rdr))
(.close rdr))))]
(read-line (reader f))))

0 comments on commit 5918656

Please sign in to comment.