Skip to content

Commit

Permalink
Merge pull request #1670 from duduribeiro/fix_positional_arguments_in…
Browse files Browse the repository at this point in the history
…_ruby_3

Fix usage of inherited Sinatra::Base classes keyword arguments
  • Loading branch information
namusyaka committed Jan 7, 2021
2 parents 8ed1f0b + 77e6872 commit d39b135
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/sinatra/base.rb
Expand Up @@ -916,7 +916,7 @@ class Base
attr_accessor :app, :env, :request, :response, :params
attr_reader :template_cache

def initialize(app = nil)
def initialize(app = nil, **kwargs)
super()
@app = app
@template_cache = Tilt::Cache.new
Expand Down Expand Up @@ -1522,8 +1522,8 @@ def prototype
# Create a new instance of the class fronted by its middleware
# pipeline. The object is guaranteed to respond to #call but may not be
# an instance of the class new was called on.
def new(*args, &bk)
instance = new!(*args, &bk)
def new(*args, **kwargs, &bk)
instance = new!(*args, **kwargs, &bk)
Wrapper.new(build(instance).to_app, instance)
end

Expand Down
18 changes: 18 additions & 0 deletions test/base_test.rb
Expand Up @@ -6,6 +6,14 @@ class TestApp < Sinatra::Base
get('/') { 'Hello World' }
end

class TestKeywordArgumentInitializerApp < Sinatra::Base
def initialize(argument:)
@argument = argument
end

get('/') { "Hello World with Keyword Arguments: #{@argument}" }
end

it 'include Rack::Utils' do
assert TestApp.included_modules.include?(Rack::Utils)
end
Expand Down Expand Up @@ -48,6 +56,16 @@ class TestApp < Sinatra::Base
TestApp.configure { context = self }
assert_equal self, context
end

it "allows constructor to receive keyword arguments" do
app = TestKeywordArgumentInitializerApp.new(argument: "some argument")
request = Rack::MockRequest.new(app)

response = request.get('/')

assert response.ok?
assert_equal 'Hello World with Keyword Arguments: some argument', response.body
end
end

describe "Sinatra::Base#new" do
Expand Down

0 comments on commit d39b135

Please sign in to comment.