Created
December 24, 2018 19:28
-
-
Save MageMasher/4c61445d81954c95bb1d7f8ab60fdf5a to your computer and use it in GitHub Desktop.
In Clojure 1.10 Objects can dynamically extend protocol methods at runtime. First file is cognitect.transcriptor output, second is the transcriptor file itself.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[cognitect.transcriptor :refer (check!)]) | |
(defprotocol CustomerDatabase | |
:extend-via-metadata true | |
(get-customer-by-id [db id]) | |
(update-customer-by-id [db id data]) | |
(delete-customer-by-id [db id])) | |
(defn dynamic-delete-customer-by-id | |
[db id] | |
(try | |
(delete-customer-by-id db id) | |
(catch Exception _ | |
(println "Boom!") | |
(when-not (get (meta db) `delete-customer-by-id) | |
(println "Installing delete-customer-by-id onto db metadata") | |
(alter-meta! db assoc `delete-customer-by-id (fn [db id] (swap! db dissoc id))) | |
(delete-customer-by-id db id))))) | |
(def mock-db | |
(atom {} | |
:meta | |
{`get-customer-by-id (fn [db id] (get @db id)) | |
`update-customer-by-id (fn [db id data] (swap! db assoc id data))})) | |
@mock-db | |
(check! empty?) | |
(update-customer-by-id mock-db 123 {:name "Joe"}) | |
(update-customer-by-id mock-db 456 {:name "Luke"}) | |
(get-customer-by-id mock-db 123) | |
(check! #{{:name "Joe"}}) | |
(get-customer-by-id mock-db 456) | |
(check! #{{:name "Luke"}}) | |
(println "Check out this mock db!") | |
@mock-db | |
(get (meta mock-db) `delete-customer-by-id) | |
(check! nil?) | |
(dynamic-delete-customer-by-id mock-db 123) | |
(get (meta mock-db) `delete-customer-by-id) | |
(check! ifn?) | |
@mock-db | |
(check! #{{456 {:name "Luke"}}}) | |
(get-customer-by-id mock-db 123) | |
(check! nil?) | |
(get-customer-by-id mock-db 456) | |
(check! #{{:name "Luke"}}) | |
(dynamic-delete-customer-by-id mock-db 456) | |
(check! empty?) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(comment {:transcript "./extend.repl", :namespace cognitect.transcriptor.t_56}) | |
(require '[cognitect.transcriptor :refer (check!)]) | |
;;=> nil | |
(defprotocol | |
CustomerDatabase | |
:extend-via-metadata | |
true | |
(get-customer-by-id [db id]) | |
(update-customer-by-id [db id data]) | |
(delete-customer-by-id [db id])) | |
;;=> CustomerDatabase | |
(defn | |
dynamic-delete-customer-by-id | |
[db id] | |
(try | |
(delete-customer-by-id db id) | |
(catch | |
Exception | |
_ | |
(println "Boom!") | |
(when-not | |
(get (meta db) 'cognitect.transcriptor.t_56/delete-customer-by-id) | |
(println "Installing delete-customer-by-id onto db metadata") | |
(alter-meta! | |
db | |
assoc | |
'cognitect.transcriptor.t_56/delete-customer-by-id | |
(fn [db id] (swap! db dissoc id))) | |
(delete-customer-by-id db id))))) | |
;;=> #'cognitect.transcriptor.t_56/dynamic-delete-customer-by-id | |
(def | |
mock-db | |
(atom | |
{} | |
:meta | |
{'cognitect.transcriptor.t_56/get-customer-by-id | |
(fn [db id] (get @db id)), | |
'cognitect.transcriptor.t_56/update-customer-by-id | |
(fn [db id data] (swap! db assoc id data))})) | |
;;=> #'cognitect.transcriptor.t_56/mock-db | |
@mock-db | |
;;=> {} | |
(check! empty?) | |
;;=> nil | |
(update-customer-by-id mock-db 123 {:name "Joe"}) | |
;;=> {123 {:name "Joe"}} | |
(update-customer-by-id mock-db 456 {:name "Luke"}) | |
;;=> {123 {:name "Joe"}, 456 {:name "Luke"}} | |
(get-customer-by-id mock-db 123) | |
;;=> {:name "Joe"} | |
(check! #{{:name "Joe"}}) | |
;;=> nil | |
(get-customer-by-id mock-db 456) | |
;;=> {:name "Luke"} | |
(check! #{{:name "Luke"}}) | |
;;=> nil | |
(println "Check out this mock db!") | |
Check out this mock db! | |
;;=> nil | |
@mock-db | |
;;=> {123 {:name "Joe"}, 456 {:name "Luke"}} | |
(get (meta mock-db) 'cognitect.transcriptor.t_56/delete-customer-by-id) | |
;;=> nil | |
(check! nil?) | |
;;=> nil | |
(dynamic-delete-customer-by-id mock-db 123) | |
;;Boom! | |
;;Installing delete-customer-by-id onto db metadata | |
;;=> {456 {:name "Luke"}} | |
(get (meta mock-db) 'cognitect.transcriptor.t_56/delete-customer-by-id) | |
;;=> #object[cognitect.transcriptor.t_56$dynamic_delete_customer_by_id$fn__6578 0x10bf2726 "cognitect.transcriptor.t_56$dynamic_delete_customer_by_id$fn__6578@10bf2726"] | |
(check! ifn?) | |
;;=> nil | |
@mock-db | |
;;=> {456 {:name "Luke"}} | |
(check! #{{456 {:name "Luke"}}}) | |
;;=> nil | |
(get-customer-by-id mock-db 123) | |
;;=> nil | |
(check! nil?) | |
;;=> nil | |
(get-customer-by-id mock-db 456) | |
;;=> {:name "Luke"} | |
(check! #{{:name "Luke"}}) | |
;;=> nil | |
(dynamic-delete-customer-by-id mock-db 456) | |
;;=> {} | |
(check! empty?) | |
;;=> nil | |
nil nil) | |
;;user=> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment