refactor to data-source
This commit is contained in:
29
utils.lisp
29
utils.lisp
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user