Implement overriding item counts in profile

This commit is contained in:
2024-04-14 17:27:13 +02:00
parent dd63cee18f
commit 2018b9f7d1

View File

@@ -9,43 +9,47 @@
(let [producer (or (:category-producer i18n) #(str "### " %))]
(producer (get i18n category))))
(defn produce-item [item nights i18n]
(let [quantity (if (= (type (:quantity item)) java.lang.Long) (:quantity item) ((:quantity item) nights))
(defn produce-item [item nights i18n overrides]
(let [quantity (if (number? (:quantity item)) (:quantity item) ((:quantity item) nights))
quantity-long (long (Math/ceil quantity))
quantity-override (get (:count overrides) (:key item))
quantity-final (or (when quantity-override (if (number? quantity-override) (long (Math/ceil quantity-override)) (quantity-override quantity-long))) quantity-long)
name (or (get i18n (:key item)) (:name item))
producer (or (:item-producer i18n) #(str "- [ ] " %1 (if (> %2 1) (str " x" %2) "")))]
(producer name quantity-long)))
(producer name quantity-final)))
(defn produce-items [items nights i18n]
(defn produce-items [items nights i18n overrides]
(->> items
(map #(produce-item % nights i18n))
(map #(produce-item % nights i18n overrides))
(clojure.string/join "\n")
))
(defn produce-category [category items nights i18n]
(defn produce-category [category items nights i18n overrides]
(if (empty? items)
""
(str
(produce-category-name category i18n)
"\n"
(produce-items items nights i18n))))
(produce-items items nights i18n overrides))))
(defn validate-profile [store profile]
(let [all-items (->> store
vals
flatten
(map :key))
missing-items (remove (set all-items) profile)
missing-items (remove (set all-items) (filter keyword? profile))
missing-items-str (clojure.string/join ", " missing-items)]
(when (not (empty? missing-items))
(throw (Exception. (str "The following keys are found in the profile, but they are not defined in store: " missing-items-str))))))
(defn produce [store profile nights i18n]
(do
(validate-profile store profile)
(->> store
keys
(map (fn [category] [category (filter #(.contains profile (:key %)) (get store category))]))
(filter #(not (empty? (second %))))
(map #(produce-category (first %) (second %) nights i18n))
(clojure.string/join "\n\n"))))
(let [sanitized-profile (into #{} profile)
overrides (eval (concat '(merge-with into) (filter map? sanitized-profile)))]
(do
(validate-profile store sanitized-profile)
(->> store
keys
(map (fn [category] [category (filter #(.contains sanitized-profile (:key %)) (get store category))]))
(filter #(not (empty? (second %))))
(map #(produce-category (first %) (second %) nights i18n overrides))
(clojure.string/join "\n\n")))))