Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[instance] Provide non-vararg arity for update-original-and-current #181

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions src/toucan2/instance.clj
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@

Toucan instances need to implement [[protocols/IModel]], [[protocols/IWithModel]], and [[protocols/IRecordChanges]]."
[x]
(every? #(clojure.core/instance? % x)
[toucan2.protocols.IModel
toucan2.protocols.IWithModel
toucan2.protocols.IRecordChanges]))
(and (clojure.core/instance? toucan2.protocols.IModel x)
(clojure.core/instance? toucan2.protocols.IWithModel x)
(clojure.core/instance? toucan2.protocols.IRecordChanges x)))

(defn instance-of?
"True if `x` is a Toucan2 instance, and its [[protocols/model]] `isa?` `model`.
Expand Down Expand Up @@ -307,16 +306,22 @@
(defn update-original
"Applies `f` directly to the underlying `original` map of `an-instance`. No-ops if `an-instance` is not
an [[Instance]]."
[an-instance f & args]
(if (instance? an-instance)
(protocols/with-original an-instance (apply f (protocols/original an-instance) args))
an-instance))
([an-instance f]
(if (instance? an-instance)
(protocols/with-original an-instance (f (protocols/original an-instance)))

Check warning on line 311 in src/toucan2/instance.clj

View check run for this annotation

Codecov / codecov/patch

src/toucan2/instance.clj#L311

Added line #L311 was not covered by tests
an-instance))
([an-instance f & args]
(if (instance? an-instance)
(protocols/with-original an-instance (apply f (protocols/original an-instance) args))
an-instance)))

(defn update-current
"Applies `f` directly to the underlying `current` map of `an-instance`; useful if you need to operate on it directly.
Acts like regular `(apply f instance args)` if `an-instance` is not an [[Instance]]."
[an-instance f & args]
(protocols/with-current an-instance (apply f (protocols/current an-instance) args)))
([an-instance f]
(protocols/with-current an-instance (f (protocols/current an-instance))))
([an-instance f & args]
(protocols/with-current an-instance (apply f (protocols/current an-instance) args))))

(defn update-original-and-current
"Like `(apply f instance args)`, but affects both the `original` map and `current` map of `an-instance` rather than
Expand All @@ -325,9 +330,15 @@
`f` is applied directly to the underlying `original` and `current` maps of `instance` itself. `f` is only applied
once if `original` and `current` are currently the same object (i.e., the new `original` and `current` will also be
the same object). If `current` and `original` are not the same object, `f` is applied twice."
[an-instance f & args]
(if (identical? (protocols/original an-instance) (protocols/current an-instance))
(reset-original (apply update-current an-instance f args))
(as-> an-instance an-instance
(apply update-original an-instance f args)
(apply update-current an-instance f args))))
([an-instance f]
(if (identical? (protocols/original an-instance) (protocols/current an-instance))
(reset-original (update-current an-instance f))
(as-> an-instance an-instance
(update-original an-instance f)
(update-current an-instance f))))
([an-instance f & args]
(if (identical? (protocols/original an-instance) (protocols/current an-instance))
(reset-original (apply update-current an-instance f args))
(as-> an-instance an-instance
(apply update-original an-instance f args)
(apply update-current an-instance f args)))))
Loading