branch: externals/triples commit 6f8f3376f1e4df17a3113f88b41864556a25d7da Author: Andrew Hyatt <ahy...@gmail.com> Commit: Andrew Hyatt <ahy...@gmail.com>
Add `triples-set-types'. This is an form useful for those who have many properties of different types to set all at once, replacing those types completely. --- triples-test.el | 11 +++++++++++ triples.el | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/triples-test.el b/triples-test.el index f7e1b8f456..85a747966e 100644 --- a/triples-test.el +++ b/triples-test.el @@ -128,6 +128,17 @@ easily debug into it.") (triples-delete-subject db "foo") (should-not (triples-get-subject db "foo")))) +(ert-deftest triples-set-types () + (triples-test-with-temp-db + (triples-add-schema db 'named + '(name :unique t) + 'alias) + (triples-add-schema db 'reachable 'phone) + (triples-set-type db "foo" 'named :name "Name" :alias '("alias1" "alias2")) + (triples-set-types db "foo" :named/name "New Name" :reachable/phone '("867-5309")) + (should (equal (triples-test-plist-sort '(:named/name "New Name" :reachable/phone ("867-5309"))) + (triples-test-plist-sort (triples-get-subject db "foo")))))) + (ert-deftest triples-single-element () (triples-test-with-temp-db (triples-add-schema db 'named 'name) diff --git a/triples.el b/triples.el index 149d13cd63..c825bf0000 100644 --- a/triples.el +++ b/triples.el @@ -168,6 +168,22 @@ PROPERTIES is a plist of properties, without TYPE prefixes." (triples-verify-schema-compliant db (cdr op)) (triples--add db op))) +(defun triples-set-types (db subject &rest combined-props) + "Set all data for types in COMBINED-PROPS in DB for SUBJECT. +COMBINED-PROPS is a plist which takes combined properties such as +:named/name and their values. All other data related to the types +given in the COMBINED-PROPS will be removed." + (let ((type-to-plist (make-hash-table))) + (triples--plist-mapc + (lambda (cp val) + (pcase-let ((`(,type . ,prop) (triples-combined-to-type-and-prop cp))) + (puthash (triples--decolon type) + (plist-put (gethash (triples--decolon type) type-to-plist) + (triples--encolon prop) val) type-to-plist))) + combined-props) + (cl-loop for k being the hash-keys of type-to-plist using (hash-values v) + do (apply #'triples-set-type db subject k v)))) + (defun triples--set-type-op (subject type properties) "Create operation to replace PROPERTIES for TYPE for SUBJECT. PROPERTIES is a plist of properties, without TYPE prefixes."