From 8cd284b6111f9b51ec5da1bde5f32e924dd9a074 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Thu, 19 Nov 2020 20:47:44 +0100 Subject: [PATCH] Use #ruby2_keywords for correct delegation on Ruby <= 2.6, 2.7 and 3 * See https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/#a-compatible-delegation --- CHANGELOG.md | 1 + lib/grape/middleware/stack.rb | 30 ++++++++++++----------------- spec/grape/middleware/stack_spec.rb | 3 +-- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1462d08d8e..7377d3d640 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Your contribution here. * [#2131](https://github.com/ruby-grape/grape/pull/2131): Fix Ruby 2.7 keyword deprecation warning in validators/coerce - [@K0H205](https://github.com/K0H205). +* [#2132](https://github.com/ruby-grape/grape/pull/2132): Use #ruby2_keywords for correct delegation on Ruby <= 2.6, 2.7 and 3 - [@eregon](https://github.com/eregon). ### 1.5.1 (2020/11/15) diff --git a/lib/grape/middleware/stack.rb b/lib/grape/middleware/stack.rb index a11869da8e..dab755fe6f 100644 --- a/lib/grape/middleware/stack.rb +++ b/lib/grape/middleware/stack.rb @@ -6,12 +6,11 @@ module Middleware # It allows to insert and insert after class Stack class Middleware - attr_reader :args, :opts, :block, :klass + attr_reader :args, :block, :klass - def initialize(klass, *args, **opts, &block) + def initialize(klass, *args, &block) @klass = klass - @args = args - @opts = opts + @args = args @block = block end @@ -32,16 +31,8 @@ def inspect klass.to_s end - if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7') - def use_in(builder) - block ? builder.use(klass, *args, **opts, &block) : builder.use(klass, *args, **opts) - end - else - def use_in(builder) - args = self.args - args += [opts] unless opts.empty? - block ? builder.use(klass, *args, &block) : builder.use(klass, *args) - end + def use_in(builder) + builder.use(@klass, *@args, &@block) end end @@ -70,11 +61,12 @@ def [](i) middlewares[i] end - def insert(index, *args, **kwargs, &block) + def insert(index, *args, &block) index = assert_index(index, :before) - middleware = self.class::Middleware.new(*args, **kwargs, &block) + middleware = self.class::Middleware.new(*args, &block) middlewares.insert(index, middleware) end + ruby2_keywords :insert if respond_to?(:ruby2_keywords, true) alias insert_before insert @@ -82,11 +74,13 @@ def insert_after(index, *args, &block) index = assert_index(index, :after) insert(index + 1, *args, &block) end + ruby2_keywords :insert_after if respond_to?(:ruby2_keywords, true) - def use(*args, **kwargs, &block) - middleware = self.class::Middleware.new(*args, **kwargs, &block) + def use(*args, &block) + middleware = self.class::Middleware.new(*args, &block) middlewares.push(middleware) end + ruby2_keywords :use if respond_to?(:ruby2_keywords, true) def merge_with(middleware_specs) middleware_specs.each do |operation, *args| diff --git a/spec/grape/middleware/stack_spec.rb b/spec/grape/middleware/stack_spec.rb index 64f9bf382f..b7ac1b1491 100644 --- a/spec/grape/middleware/stack_spec.rb +++ b/spec/grape/middleware/stack_spec.rb @@ -35,8 +35,7 @@ def initialize(&block) expect { subject.use StackSpec::BarMiddleware, false, my_arg: 42 } .to change { subject.size }.by(1) expect(subject.last).to eq(StackSpec::BarMiddleware) - expect(subject.last.args).to eq([false]) - expect(subject.last.opts).to eq(my_arg: 42) + expect(subject.last.args).to eq([false, { my_arg: 42 }]) end it 'pushes a middleware class with block arguments onto the stack' do