Skip to content

Commit

Permalink
MONGOID-5673 [Monkey Patch Removal] Remove Object#do_or_do_not and Ob…
Browse files Browse the repository at this point in the history
…ject#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]>
  • Loading branch information
johnnyshields and jamis authored Nov 8, 2023
1 parent fb1d280 commit f12e442
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 77 deletions.
32 changes: 25 additions & 7 deletions lib/mongoid/association/bindable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def remove_associated_in_to(doc, inverse)
# @param [ Object ] id The id of the bound document.
def bind_foreign_key(keyed, id)
unless keyed.frozen?
keyed.you_must(_association.foreign_key_setter, id)
try_method(keyed, _association.foreign_key_setter, id)
end
end

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

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

Expand All @@ -167,8 +167,8 @@ def bind_polymorphic_inverse_type(typed, name)
# @param [ Document ] doc The base document.
# @param [ Document ] inverse The inverse document.
def bind_inverse(doc, inverse)
if doc.respond_to?(_association.inverse_setter)
doc.you_must(_association.inverse_setter, inverse)
if doc.respond_to?(_association.inverse_setter) && !doc.frozen?
try_method(doc, _association.inverse_setter, inverse)
end
end

Expand Down Expand Up @@ -223,6 +223,24 @@ def unbind_from_relational_parent(doc)
bind_polymorphic_type(doc, nil)
bind_inverse(doc, nil)
end

# Convenience method to perform +#try+ but return
# nil if the method argument is nil.
#
# @example Call method if it exists.
# object.try_method(:use, "The Force")
#
# @example Return nil if method argument is nil.
# object.try_method(nil, "The Force") #=> nil
#
# @param [ String | Symbol ] method_name The method name.
# @param [ Object... ] *args The arguments.
#
# @return [ Object | nil ] The result of the try or nil if the
# method does not exist.
def try_method(object, method_name, *args)
object.try(method_name, *args) if method_name
end
end
end
end
8 changes: 4 additions & 4 deletions lib/mongoid/association/embedded/embedded_in/binding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def bind_one
_base._association = _association.inverse_association(_target) unless _base._association
_base.parentize(_target)
if _base.embedded_many?
_target.do_or_do_not(_association.inverse(_target)).push(_base)
_target.send(_association.inverse(_target)).push(_base)
else
remove_associated(_target)
_target.do_or_do_not(_association.inverse_setter(_target), _base)
try_method(_target, _association.inverse_setter(_target), _base)
end
end
end
Expand All @@ -42,9 +42,9 @@ def bind_one
def unbind_one
binding do
if _base.embedded_many?
_target.do_or_do_not(_association.inverse(_target)).delete(_base)
_target.send(_association.inverse(_target)).delete(_base)
else
_target.do_or_do_not(_association.inverse_setter(_target), nil)
try_method(_target, _association.inverse_setter(_target), nil)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/association/embedded/embeds_many/binding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def bind_one(doc)
doc.parentize(_base)
binding do
remove_associated(doc)
doc.do_or_do_not(_association.inverse_setter(_target), _base)
try_method(doc, _association.inverse_setter(_target), _base)
end
end

Expand All @@ -33,7 +33,7 @@ def bind_one(doc)
# @param [ Document ] doc The single document to unbind.
def unbind_one(doc)
binding do
doc.do_or_do_not(_association.inverse_setter(_target), nil)
try_method(doc, _association.inverse_setter(_target), nil)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/association/embedded/embeds_one/binding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Binding
def bind_one
_target.parentize(_base)
binding do
_target.do_or_do_not(_association.inverse_setter(_target), _base)
try_method(_target, _association.inverse_setter(_target), _base)
end
end

Expand All @@ -34,7 +34,7 @@ def bind_one
# person.name = nil
def unbind_one
binding do
_target.do_or_do_not(_association.inverse_setter(_target), nil)
try_method(_target, _association.inverse_setter(_target), nil)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class Binding
# @param [ Document ] doc The single document to bind.
def bind_one(doc)
binding do
inverse_keys = doc.you_must(_association.inverse_foreign_key)
inverse_keys = try_method(doc, _association.inverse_foreign_key) unless doc.frozen?
if inverse_keys
record_id = inverse_record_id(doc)
unless inverse_keys.include?(record_id)
doc.you_must(_association.inverse_foreign_key_setter, inverse_keys.push(record_id))
try_method(doc, _association.inverse_foreign_key_setter, inverse_keys.push(record_id))
end
doc.reset_relation_criteria(_association.inverse)
end
Expand All @@ -39,7 +39,7 @@ def bind_one(doc)
def unbind_one(doc)
binding do
_base.send(_association.foreign_key).delete_one(record_id(doc))
inverse_keys = doc.you_must(_association.inverse_foreign_key)
inverse_keys = try_method(doc, _association.inverse_foreign_key) unless doc.frozen?
if inverse_keys
inverse_keys.delete_one(inverse_record_id(doc))
doc.reset_relation_criteria(_association.inverse)
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/cacheable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Cacheable
# @return [ String ] the string with or without updated_at
def cache_key
return "#{model_key}/new" if new_record?
return "#{model_key}/#{_id}-#{updated_at.utc.to_formatted_s(cache_timestamp_format)}" if do_or_do_not(:updated_at)
return "#{model_key}/#{_id}-#{updated_at.utc.to_formatted_s(cache_timestamp_format)}" if try(:updated_at)
"#{model_key}/#{_id}"
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/mongoid/extensions/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def __to_inc__
end
Mongoid.deprecate(self, :__to_inc__)


# Do or do not, there is no try. -- Yoda.
#
# @example Do or do not.
Expand All @@ -94,9 +95,11 @@ def __to_inc__
#
# @return [ Object | nil ] The result of the method call or nil if the
# method does not exist.
# @deprecated
def do_or_do_not(name, *args)
send(name, *args) if name && respond_to?(name)
end
Mongoid.deprecate(self, :do_or_do_not)

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

module ClassMethods
# Convert the provided object to a foreign key, given the metadata key
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/validatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def read_attribute_for_validation(attr)
begin_validate
relation = without_autobuild { send(attr) }
exit_validate
relation.do_or_do_not(:in_memory) || relation
relation.try(:in_memory) || relation
elsif fields[attribute].try(:localized?)
attributes[attribute]
else
Expand Down
57 changes: 0 additions & 57 deletions spec/mongoid/extensions/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,45 +132,6 @@
end
end

describe "#do_or_do_not" do

context "when the object is nil" do

let(:result) do
nil.do_or_do_not(:not_a_method, "The force is strong with you")
end

it "returns nil" do
expect(result).to be_nil
end
end

context "when the object is not nil" do

context "when the object responds to the method" do

let(:result) do
[ "Yoda", "Luke" ].do_or_do_not(:join, ",")
end

it "returns the result of the method" do
expect(result).to eq("Yoda,Luke")
end
end

context "when the object does not respond to the method" do

let(:result) do
"Yoda".do_or_do_not(:use, "The Force", 1000)
end

it "returns the result of the method" do
expect(result).to be_nil
end
end
end
end

describe ".mongoize" do

let(:object) do
Expand Down Expand Up @@ -200,24 +161,6 @@
end
end

describe "#you_must" do

context "when the object is frozen" do

let(:person) do
Person.new.tap { |peep| peep.freeze }
end

let(:result) do
person.you_must(:aliases=, [])
end

it "returns nil" do
expect(result).to be_nil
end
end
end

describe "#remove_ivar" do

context "when the instance variable is defined" do
Expand Down

0 comments on commit f12e442

Please sign in to comment.