Hi Jeremy, Just wanted to clarify, is this something you'll be adding to the Sequel codebase in the future?
On Tuesday, January 12, 2021 at 11:08:17 AM UTC-5 Michael Scrivo wrote: > That appears to work Jeremy, tests pass and no warnings are emitted. > > Thanks! > > On Tuesday, January 12, 2021 at 11:00:04 AM UTC-5 Jeremy Evans wrote: > >> On Mon, Jan 11, 2021 at 7:40 PM 'Michael Scrivo' via sequel-talk < >> [email protected]> wrote: >> >>> Hi, >>> >>> I'm wondering if it's possible to use a custom adder with a keyword arg >>> that doesn't throw a deprecation warning in Ruby 2.7+ >>> >>> For example, if you have an adder like this: >>> >>> adder: (lambda do |entity, some_key_word_arg: default| >>> # do something >>> end), >>> >>> You get a warning from the following line in add_associated_objects in >>> associations.rb: >>> return if !send(opts[:_add_method], o, *args) && >>> opts.handle_silent_modification_failure? >>> when it's called. >>> >>> Is there a way do this without the warning? >>> >> >> We can mark the created methods with ruby2_keywords . Can you try this >> and see if it works: >> >> diff --git a/lib/sequel/model/associations.rb >> b/lib/sequel/model/associations.rb >> index 9aa591b0d..18c57c862 100644 >> --- a/lib/sequel/model/associations.rb >> +++ b/lib/sequel/model/associations.rb >> @@ -1932,6 +1932,13 @@ module Sequel >> association_module(opts).send(:define_method, name, &block) >> association_module(opts).send(:alias_method, name, name) >> end >> + >> + def association_module_delegate_def(name, opts, &block) >> + mod = association_module(opts) >> + mod.send(:define_method, name, &block) >> + mod.send(:ruby2_keywords, name) if >> mod.respond_to?(:ruby2_keywords, true) >> + mod.send(:alias_method, name, name) >> + end >> >> # Add a private method to the module included in the class. >> def association_module_private_def(name, opts=OPTS, &block) >> @@ -1982,17 +1989,17 @@ module Sequel >> >> if adder = opts[:adder] >> association_module_private_def(opts[:_add_method], opts, >> &adder) >> - association_module_def(opts[:add_method], opts){|o,*args| >> add_associated_object(opts, o, *args)} >> + association_module_delegate_def(opts[:add_method], >> opts){|o,*args| add_associated_object(opts, o, *args)} >> end >> >> if remover = opts[:remover] >> association_module_private_def(opts[:_remove_method], opts, >> &remover) >> - association_module_def(opts[:remove_method], opts){|o,*args| >> remove_associated_object(opts, o, *args)} >> + association_module_delegate_def(opts[:remove_method], >> opts){|o,*args| remove_associated_object(opts, o, *args)} >> end >> >> if clearer = opts[:clearer] >> association_module_private_def(opts[:_remove_all_method], >> opts, &clearer) >> - association_module_def(opts[:remove_all_method], >> opts){|*args| remove_all_associated_objects(opts, *args)} >> + association_module_delegate_def(opts[:remove_all_method], >> opts){|*args| remove_all_associated_objects(opts, *args)} >> end >> end >> >> @@ -2424,6 +2431,7 @@ module Sequel >> run_association_callbacks(opts, :after_add, o) >> o >> end >> + ruby2_keywords(:add_associated_object) if >> respond_to?(:ruby2_keywords, true) >> >> # Add/Set the current object to/as the given object's reciprocal >> association. >> def add_reciprocal_object(opts, o) >> @@ -2566,6 +2574,7 @@ module Sequel >> associations[opts[:name]] = [] >> ret >> end >> + ruby2_keywords(:remove_all_associated_objects) if >> respond_to?(:ruby2_keywords, true) >> >> # Remove the given associated object from the given association >> def remove_associated_object(opts, o, *args) >> @@ -2587,6 +2596,7 @@ module Sequel >> run_association_callbacks(opts, :after_remove, o) >> o >> end >> + ruby2_keywords(:remove_associated_object) if >> respond_to?(:ruby2_keywords, true) >> >> # Check that the object from the associated table specified by >> the primary key >> # is currently associated to the receiver. If it is associated, >> return the object, otherwise >> >> Thanks, >> Jeremy >> >> >> -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/sequel-talk/ca9edde7-3206-4d47-b7c7-8980fd3d4b19n%40googlegroups.com.
