From 9d10fb97488fff846012bdd4e848082916026457 Mon Sep 17 00:00:00 2001 From: Marcus Kammer Date: Fri, 4 Oct 2024 12:01:47 +0200 Subject: [PATCH] Better naming for property keys --- src/as2.lisp | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/as2.lisp b/src/as2.lisp index 298c326..9f63ff3 100644 --- a/src/as2.lisp +++ b/src/as2.lisp @@ -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