Toucan 2 instances are a custom map type that does two things regular maps do not do:
They are associated with a particular model; [[toucan2.protocols/model]] can be used to get it. This is usually set when the instance comes out of that database.
They track their [[toucan2.protocols/original]] version when they come out of the application database. This can in
turn be used to calculate the [[toucan2.protocols/changes]] that have been made, which powers features
like toucan2.save/save!
.
Normally a Toucan instance is considered equal to a plain map with the same current (via [[toucan2.protocols/current]]) value. It is considered equal to other instances if they have the same current value and their model is the same.
Toucan 2 instances are a custom map type that does two things regular maps do not do: 1. They are associated with a particular model; [[toucan2.protocols/model]] can be used to get it. This is usually set when the instance comes out of that database. 2. They track their [[toucan2.protocols/original]] version when they come out of the application database. This can in turn be used to calculate the [[toucan2.protocols/changes]] that have been made, which powers features like [[toucan2.save/save!]]. Normally a Toucan instance is considered equal to a plain map with the same current (via [[toucan2.protocols/current]]) value. It is considered equal to other instances if they have the same current value and their model is the same.
For debugging purposes: whether to print the original version of an instance, in addition to the current version, when
printing an instance
.
For debugging purposes: whether to print the original version of an instance, in addition to the current version, when printing an [[instance]].
(instance)
(instance model)
(instance model m)
(instance model k v & more)
Create a new Toucan 2 instance. See the namespace docstring for toucan2.instance
for more information about what
a Toucan 2 instance is.
This function has several arities:
With no args, creates an empty instance with its model set to nil
With one arg, creates an empty instance of a model.
With two args, creates an instance of a model from an existing map. This is optimized: if the map is already an instance of the model, returns the map as-is.
With three or more args, creates an instance of a model with key-value args.
Create a new Toucan 2 instance. See the namespace docstring for [[toucan2.instance]] for more information about *what* a Toucan 2 instance is. This function has several arities: * With no args, creates an empty instance with its *model* set to `nil` * With one arg, creates an empty instance of a *model*. * With two args, creates an instance of a *model* from an existing map. This is optimized: if the map is already an instance of the model, returns the map as-is. * With three or more args, creates an instance of a *model* with key-value args.
(instance-of? model x)
True if x
is a Toucan2 instance, and its [[protocols/model]] isa?
model
.
(instance-of? ::bird (instance ::toucan {})) ; -> true
(instance-of? ::toucan (instance ::bird {})) ; -> false
True if `x` is a Toucan2 instance, and its [[protocols/model]] `isa?` `model`. ```clj (instance-of? ::bird (instance ::toucan {})) ; -> true (instance-of? ::toucan (instance ::bird {})) ; -> false ```
(instance? x)
True if x
is a Toucan2 instance, i.e. a toucan2.instance.Instance
or some other class that satisfies the correct
interfaces.
Toucan instances need to implement protocols/IModel
, protocols/IWithModel
, and protocols/IRecordChanges
.
True if `x` is a Toucan2 instance, i.e. a `toucan2.instance.Instance` or some other class that satisfies the correct interfaces. Toucan instances need to implement [[protocols/IModel]], [[protocols/IWithModel]], and [[protocols/IRecordChanges]].
(reset-original an-instance)
Return a copy of an-instance
with its original
value set to its current value, discarding the previous original
value. No-ops if an-instance
is not a Toucan 2 instance.
Return a copy of `an-instance` with its `original` value set to its current value, discarding the previous original value. No-ops if `an-instance` is not a Toucan 2 instance.
(update-current an-instance f & args)
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]].
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]].
(update-original an-instance f & args)
Applies f
directly to the underlying original
map of an-instance
. No-ops if an-instance
is not
an [[Instance]].
Applies `f` directly to the underlying `original` map of `an-instance`. No-ops if `an-instance` is not an [[Instance]].
(update-original-and-current an-instance f & args)
Like (apply f instance args)
, but affects both the original
map and current
map of an-instance
rather than
just the current map. Acts like regular (apply f instance args)
if instance
is not an Instance
.
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.
Like `(apply f instance args)`, but affects both the `original` map and `current` map of `an-instance` rather than just the current map. Acts like regular `(apply f instance args)` if `instance` is not an `Instance`. `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.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close