diff --git a/src/packer/core.clj b/src/packer/core.clj index 1d561b8..2b5d7b5 100644 --- a/src/packer/core.clj +++ b/src/packer/core.clj @@ -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")))) \ No newline at end of file + (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"))))) \ No newline at end of file