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 "### " %))] (let [producer (or (:category-producer i18n) #(str "### " %))]
(producer (get i18n category)))) (producer (get i18n category))))
(defn produce-item [item nights i18n] (defn produce-item [item nights i18n overrides]
(let [quantity (if (= (type (:quantity item)) java.lang.Long) (:quantity item) ((:quantity item) nights)) (let [quantity (if (number? (:quantity item)) (:quantity item) ((:quantity item) nights))
quantity-long (long (Math/ceil quantity)) 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)) name (or (get i18n (:key item)) (:name item))
producer (or (:item-producer i18n) #(str "- [ ] " %1 (if (> %2 1) (str " x" %2) "")))] 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 (->> items
(map #(produce-item % nights i18n)) (map #(produce-item % nights i18n overrides))
(clojure.string/join "\n") (clojure.string/join "\n")
)) ))
(defn produce-category [category items nights i18n] (defn produce-category [category items nights i18n overrides]
(if (empty? items) (if (empty? items)
"" ""
(str (str
(produce-category-name category i18n) (produce-category-name category i18n)
"\n" "\n"
(produce-items items nights i18n)))) (produce-items items nights i18n overrides))))
(defn validate-profile [store profile] (defn validate-profile [store profile]
(let [all-items (->> store (let [all-items (->> store
vals vals
flatten flatten
(map :key)) (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)] missing-items-str (clojure.string/join ", " missing-items)]
(when (not (empty? 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)))))) (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] (defn produce [store profile nights i18n]
(do (let [sanitized-profile (into #{} profile)
(validate-profile store profile) overrides (eval (concat '(merge-with into) (filter map? sanitized-profile)))]
(->> store (do
keys (validate-profile store sanitized-profile)
(map (fn [category] [category (filter #(.contains profile (:key %)) (get store category))])) (->> store
(filter #(not (empty? (second %)))) keys
(map #(produce-category (first %) (second %) nights i18n)) (map (fn [category] [category (filter #(.contains sanitized-profile (:key %)) (get store category))]))
(clojure.string/join "\n\n")))) (filter #(not (empty? (second %))))
(map #(produce-category (first %) (second %) nights i18n overrides))
(clojure.string/join "\n\n")))))