/
config.clj
66 lines (52 loc) · 1.93 KB
/
config.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(ns io.randomseed.bankster.config
^{:doc "Bankster library, configuration handling."
:author "Paweł Wilk"
:added "1.0.0"}
(:refer-clojure :exclude [load])
(:require [clojure.edn :as edn]
[io.randomseed.bankster.util :refer :all]
[io.randomseed.bankster.util.fs :as fs]
[io.randomseed.bankster])
(:import [io.randomseed.bankster Currency Registry]))
;;
;; Default configuration filename.
;;
(def ^String ^private ^const default-resource-path
"Default registry resource file – relative path."
"io/randomseed/bankster/config.edn")
(def ^String ^private ^const user-resource-path
"Additional registry resource file – relative path."
"META-INF/io/randomseed/bankster/currencies.edn")
;;
;; Config file reader.
;;
(defn ^clojure.lang.PersistentHashMap load
"Loads data structures from an EDN file. The given path should reside in one of the
resource directories. If it is not given the default-resource-path will be used."
{:added "1.0.0"}
([]
(load default-resource-path))
([^String resource-path]
(when-some [^java.net.URL r (fs/paths->resource resource-path)]
(when-some [config (edn/read-string (slurp r))]
(when (and (map? config) (pos? (count config)))
config)))))
;;
;; Getters.
;;
(defn currencies
"Returns currencies map of the given configuration map."
{:tag clojure.lang.PersistentHashMap :added "1.0.0"}
([cfg] (get cfg :currencies {})))
(defn countries
"Returns countries map of the given configuration map."
{:tag clojure.lang.PersistentHashMap :added "1.0.0"}
([cfg] (get cfg :countries {})))
(defn localized
"Returns localized properties map of the given configuration map."
{:tag clojure.lang.PersistentHashMap :added "1.0.0"}
([cfg] (get cfg :localized {})))
(defn version
"Returns version string of the given configuration map."
{:tag String :added "1.0.0"}
([cfg] (get cfg :version)))