Modernize method missing implementations

`...` is both simpler an more correct since the keyword argument
separation.
This commit is contained in:
Jean Boussier 2024-01-16 13:02:12 +01:00
parent 6a9795d4d2
commit 946e46ebcc
29 changed files with 59 additions and 63 deletions

@ -668,7 +668,7 @@ def respond_to?(string, include_all = false)
true true
end end
def method_missing(*args) def method_missing(...)
nil nil
end end
end end

@ -306,7 +306,7 @@ def any(*args, &block)
end end
alias :all :any alias :all :any
def method_missing(name, *args, &block) def method_missing(name, *, &block)
@variants[name] = block if block_given? @variants[name] = block if block_given?
end end

@ -326,7 +326,7 @@ def all?; false; end
def to_ary; end def to_ary; end
def to_a; end def to_a; end
def method_missing(method, *args) def method_missing(method, ...)
if method.end_with?("?") if method.end_with?("?")
method[0..-2].downcase.to_sym == to_sym method[0..-2].downcase.to_sym == to_sym
else else
@ -373,7 +373,7 @@ def respond_to_missing?(method, _)
method.end_with?("?") method.end_with?("?")
end end
def method_missing(method, *args) def method_missing(method, ...)
false if method.end_with?("?") false if method.end_with?("?")
end end
end end

@ -178,9 +178,9 @@ def app_host
end end
end end
def method_missing(name, *args, &block) def method_missing(name, ...)
if url_helpers.respond_to?(name) if url_helpers.respond_to?(name)
url_helpers.public_send(name, *args, &block) url_helpers.public_send(name, ...)
else else
super super
end end

@ -151,8 +151,8 @@ def test_child_session_assertions_bubble_up_to_root
# try to delegate these methods to the session object. # try to delegate these methods to the session object.
def test_does_not_prevent_method_missing_passing_up_to_ancestors def test_does_not_prevent_method_missing_passing_up_to_ancestors
mixin = Module.new do mixin = Module.new do
def method_missing(name, *args) def method_missing(name, ...)
name.to_s == "foo" ? "pass" : super name == :foo ? "pass" : super
end end
end end
@test.class.superclass.include(mixin) @test.class.superclass.include(mixin)

@ -3895,9 +3895,9 @@ def url_for(options = {})
@app.routes.url_helpers.url_for(options) @app.routes.url_helpers.url_for(options)
end end
def method_missing(method, *args, &block) def method_missing(method, ...)
if method.to_s.match?(/_(path|url)$/) if method.match?(/_(path|url)$/)
@app.routes.url_helpers.send(method, *args, &block) @app.routes.url_helpers.send(method, ...)
else else
super super
end end

@ -334,12 +334,12 @@ def respond_to_missing?(*args)
true true
end end
def method_missing(called, *args, **options, &block) def method_missing(called, ...)
name = called.to_s.dasherize name = called.name.dasherize
TagHelper.ensure_valid_html5_tag_name(name) TagHelper.ensure_valid_html5_tag_name(name)
tag_string(name, *args, **options, &block) tag_string(name, ...)
end end
end end

@ -279,8 +279,8 @@ def test_uncountable_route_key
end end
private private
def method_missing(method, *args) def method_missing(method, ...)
ActiveModel::Naming.public_send(method, *args) ActiveModel::Naming.public_send(method, ...)
end end
end end

@ -18,7 +18,7 @@ def attributes
instance_values.except("address", "friends") instance_values.except("address", "friends")
end end
def method_missing(method_name, *args) def method_missing(method_name, ...)
if method_name == :bar if method_name == :bar
"i_am_bar" "i_am_bar"
else else

@ -16,7 +16,7 @@ class NullPool # :nodoc:
include ConnectionAdapters::AbstractPool include ConnectionAdapters::AbstractPool
class NullConfig # :nodoc: class NullConfig # :nodoc:
def method_missing(*) def method_missing(...)
nil nil
end end
end end

@ -12,12 +12,12 @@ def respond_to_missing?(name, _)
end end
end end
def method_missing(name, *arguments, &block) def method_missing(name, ...)
match = Method.match(self, name) match = Method.match(self, name)
if match && match.valid? if match && match.valid?
match.define match.define
send(name, *arguments, &block) send(name, ...)
else else
super super
end end

@ -272,16 +272,16 @@ def load_instances?
use_instantiated_fixtures != :no_instances use_instantiated_fixtures != :no_instances
end end
def method_missing(name, *args, **kwargs, &block) def method_missing(method, ...)
if fs_name = fixture_sets[name.to_s] if fs_name = fixture_sets[method.name]
access_fixture(fs_name, *args, **kwargs, &block) access_fixture(fs_name, ...)
else else
super super
end end
end end
def respond_to_missing?(name, include_private = false) def respond_to_missing?(method, include_private = false)
if include_private && fixture_sets.key?(name.to_s) if include_private && fixture_sets.key?(method.name)
true true
else else
super super

@ -309,8 +309,8 @@ def is_a?(klass)
model.is_a?(klass) model.is_a?(klass)
end end
def method_missing(method, *args, &block) def method_missing(...)
model.send(method, *args, &block) model.send(...)
end end
end end

@ -39,7 +39,7 @@ def respond_to_missing?(name, include_private = false)
name.end_with?("?") || super name.end_with?("?") || super
end end
def method_missing(name, *args) def method_missing(name, ...)
if name.end_with?("?") if name.end_with?("?")
any?(name[0..-2]) any?(name[0..-2])
else else

@ -231,15 +231,15 @@ def dispatch(&block)
@broadcasts.each { |logger| block.call(logger) } @broadcasts.each { |logger| block.call(logger) }
end end
def method_missing(name, *args, **kwargs, &block) def method_missing(name, ...)
loggers = @broadcasts.select { |logger| logger.respond_to?(name) } loggers = @broadcasts.select { |logger| logger.respond_to?(name) }
if loggers.none? if loggers.none?
super(name, *args, **kwargs, &block) super
elsif loggers.one? elsif loggers.one?
loggers.first.send(name, *args, **kwargs, &block) loggers.first.send(name, ...)
else else
loggers.map { |logger| logger.send(name, *args, **kwargs, &block) } loggers.map { |logger| logger.send(name, ...) }
end end
end end

@ -183,9 +183,9 @@ def const_missing(name)
target.const_get(name) target.const_get(name)
end end
def method_missing(called, *args, &block) def method_missing(...)
@deprecator.warn(@message, caller_locations) @deprecator.warn(@message, caller_locations)
target.__send__(called, *args, &block) target.__send__(...)
end end
end end
end end

@ -506,8 +506,8 @@ def respond_to_missing?(method, _)
value.respond_to?(method) value.respond_to?(method)
end end
def method_missing(method, *args, &block) def method_missing(...)
value.public_send(method, *args, &block) value.public_send(...)
end end
def raise_type_error(other) def raise_type_error(other)

@ -59,8 +59,8 @@ def initialize(string)
end end
# Forward all undefined methods to the wrapped string. # Forward all undefined methods to the wrapped string.
def method_missing(method, *args, &block) def method_missing(method, ...)
result = @wrapped_string.__send__(method, *args, &block) result = @wrapped_string.__send__(method, ...)
if method.end_with?("!") if method.end_with?("!")
self if result self if result
else else

@ -31,8 +31,8 @@ def method_missing(method, *arguments, &block)
end end
end end
def respond_to_missing?(*arguments) def respond_to_missing?(...)
@context.respond_to?(*arguments) @context.respond_to?(...)
end end
end end
end end

@ -46,18 +46,14 @@ def dig(key, *identifiers)
super(key.to_sym, *identifiers) super(key.to_sym, *identifiers)
end end
def method_missing(name, *args) def method_missing(method, *args)
name_string = +name.to_s if method.end_with?("=")
if name_string.chomp!("=") self[method.name.chomp("=")] = args.first
self[name_string] = args.first elsif method.end_with?("!")
else name_string = method.name.chomp("!")
bangs = name_string.chomp!("!")
if bangs
self[name_string].presence || raise(KeyError.new(":#{name_string} is blank")) self[name_string].presence || raise(KeyError.new(":#{name_string} is blank"))
else else
self[name_string] self[method.name]
end
end end
end end

@ -24,7 +24,7 @@ def respond_to_missing?(method_name, include_private = false)
method_name.end_with?("?") || super method_name.end_with?("?") || super
end end
def method_missing(method_name, *arguments) def method_missing(method_name, ...)
if method_name.end_with?("?") if method_name.end_with?("?")
self == method_name[0..-2] self == method_name[0..-2]
else else

@ -235,7 +235,7 @@ def save
def no; false; end def no; false; end
def yes; true; end def yes; true; end
def method_missing(sym, *) def method_missing(sym, ...)
case sym case sym
when /^log_(.*)/ when /^log_(.*)/
@history << $1 @history << $1

@ -12,7 +12,7 @@ def to_ary
class Proxy class Proxy
def initialize(target) @target = target end def initialize(target) @target = target end
def method_missing(*a) @target.public_send(*a) end def method_missing(...) @target.public_send(...) end
end end
class DoubtfulToAry class DoubtfulToAry

@ -76,7 +76,7 @@ def type
end end
module ExtraMissing module ExtraMissing
def method_missing(sym, *args) def method_missing(sym, ...)
if sym == :extra_missing if sym == :extra_missing
42 42
else else

@ -585,7 +585,7 @@ def method_missing(method, *args)
end end
end end
def respond_to_missing?(symbol, *) def respond_to_missing?(symbol, _)
true true
end end
end end

@ -9,9 +9,9 @@ def to_s
"DummyConfig" "DummyConfig"
end end
def method_missing(selector, *args, &blk) def method_missing(selector, ...)
if @config.respond_to?(selector) if @config.respond_to?(selector)
@config.send(selector, *args, &blk) @config.send(selector, ...)
else else
self self
end end

@ -141,7 +141,7 @@ def apply_rubocop_autocorrect_after_generate!
end end
def method_missing(method, *args) def method_missing(method, *args)
method = method.to_s.delete_suffix("=").to_sym method = method.name.delete_suffix("=").to_sym
if args.empty? if args.empty?
if method == :rails if method == :rails

@ -15,8 +15,8 @@ def initialize(generator)
%w(template copy_file directory empty_directory inside %w(template copy_file directory empty_directory inside
empty_directory_with_keep_file create_file chmod shebang).each do |method| empty_directory_with_keep_file create_file chmod shebang).each do |method|
class_eval <<-RUBY, __FILE__, __LINE__ + 1 class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{method}(*args, &block) def #{method}(...)
@generator.send(:#{method}, *args, &block) @generator.send(:#{method}, ...)
end end
RUBY RUBY
end end

@ -27,8 +27,8 @@ def configure(&block)
end end
private private
def method_missing(*args, &block) def method_missing(...)
instance.send(*args, &block) instance.send(...)
end end
end end
end end