Skip to content

Commit 05939a0

Browse files
misdoroMikhail Doronin
andauthored
Allow passing class name as a string for rake task initializer (#782)
It may be possible that API classes are not yet loaded when the oapi rake tasks are initialized in Rakefile. Add a possibility to pass class name as a string to not mess with class loader configuration. Co-authored-by: Mikhail Doronin <[email protected]>
1 parent 2d471d0 commit 05939a0

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#### Features
44

55
* Your contribution here.
6+
* [#782](https://github.com/ruby-grape/grape-swagger/pull/782): Allow passing class name as string for rake task initializer - [@misdoro](https://github.com/misdoro).
7+
68

79
#### Fixes
810

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,13 +1486,19 @@ end
14861486

14871487
## Rake Tasks <a name="rake"></a>
14881488

1489-
Add these lines to your Rakefile, and initialize the Task class with your Api class – be sure your Api class is available.
1489+
Add these lines to your Rakefile, and initialize the Task class with your Api class.
14901490

14911491
```ruby
14921492
require 'grape-swagger/rake/oapi_tasks'
14931493
GrapeSwagger::Rake::OapiTasks.new(::Api::Base)
14941494
```
14951495

1496+
You may initialize with the class name as a string if the class is not yet loaded at the time Rakefile is parsed:
1497+
```ruby
1498+
require 'grape-swagger/rake/oapi_tasks'
1499+
GrapeSwagger::Rake::OapiTasks.new('::Api::Base')
1500+
```
1501+
14961502
#### OpenApi/Swagger Documentation
14971503

14981504
```

lib/grape-swagger/rake/oapi_tasks.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,25 @@ class OapiTasks < ::Rake::TaskLib
1010
include Rack::Test::Methods
1111

1212
attr_reader :oapi
13-
attr_reader :api_class
1413

1514
def initialize(api_class)
1615
super()
1716

18-
@api_class = api_class
17+
if api_class.is_a? String
18+
@api_class_name = api_class
19+
else
20+
@api_class = api_class
21+
end
22+
1923
define_tasks
2024
end
2125

2226
private
2327

28+
def api_class
29+
@api_class ||= @api_class_name.constantize
30+
end
31+
2432
def define_tasks
2533
namespace :oapi do
2634
fetch

spec/lib/oapi_tasks_spec.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
require 'spec_helper'
44

55
RSpec.describe GrapeSwagger::Rake::OapiTasks do
6-
let(:api) do
7-
item = Class.new(Grape::API) do
6+
module Api
7+
class Item < Grape::API
88
version 'v1', using: :path
99

1010
namespace :item do
@@ -16,14 +16,24 @@
1616
end
1717
end
1818

19-
Class.new(Grape::API) do
19+
class Base < Grape::API
2020
prefix :api
21-
mount item
21+
mount Api::Item
2222
add_swagger_documentation add_version: true
2323
end
2424
end
2525

26-
subject { described_class.new(api) }
26+
subject { described_class.new(Api::Base) }
27+
28+
describe '.new' do
29+
it 'accepts class name as a constant' do
30+
expect(described_class.new(::Api::Base).send(:api_class)).to eq(Api::Base)
31+
end
32+
33+
it 'accepts class name as a string' do
34+
expect(described_class.new('::Api::Base').send(:api_class)).to eq(Api::Base)
35+
end
36+
end
2737

2838
describe '#make_request' do
2939
describe 'complete documentation' do

0 commit comments

Comments
 (0)