Skip to content

Commit 3ce1afc

Browse files
committed
Add Mongoid::RawValue wrapper
1 parent a189655 commit 3ce1afc

File tree

4 files changed

+66
-10
lines changed

4 files changed

+66
-10
lines changed

docs/release-notes/mongoid-8.1.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ See the section on :ref:`Localize :present Field Option <present-fields>` for
118118
more details on how these are used.
119119

120120

121+
Support for Passing Raw Values into Queries
122+
-------------------------------------------
123+
124+
When performing queries, it is now possible skip Mongoid's type coercion logic
125+
using the ``Mongoid::RawValue`` wrapper class. This can be useful when legacy
126+
data in the database is of a different type than the field definition.
127+
128+
.. code-block:: ruby
129+
130+
class Person
131+
include Mongoid::Document
132+
field :age, type: Integer
133+
end
134+
135+
# Query for the string "42", not the integer 42
136+
Person.where(age: Mongoid::RawValue("42"))
137+
138+
121139
Added ``:to`` and ``:from`` options to ``attribute_changed?``
122140
-------------------------------------------------------------
123141

lib/mongoid/criteria/queryable/selector.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,20 @@ def evolve_multi(specs)
150150
#
151151
# @return [ Object ] The serialized object.
152152
def evolve(serializer, value)
153-
case value
154-
when Hash
155-
evolve_hash(serializer, value)
156-
when Array
157-
evolve_array(serializer, value)
158-
when Range
159-
value.__evolve_range__(serializer: serializer)
160-
else
161-
(serializer || value.class).evolve(value)
162-
end
153+
_value = case value
154+
when Hash
155+
evolve_hash(serializer, value)
156+
when Array
157+
evolve_array(serializer, value)
158+
when Range
159+
value.__evolve_range__(serializer: serializer)
160+
when Mongoid::RawValue
161+
value
162+
else
163+
(serializer || value.class).evolve(value)
164+
end
165+
_value = _value.raw_value if _value.is_a?(Mongoid::RawValue)
166+
_value
163167
end
164168

165169
# Evolve a single key selection with array values.

lib/mongoid/extensions.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def transform_keys
4848
require "mongoid/extensions/object"
4949
require "mongoid/extensions/object_id"
5050
require "mongoid/extensions/range"
51+
require "mongoid/extensions/raw_value"
5152
require "mongoid/extensions/regexp"
5253
require "mongoid/extensions/set"
5354
require "mongoid/extensions/string"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
# Wrapper class used when a value cannot be casted in evolve method.
4+
module Mongoid
5+
def RawValue(*args)
6+
RawValue.new(*args)
7+
end
8+
9+
class RawValue
10+
11+
attr_reader :raw_value
12+
13+
def initialize(raw_value)
14+
@raw_value = raw_value
15+
end
16+
17+
# Delegate all missing methods to the raw value.
18+
#
19+
# @param [ String, Symbol ] method_name The name of the method.
20+
# @param [ Array ] args The arguments passed to the method.
21+
ruby2_keywords def method_missing(method_name, *args, &block)
22+
raw_value.send(method_name, *args, &block)
23+
end
24+
25+
# Delegate all missing methods to the raw value.
26+
#
27+
# @param [ String, Symbol ] method_name The name of the method.
28+
# @param [ true | false ] include_private Whether to check private methods.
29+
def respond_to_missing?(method_name, include_private = false)
30+
raw_value.respond_to?(method_name, include_private)
31+
end
32+
end
33+
end

0 commit comments

Comments
 (0)