Skip to content

Commit

Permalink
Use define-advice
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsius committed May 31, 2024
1 parent b4b3f24 commit c591e6b
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions closql.el
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,12 @@

;;;; Oref

(defun eieio-oref--closql-oref (fn obj slot)
(define-advice eieio-oref (:around (fn obj slot) closql-object)
"If OBJ is a `closql-object', delegate to `closql-oref'."
(if (closql--closql-object-p obj)
(closql-oref obj slot)
(funcall fn obj slot)))

(advice-add 'eieio-oref :around #'eieio-oref--closql-oref)

(defun closql--oref (obj slot)
(aref obj (eieio--slot-name-index (eieio--object-class obj) slot)))

Expand Down Expand Up @@ -131,13 +130,12 @@

;;;; Oset

(defun eieio-oset--closql-oset (fn obj slot value)
(define-advice eieio-oset (:around (fn obj slot value) closql-object)
"If OBJ is a `closql-object', delegate to `closql-oset'."
(if (closql--closql-object-p obj)
(closql-oset obj slot value)
(funcall fn obj slot value)))

(advice-add 'eieio-oset :around #'eieio-oset--closql-oset)

(defun closql--oset (obj slot value)
(aset obj (eieio--slot-name-index (eieio--object-class obj) slot) value))

Expand Down Expand Up @@ -249,8 +247,9 @@

(defconst closql--slot-properties '(:closql-class :closql-table))

(defun eieio-defclass-internal--set-closql-slot-props
(cname _superclasses slots _options)
(define-advice eieio-defclass-internal
(:after (cname _superclasses slots _options) closql-object)
"Handle additional slot properties used by `closql-object' derived classes."
(when-let* ((class (cl--find-class cname))
((child-of-class-p class 'closql-object)))
(pcase-dolist (`(,name . ,slot) slots)
Expand All @@ -262,18 +261,14 @@
((v (plist-get slot prop)))
(setf (alist-get prop (cl--slot-descriptor-props desc)) v)))))))

(advice-add 'eieio-defclass-internal :after
#'eieio-defclass-internal--set-closql-slot-props)

(defun eieio--slot-override--set-closql-slot-props (old new _)
(define-advice eieio--slot-override
(:after (old new _skipnil) closql-object)
"Handle additional slot properties used by `closql-object' derived classes."
(dolist (prop closql--slot-properties)
(when-let
((v (alist-get prop (cl--slot-descriptor-props new))))
(setf (alist-get prop (cl--slot-descriptor-props old)) v))))

(advice-add 'eieio--slot-override :after
#'eieio--slot-override--set-closql-slot-props)

;;; Database

(defclass closql-database (eieio-singleton)
Expand Down

0 comments on commit c591e6b

Please sign in to comment.