Skip to content

Commit 64b94c6

Browse files
MONGOID-5302 Replace Hash#update_values with ActiveSupport's transform_values! (#5234)
* Hash#update_values can be replaced with ActiveSupport#transform_values! * MONGOID-5302 add docs removing the methods * MONGOID-5302 fix docs Co-authored-by: shields <[email protected]> Co-authored-by: Neil Shweky <[email protected]>
1 parent 2384b48 commit 64b94c6

File tree

9 files changed

+14
-69
lines changed

9 files changed

+14
-69
lines changed

docs/release-notes/mongoid-8.0.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,10 @@ to have been used outside of Mongoid:
336336
- ``Mongoid::Extensions::Date::EPOCH``
337337
- ``Mongoid::Extensions::Time::EPOCH``
338338
- ``Mongoid::Factory::TYPE``
339+
340+
341+
Removed ``Array#update_values`` and ``Hash#update_values`` methods
342+
------------------------------------------------------------------
343+
344+
The previously deprecated ``Array#update_values`` and ``Hash#update_values``
345+
methods have been removed in Mongoid 8.

lib/mongoid/contextual/map_reduce.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def js_mode
121121
# @return [ MapReduce ] The map/reduce object.
122122
def out(location)
123123
normalized = location.dup
124-
normalized.update_values do |value|
124+
normalized.transform_values! do |value|
125125
value.is_a?(::Symbol) ? value.to_s : value
126126
end
127127
@map_reduce = @map_reduce.out(normalized)

lib/mongoid/criteria/queryable/extensions/array.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,6 @@ def __sort_pair__
108108
{ first => last.to_direction }
109109
end
110110

111-
# Update all the values in the hash with the provided block.
112-
#
113-
# @example Update the values in place.
114-
# [ 1, 2, 3 ].update_values(&:to_s)
115-
#
116-
# @param [ Proc ] block The block to execute on each value.
117-
#
118-
# @return [ Array ] the array.
119-
def update_values(&block)
120-
replace(map(&block))
121-
end
122-
123111
private
124112

125113
# Converts the array to a multi-dimensional array.

lib/mongoid/criteria/queryable/extensions/hash.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,6 @@ def __expand_complex__
134134
replacement
135135
end
136136

137-
# Update all the values in the hash with the provided block.
138-
#
139-
# @example Update the values in place.
140-
# { field: "1" }.update_values(&:to_i)
141-
#
142-
# @param [ Proc ] block The block to execute on each value.
143-
#
144-
# @return [ Hash ] the hash.
145-
def update_values(&block)
146-
each_pair do |key, value|
147-
store(key, block[value])
148-
end
149-
end
150-
151137
private
152138

153139
# Apply the provided strategy for the hash with the given object.

lib/mongoid/criteria/queryable/selectable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ def expr_query(criterion)
852852
# @param [ Hash ] criterion The criterion.
853853
def typed_override(criterion, operator)
854854
if criterion
855-
criterion.update_values do |value|
855+
criterion.transform_values! do |value|
856856
yield(value)
857857
end
858858
end

lib/mongoid/extensions/hash.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Hash
1111
#
1212
# @return [ Hash ] The converted hash.
1313
def __evolve_object_id__
14-
update_values(&:__evolve_object_id__)
14+
transform_values!(&:__evolve_object_id__)
1515
end
1616

1717
# Mongoizes each value in the hash to an object id if it is convertable.
@@ -24,7 +24,7 @@ def __mongoize_object_id__
2424
if id = self['$oid']
2525
BSON::ObjectId.from_string(id)
2626
else
27-
update_values(&:__mongoize_object_id__)
27+
transform_values!(&:__mongoize_object_id__)
2828
end
2929
end
3030

@@ -220,7 +220,7 @@ module ClassMethods
220220
# @return [ Hash ] The object mongoized.
221221
def mongoize(object)
222222
return if object.nil?
223-
evolve(object.dup).update_values { |value| value.mongoize }
223+
evolve(object.dup).transform_values!(&:mongoize)
224224
end
225225

226226
# Can the size of this object change?

lib/mongoid/fields.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -693,10 +693,8 @@ def create_translations_setter(name, meth, field)
693693
generated_methods.module_eval do
694694
re_define_method("#{meth}_translations=") do |value|
695695
attribute_will_change!(name)
696-
if value
697-
value.update_values do |_value|
698-
field.type.mongoize(_value)
699-
end
696+
value&.transform_values! do |_value|
697+
field.type.mongoize(_value)
700698
end
701699
attributes[name] = value
702700
end

spec/mongoid/criteria/queryable/extensions/array_spec.rb

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -503,23 +503,4 @@
503503
end
504504
end
505505
end
506-
507-
describe "#update_values" do
508-
509-
let(:array) do
510-
[ 1, 2, 3 ]
511-
end
512-
513-
let(:updated) do
514-
array.update_values(&:to_s)
515-
end
516-
517-
it "replaces each of the values with the result of the block" do
518-
expect(updated).to eq([ "1", "2", "3" ])
519-
end
520-
521-
it "returns the same instance of the array" do
522-
expect(updated).to equal(array)
523-
end
524-
end
525506
end

spec/mongoid/criteria/queryable/extensions/hash_spec.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,4 @@
311311
end
312312
end
313313
end
314-
315-
describe "#update_values" do
316-
317-
let(:hash) do
318-
{ field: "1" }
319-
end
320-
321-
before do
322-
hash.update_values(&:to_i)
323-
end
324-
325-
it "updates each value in the hash" do
326-
expect(hash).to eq({ field: 1 })
327-
end
328-
end
329314
end

0 commit comments

Comments
 (0)