refactor to data-source

This commit is contained in:
2026-01-04 00:49:26 -05:00
parent e15628b20a
commit 712361da86
5 changed files with 55 additions and 58 deletions

View File

@@ -19,3 +19,32 @@
(defun string-to-hash-table-key (s)
(string-replace " " s ""))
(defclass data-source ()
((cache :initform nil :accessor cache)
(file-path :initarg :file-path :accessor file-path)))
(defmacro define-data-source (singular-name)
(let* ((class-name (symbolicate singular-name '-data-source))
(constructor-name (symbolicate 'make- singular-name '-data-source))
(plural-name (concat (symbol-name singular-name) "s"))
(file-name (concat (string-downcase plural-name) ".json")))
`(progn
(defclass ,class-name (data-source) ())
(defun ,constructor-name ()
(make-instance ',class-name :file-path ,file-name)))))
(defgeneric data (data-source))
(defmethod data ((ds data-source))
(or (cache ds)
(reload ds)))
(defgeneric reload (data-source))
(defmethod reload ((ds data-source))
(if (uiop:file-exists-p (file-path ds))
(let ((data (shasht:read-json (uiop:read-file-string (file-path ds)))))
(setf (cache ds) data)
data)))
(defgeneric refresh (data-source))