Skip to content

Commit f12e442

Browse files
johnnyshieldsjamis
andauthored
MONGOID-5673 [Monkey Patch Removal] Remove Object#do_or_do_not and Object#you_must (#5713)
* Remove do_or_do_not and you_must * Remove more do_or_do_not * reduce indent * Remove test * deprecate instead of remove --------- Co-authored-by: Jamis Buck <[email protected]>
1 parent fb1d280 commit f12e442

File tree

9 files changed

+43
-77
lines changed

9 files changed

+43
-77
lines changed

lib/mongoid/association/bindable.rb

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def remove_associated_in_to(doc, inverse)
120120
# @param [ Object ] id The id of the bound document.
121121
def bind_foreign_key(keyed, id)
122122
unless keyed.frozen?
123-
keyed.you_must(_association.foreign_key_setter, id)
123+
try_method(keyed, _association.foreign_key_setter, id)
124124
end
125125
end
126126

@@ -135,8 +135,8 @@ def bind_foreign_key(keyed, id)
135135
# @param [ Document ] typed The document that stores the type field.
136136
# @param [ String ] name The name of the model.
137137
def bind_polymorphic_type(typed, name)
138-
if _association.type
139-
typed.you_must(_association.type_setter, name)
138+
if _association.type && !typed.frozen?
139+
try_method(typed, _association.type_setter, name)
140140
end
141141
end
142142

@@ -151,8 +151,8 @@ def bind_polymorphic_type(typed, name)
151151
# @param [ Document ] typed The document that stores the type field.
152152
# @param [ String ] name The name of the model.
153153
def bind_polymorphic_inverse_type(typed, name)
154-
if _association.inverse_type
155-
typed.you_must(_association.inverse_type_setter, name)
154+
if _association.inverse_type && !typed.frozen?
155+
try_method(typed, _association.inverse_type_setter, name)
156156
end
157157
end
158158

@@ -167,8 +167,8 @@ def bind_polymorphic_inverse_type(typed, name)
167167
# @param [ Document ] doc The base document.
168168
# @param [ Document ] inverse The inverse document.
169169
def bind_inverse(doc, inverse)
170-
if doc.respond_to?(_association.inverse_setter)
171-
doc.you_must(_association.inverse_setter, inverse)
170+
if doc.respond_to?(_association.inverse_setter) && !doc.frozen?
171+
try_method(doc, _association.inverse_setter, inverse)
172172
end
173173
end
174174

@@ -223,6 +223,24 @@ def unbind_from_relational_parent(doc)
223223
bind_polymorphic_type(doc, nil)
224224
bind_inverse(doc, nil)
225225
end
226+
227+
# Convenience method to perform +#try+ but return
228+
# nil if the method argument is nil.
229+
#
230+
# @example Call method if it exists.
231+
# object.try_method(:use, "The Force")
232+
#
233+
# @example Return nil if method argument is nil.
234+
# object.try_method(nil, "The Force") #=> nil
235+
#
236+
# @param [ String | Symbol ] method_name The method name.
237+
# @param [ Object... ] *args The arguments.
238+
#
239+
# @return [ Object | nil ] The result of the try or nil if the
240+
# method does not exist.
241+
def try_method(object, method_name, *args)
242+
object.try(method_name, *args) if method_name
243+
end
226244
end
227245
end
228246
end

lib/mongoid/association/embedded/embedded_in/binding.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ def bind_one
2525
_base._association = _association.inverse_association(_target) unless _base._association
2626
_base.parentize(_target)
2727
if _base.embedded_many?
28-
_target.do_or_do_not(_association.inverse(_target)).push(_base)
28+
_target.send(_association.inverse(_target)).push(_base)
2929
else
3030
remove_associated(_target)
31-
_target.do_or_do_not(_association.inverse_setter(_target), _base)
31+
try_method(_target, _association.inverse_setter(_target), _base)
3232
end
3333
end
3434
end
@@ -42,9 +42,9 @@ def bind_one
4242
def unbind_one
4343
binding do
4444
if _base.embedded_many?
45-
_target.do_or_do_not(_association.inverse(_target)).delete(_base)
45+
_target.send(_association.inverse(_target)).delete(_base)
4646
else
47-
_target.do_or_do_not(_association.inverse_setter(_target), nil)
47+
try_method(_target, _association.inverse_setter(_target), nil)
4848
end
4949
end
5050
end

lib/mongoid/association/embedded/embeds_many/binding.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def bind_one(doc)
2121
doc.parentize(_base)
2222
binding do
2323
remove_associated(doc)
24-
doc.do_or_do_not(_association.inverse_setter(_target), _base)
24+
try_method(doc, _association.inverse_setter(_target), _base)
2525
end
2626
end
2727

@@ -33,7 +33,7 @@ def bind_one(doc)
3333
# @param [ Document ] doc The single document to unbind.
3434
def unbind_one(doc)
3535
binding do
36-
doc.do_or_do_not(_association.inverse_setter(_target), nil)
36+
try_method(doc, _association.inverse_setter(_target), nil)
3737
end
3838
end
3939
end

lib/mongoid/association/embedded/embeds_one/binding.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Binding
2222
def bind_one
2323
_target.parentize(_base)
2424
binding do
25-
_target.do_or_do_not(_association.inverse_setter(_target), _base)
25+
try_method(_target, _association.inverse_setter(_target), _base)
2626
end
2727
end
2828

@@ -34,7 +34,7 @@ def bind_one
3434
# person.name = nil
3535
def unbind_one
3636
binding do
37-
_target.do_or_do_not(_association.inverse_setter(_target), nil)
37+
try_method(_target, _association.inverse_setter(_target), nil)
3838
end
3939
end
4040
end

lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ class Binding
1919
# @param [ Document ] doc The single document to bind.
2020
def bind_one(doc)
2121
binding do
22-
inverse_keys = doc.you_must(_association.inverse_foreign_key)
22+
inverse_keys = try_method(doc, _association.inverse_foreign_key) unless doc.frozen?
2323
if inverse_keys
2424
record_id = inverse_record_id(doc)
2525
unless inverse_keys.include?(record_id)
26-
doc.you_must(_association.inverse_foreign_key_setter, inverse_keys.push(record_id))
26+
try_method(doc, _association.inverse_foreign_key_setter, inverse_keys.push(record_id))
2727
end
2828
doc.reset_relation_criteria(_association.inverse)
2929
end
@@ -39,7 +39,7 @@ def bind_one(doc)
3939
def unbind_one(doc)
4040
binding do
4141
_base.send(_association.foreign_key).delete_one(record_id(doc))
42-
inverse_keys = doc.you_must(_association.inverse_foreign_key)
42+
inverse_keys = try_method(doc, _association.inverse_foreign_key) unless doc.frozen?
4343
if inverse_keys
4444
inverse_keys.delete_one(inverse_record_id(doc))
4545
doc.reset_relation_criteria(_association.inverse)

lib/mongoid/cacheable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module Cacheable
2727
# @return [ String ] the string with or without updated_at
2828
def cache_key
2929
return "#{model_key}/new" if new_record?
30-
return "#{model_key}/#{_id}-#{updated_at.utc.to_formatted_s(cache_timestamp_format)}" if do_or_do_not(:updated_at)
30+
return "#{model_key}/#{_id}-#{updated_at.utc.to_formatted_s(cache_timestamp_format)}" if try(:updated_at)
3131
"#{model_key}/#{_id}"
3232
end
3333
end

lib/mongoid/extensions/object.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def __to_inc__
8484
end
8585
Mongoid.deprecate(self, :__to_inc__)
8686

87+
8788
# Do or do not, there is no try. -- Yoda.
8889
#
8990
# @example Do or do not.
@@ -94,9 +95,11 @@ def __to_inc__
9495
#
9596
# @return [ Object | nil ] The result of the method call or nil if the
9697
# method does not exist.
98+
# @deprecated
9799
def do_or_do_not(name, *args)
98100
send(name, *args) if name && respond_to?(name)
99101
end
102+
Mongoid.deprecate(self, :do_or_do_not)
100103

101104
# Get the value for an instance variable or false if it doesn't exist.
102105
#
@@ -195,9 +198,11 @@ def substitutable
195198
#
196199
# @return [ Object | nil ] The result of the method call or nil if the
197200
# method does not exist. Nil if the object is frozen.
201+
# @deprecated
198202
def you_must(name, *args)
199203
frozen? ? nil : do_or_do_not(name, *args)
200204
end
205+
Mongoid.deprecate(self, :you_must)
201206

202207
module ClassMethods
203208
# Convert the provided object to a foreign key, given the metadata key

lib/mongoid/validatable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def read_attribute_for_validation(attr)
6868
begin_validate
6969
relation = without_autobuild { send(attr) }
7070
exit_validate
71-
relation.do_or_do_not(:in_memory) || relation
71+
relation.try(:in_memory) || relation
7272
elsif fields[attribute].try(:localized?)
7373
attributes[attribute]
7474
else

spec/mongoid/extensions/object_spec.rb

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -132,45 +132,6 @@
132132
end
133133
end
134134

135-
describe "#do_or_do_not" do
136-
137-
context "when the object is nil" do
138-
139-
let(:result) do
140-
nil.do_or_do_not(:not_a_method, "The force is strong with you")
141-
end
142-
143-
it "returns nil" do
144-
expect(result).to be_nil
145-
end
146-
end
147-
148-
context "when the object is not nil" do
149-
150-
context "when the object responds to the method" do
151-
152-
let(:result) do
153-
[ "Yoda", "Luke" ].do_or_do_not(:join, ",")
154-
end
155-
156-
it "returns the result of the method" do
157-
expect(result).to eq("Yoda,Luke")
158-
end
159-
end
160-
161-
context "when the object does not respond to the method" do
162-
163-
let(:result) do
164-
"Yoda".do_or_do_not(:use, "The Force", 1000)
165-
end
166-
167-
it "returns the result of the method" do
168-
expect(result).to be_nil
169-
end
170-
end
171-
end
172-
end
173-
174135
describe ".mongoize" do
175136

176137
let(:object) do
@@ -200,24 +161,6 @@
200161
end
201162
end
202163

203-
describe "#you_must" do
204-
205-
context "when the object is frozen" do
206-
207-
let(:person) do
208-
Person.new.tap { |peep| peep.freeze }
209-
end
210-
211-
let(:result) do
212-
person.you_must(:aliases=, [])
213-
end
214-
215-
it "returns nil" do
216-
expect(result).to be_nil
217-
end
218-
end
219-
end
220-
221164
describe "#remove_ivar" do
222165

223166
context "when the instance variable is defined" do

0 commit comments

Comments
 (0)