[NEEDS FEEDBACK] MONGOID-5382 RawValue for mongoize/demongoize #5368
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
This PR is a work-in-progress, code doesn't match spec below yet
MONGOID-5408 introduced a new class
Mongoid::RawValue
which is used to represent uncastable values. MONGOID-5408 added support for the#evolve
(cast-to-query) method, however, the concept can be extended to#mongoize
and#demongoize
.The following is intended:
"use_raw_value_for_type_casting"
(name TBD). The legacy behavior of this flag is "false" and the new behavior will be "true".mongoize
anddemongoize
will now return Mongoid::RawValue in case the object cannot be casted.raw_value
object. This is done primarily fordemongoize
, so that you can easily work with RawValues.InvalidValue
(orInvalidAssignment
, name TBD) error ifmongoize
returns aMongoid::RawValue
. To get around this error You will still be able to assign a RawValue directly.To illustrate all this:
For reference, the current Mongoid 8.0 behavior is that both
#mongoize
and#demongoize
coerce uncastable values tonil
, which is dangerous. Previously, in Mongoid 7.x and earlier, some cases of assignment resulted in errors being raised, however the logic was inconsistent and unintentional (e.g. errors happened to be raised because of bugs, which had the side effect of preventing some cases uncastable assignment.)