Skip to content

Commit

Permalink
Merge pull request #2132 from eregon/ruby2_keywords
Browse files Browse the repository at this point in the history
Use #ruby2_keywords for correct delegation on Ruby <= 2.6, 2.7 and 3
  • Loading branch information
dblock authored Nov 20, 2020
2 parents e86339a + 8cd284b commit d70a51e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
30 changes: 12 additions & 18 deletions lib/grape/middleware/stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -70,23 +61,26 @@ 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

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|
Expand Down
3 changes: 1 addition & 2 deletions spec/grape/middleware/stack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d70a51e

Please sign in to comment.