summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarius Peter <dev@marius-peter.com>2025-11-02 19:40:29 +0100
committerMarius Peter <dev@marius-peter.com>2025-11-02 19:40:29 +0100
commit1b23eec4517a8628a9564c9ed25923e6c9ea5bd8 (patch)
treef527ed4ad667fc0ac005c8300dd151d17d06facd
parentb310f6e261e623237f0379c954df040cf4e57a4c (diff)
Add custom display functions for certain model entities.
-rw-r--r--models/fertilizer-product.rkt17
-rw-r--r--models/nutrient-measurement.rkt13
-rw-r--r--models/nutrient-target.rkt13
-rw-r--r--models/nutrient.rkt8
4 files changed, 47 insertions, 4 deletions
diff --git a/models/fertilizer-product.rkt b/models/fertilizer-product.rkt
index bd6bba9..0a6c03d 100644
--- a/models/fertilizer-product.rkt
+++ b/models/fertilizer-product.rkt
@@ -30,7 +30,22 @@
"nutrient.rkt")
;; Instances of this struct are persisted in the fertilizer_products table.
-(struct fertilizer-product (id canonical-name nutrient-values brand-name) #:transparent)
+(struct fertilizer-product (id canonical-name nutrient-values brand-name)
+ #:transparent
+ #:property prop:custom-write
+ (λ (v out _mode)
+ (fprintf out "Fertilizer #~a\n" (fertilizer-product-id v))
+ (if (fertilizer-product-brand-name v)
+ (fprintf out "~a (~a)\n"
+ (fertilizer-product-canonical-name v)
+ (fertilizer-product-brand-name v))
+ (fprintf out "~a\n"
+ (fertilizer-product-canonical-name v)))
+ (for ([nv (in-list (fertilizer-product-nutrient-values v))])
+ (match-define (cons n v) nv)
+ (fprintf out "~a ~a\n"
+ (~a (nutrient-name n) #:min-width 14)
+ (~a v #:max-width 6 #:align 'right)))))
;; CREATE
diff --git a/models/nutrient-measurement.rkt b/models/nutrient-measurement.rkt
index d3631de..e73a10e 100644
--- a/models/nutrient-measurement.rkt
+++ b/models/nutrient-measurement.rkt
@@ -31,7 +31,18 @@
"nutrient.rkt")
;; Instances of this struct are persisted in the nutrient_measurements table.
-(struct nutrient-measurement (id measured-on nutrient-values) #:transparent)
+(struct nutrient-measurement (id measured-on nutrient-values)
+ #:transparent
+ #:property prop:custom-write
+ (λ (v out _)
+ (fprintf out "Measurement #~a on ~a\n"
+ (nutrient-measurement-id v)
+ (nutrient-measurement-measured-on v))
+ (for ([nv (nutrient-measurement-nutrient-values v)])
+ (match-define (cons n v) nv)
+ (fprintf out "~a ~a\n"
+ (~a (nutrient-name n) #:min-width 14)
+ (~a v #:max-width 6 #:align 'right)))))
;; CREATE
diff --git a/models/nutrient-target.rkt b/models/nutrient-target.rkt
index 8611a40..5a6d673 100644
--- a/models/nutrient-target.rkt
+++ b/models/nutrient-target.rkt
@@ -30,7 +30,18 @@
"nutrient.rkt")
;; Instances of this struct are persisted in the nutrient_targets table.
-(struct nutrient-target (id effective-on nutrient-values) #:transparent)
+(struct nutrient-target (id effective-on nutrient-values)
+ #:transparent
+ #:property prop:custom-write
+ (λ (v out _)
+ (fprintf out "Target #~a on ~a\n"
+ (nutrient-target-id v)
+ (nutrient-target-effective-on v))
+ (for ([nv (nutrient-target-nutrient-values v)])
+ (match-define (cons n v) nv)
+ (fprintf out "~a ~a\n"
+ (~a (nutrient-name n) #:min-width 14)
+ (~a v #:max-width 6 #:align 'right)))))
;; CREATE
diff --git a/models/nutrient.rkt b/models/nutrient.rkt
index 6df0989..10e274e 100644
--- a/models/nutrient.rkt
+++ b/models/nutrient.rkt
@@ -25,7 +25,13 @@
sql
"../db/conn.rkt")
-(struct nutrient (id name formula) #:transparent)
+(struct nutrient (id name formula)
+ #:transparent
+ #:property prop:custom-write
+ (λ (v out _)
+ (fprintf out "#<~a ~a>"
+ (nutrient-id v)
+ (nutrient-name v))))
;; CREATE
Copyright 2019--2025 Marius PETER