Skip to content

Commit 4e00bee

Browse files
committed
creates templates generator
1 parent c7fcedd commit 4e00bee

10 files changed

+53
-21
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99

1010
# rspec failure tracking
1111
.rspec_status
12+
*.gem

jsonapi-swagger-blocks-generator.gemspec

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
# frozen_string_literal: true
3+
24
lib = File.expand_path("../lib", __FILE__)
35
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
46
require "jsonapi/swagger/blocks/generator/version"
@@ -9,20 +11,11 @@ Gem::Specification.new do |spec|
911
spec.authors = ["Gleydson Tavares"]
1012
spec.email = ["[email protected]"]
1113

12-
spec.summary = %q{TODO: Write a short summary, because RubyGems requires one.}
13-
spec.description = %q{TODO: Write a longer description or delete this line.}
14-
spec.homepage = "TODO: Put your gem's website or public repo URL here."
14+
spec.summary = "creates swagger-blocks gem files using jsonapi specification"
15+
spec.description = "The swagger-bloks files are created based on the JSONAPI::Resource files. To use this gem, you need to have jsonapi-resources gem installed."
16+
spec.homepage = "https://github.com/g13ydson/jsonapi-swagger-blocks"
1517
spec.license = "MIT"
1618

17-
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18-
# to allow pushing to a single host or delete this section to allow pushing to any host.
19-
if spec.respond_to?(:metadata)
20-
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21-
else
22-
raise "RubyGems 2.0 or newer is required to protect against " \
23-
"public gem pushes."
24-
end
25-
2619
spec.files = `git ls-files -z`.split("\x0").reject do |f|
2720
f.match(%r{^(test|spec|features)/})
2821
end

lib/generators/jsonapi/USAGE

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Description:
2+
Creates swagger blocks files from resources
3+
Example:
4+
rails generate jsonapi:swagger_bloks user
5+
6+
This will create:
7+
controllers/docs/user.rb
8+
controllers/docs/users_controller.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
module Jsonapi
4+
module Generators
5+
class SwaggerBlocksGenerator < Rails::Generators::Base
6+
source_root File.expand_path("templates", __dir__)
7+
argument :model, type: :string
8+
9+
def generate_documentation
10+
begin
11+
Object.const_get("#{model.camelcase}Resource").is_a?(Class)
12+
generate_model_template
13+
generate_controller_template
14+
rescue Exception => e
15+
p e.message
16+
exit!
17+
end
18+
end
19+
20+
private
21+
def generate_model_template
22+
template "model_template.template", "app/controllers/docs/#{model.underscore}.rb"
23+
end
24+
25+
def generate_controller_template
26+
template "controller_template.template", "app/controllers/docs/#{model.underscore.pluralize}_controller.rb"
27+
end
28+
end
29+
end
30+
end

lib/generators/jsonapi/templates/controller_template.template

Whitespace-only changes.

lib/generators/jsonapi/templates/model_template.template

Whitespace-only changes.
+3-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
require "jsonapi/swagger/blocks/generator/version"
1+
# frozen_string_literal: true
22

3-
module Jsonapi
4-
module Swagger
5-
module Blocks
6-
module Generator
7-
# Your code goes here...
8-
end
9-
end
10-
end
3+
module Jsonapi::Swagger::Blocks
4+
# Your code goes here...
115
end

lib/jsonapi/swagger/blocks/generator/version.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module Jsonapi
24
module Swagger
35
module Blocks

spec/jsonapi/swagger/blocks/generator_spec.rb spec/jsonapi_swagger_blocks_generator_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
RSpec.describe Jsonapi::Swagger::Blocks::Generator do
24
it "has a version number" do
35
expect(Jsonapi::Swagger::Blocks::Generator::VERSION).not_to be nil

spec/spec_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require "bundler/setup"
24
require "jsonapi/swagger/blocks/generator"
35

0 commit comments

Comments
 (0)