Better naming for property keys

This commit is contained in:
Marcus Kammer 2024-10-04 12:01:47 +02:00
parent 3f9821b308
commit 9d10fb9748
Signed by: marcuskammer
GPG key ID: C374817BE285268F

View file

@ -56,6 +56,23 @@
(setf capitalize nil))
(t (write-char (char-downcase char) out))))))
(defun remove-postfix (postfix string)
"Remove the given postfix from the string if it exists."
(let* ((postfix-length (length postfix))
(string-length (length string))
(index (- string-length postfix-length)))
(if (and (>= string-length postfix-length)
(string= postfix (subseq string index)))
(subseq string 0 index)
string)))
(defun remove-as2-postfix (string)
"Remove the '-as2' prefix from the given string."
(remove-postfix "-as2" string))
(defun prepare-property-key (name)
(kebab-camel (remove-as2-postfix (string-downcase (symbol-name name)))))
(defclass property-container ()
((properties :initform (make-hash-table :test #'equal)
:accessor properties)))
@ -64,17 +81,9 @@
(setf (gethash "type" (properties obj))
(kebab-camel (string-capitalize (class-name (class-of obj))))))
(defgeneric as2-type (property-container &optional value))
(defmethod as2-type ((obj property-container) &optional value)
(if value
(prog1 obj
(setf (gethash "type" (properties obj)) value))
(gethash "type" (properties obj))))
(defmacro define-property-method (class-name name)
`(defmethod ,name ((obj ,class-name) &optional value)
(let ((key ,(kebab-camel (string-downcase (symbol-name name)))))
(let ((key ,(prepare-property-key name)))
(if value
(prog1 obj
(setf (gethash key (properties obj)) value))
@ -96,6 +105,7 @@
end-time
generator
icon
id
image
in-reply-to
location
@ -105,6 +115,7 @@
start-time
summary
tag
type-as2
updated
url
to
@ -116,6 +127,7 @@
(defclass link (property-container) ())
(define-property-methods link
id
href
rel
media-type