grape-starter 1.6.0 → 1.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/pipeline.yml +2 -0
- data/.rubocop.yml +2 -2
- data/lib/starter/build.rb +12 -16
- data/lib/starter/builder/activerecord.rb +105 -0
- data/lib/starter/builder/base_file.rb +4 -4
- data/lib/starter/builder/names.rb +32 -28
- data/lib/starter/builder/sequel.rb +106 -0
- data/lib/starter/orms.rb +2 -4
- data/lib/starter/templates/endpoints.rb +119 -0
- data/lib/starter/templates/files.rb +84 -0
- data/lib/starter/version.rb +1 -1
- data/lib/starter.rb +1 -6
- metadata +7 -7
- data/lib/starter/builder/templates/activerecord.rb +0 -107
- data/lib/starter/builder/templates/endpoints.rb +0 -121
- data/lib/starter/builder/templates/files.rb +0 -86
- data/lib/starter/builder/templates/sequel.rb +0 -108
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a0e8dbdcdf0bc8ec707834a2a55a2c36826c316d82a45f7f8c219e1ec4361f37
|
4
|
+
data.tar.gz: 21d235ca4a99f447965c8cf9a765bfaabca150f74a493ee332fb60aa137ce7fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c54cbaf4ce481b619e621c733a6b16bd1582761a297e4380de381773c837d3a0894a472f6e45bf451ad702fa5ca8fe1d0abcf8c067f31844d5eb75532cfec7cf
|
7
|
+
data.tar.gz: df826306d1964ed1f2a58f87353cfb03b6f86bb084b0cb70e14639b04748d18986e5d971be420efb12919b82a28042cf8e1147e0c938eb5a52c0ef4ed44694ae
|
data/.rubocop.yml
CHANGED
@@ -37,8 +37,8 @@ Metrics/MethodLength:
|
|
37
37
|
|
38
38
|
Naming/AccessorMethodName:
|
39
39
|
Exclude:
|
40
|
-
- 'lib/starter/
|
41
|
-
- 'lib/starter/
|
40
|
+
- 'lib/starter/templates/files.rb'
|
41
|
+
- 'lib/starter/templates/endpoints.rb'
|
42
42
|
|
43
43
|
Style/AsciiComments:
|
44
44
|
Enabled: false
|
data/lib/starter/build.rb
CHANGED
@@ -1,21 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Starter
|
4
|
-
# require 'starter/builder/orms'
|
5
|
-
# require 'starter/builder/names'
|
6
|
-
# require 'starter/builder/base_file'
|
7
|
-
# require 'starter/builder/file_foo'
|
8
|
-
# require 'starter/builder/templates/files'
|
9
|
-
# require 'starter/builder/templates/endpoints'
|
10
|
-
|
11
4
|
class Build
|
12
|
-
extend Builder::Names
|
13
5
|
extend Builder::BaseFile
|
14
|
-
extend
|
15
|
-
extend
|
6
|
+
extend Templates::Files
|
7
|
+
extend Templates::Endpoints
|
16
8
|
|
17
9
|
class << self
|
18
|
-
attr_reader :prefix, :resource, :
|
10
|
+
attr_reader :prefix, :resource, :entity,
|
11
|
+
:set, :force, :destination, :orm,
|
12
|
+
:naming
|
19
13
|
|
20
14
|
#
|
21
15
|
# public methods
|
@@ -34,6 +28,7 @@ module Starter
|
|
34
28
|
@resource = name
|
35
29
|
@destination = destination
|
36
30
|
@prefix = options[:p] # can be nil
|
31
|
+
@naming = Starter::Builder::Names.new(@resource)
|
37
32
|
|
38
33
|
FileUtils.copy_entry source, destination
|
39
34
|
|
@@ -69,8 +64,9 @@ module Starter
|
|
69
64
|
@force = options[:force]
|
70
65
|
@entity = options[:entity]
|
71
66
|
@orm = options[:orm]
|
67
|
+
@naming = Starter::Builder::Names.new(@resource)
|
72
68
|
|
73
|
-
Orms.add_migration(klass_name, resource.downcase) if @orm
|
69
|
+
Orms.add_migration(@naming.klass_name, resource.downcase) if @orm
|
74
70
|
save_resource
|
75
71
|
end
|
76
72
|
|
@@ -119,7 +115,7 @@ module Starter
|
|
119
115
|
#
|
120
116
|
# creates a new file in lib folder as namespace, includind the version
|
121
117
|
def add_namespace_with_version
|
122
|
-
new_lib = File.join(destination, 'lib', base_file_name)
|
118
|
+
new_lib = File.join(destination, 'lib', @naming.base_file_name)
|
123
119
|
FileOps.write_file(new_lib, base_namespace_file.strip_heredoc)
|
124
120
|
end
|
125
121
|
|
@@ -135,7 +131,7 @@ module Starter
|
|
135
131
|
#
|
136
132
|
# provides an array of endpoints for the new resource
|
137
133
|
def endpoint_set
|
138
|
-
crud_set = singular? ? singular_one : crud
|
134
|
+
crud_set = @naming.singular? ? singular_one : crud
|
139
135
|
return crud_set if set.blank?
|
140
136
|
|
141
137
|
crud_set.each_with_object([]) { |x, memo| set.map { |y| memo << x if x.to_s.start_with?(y) } }
|
@@ -145,7 +141,7 @@ module Starter
|
|
145
141
|
# saves all resource related files
|
146
142
|
def save_resource
|
147
143
|
created_files = file_list.each_with_object([]) do |new_file, memo|
|
148
|
-
memo << send("#{new_file}_name")
|
144
|
+
memo << @naming.send("#{new_file}_name")
|
149
145
|
save_file(new_file)
|
150
146
|
end
|
151
147
|
|
@@ -157,7 +153,7 @@ module Starter
|
|
157
153
|
#
|
158
154
|
# saves new resource files
|
159
155
|
def save_file(new_file)
|
160
|
-
new_file_name = send("#{new_file}_name")
|
156
|
+
new_file_name = @naming.send("#{new_file}_name")
|
161
157
|
should_raise?(new_file_name)
|
162
158
|
FileOps.write_file(new_file_name, send(new_file.strip_heredoc))
|
163
159
|
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
module Starter
|
6
|
+
module Builder
|
7
|
+
module ActiveRecord
|
8
|
+
def model_klass
|
9
|
+
'ActiveRecord::Base'
|
10
|
+
end
|
11
|
+
|
12
|
+
def initializer
|
13
|
+
<<-FILE.strip_heredoc
|
14
|
+
# frozen_string_literal: true
|
15
|
+
|
16
|
+
require 'yaml'
|
17
|
+
require 'erb'
|
18
|
+
require 'active_record'
|
19
|
+
|
20
|
+
config_content = File.read(File.join('config', 'database.yml'))
|
21
|
+
|
22
|
+
db_conf = YAML.safe_load(ERB.new(config_content).result)
|
23
|
+
env = ENV['RACK_ENV'] || 'development'
|
24
|
+
|
25
|
+
ActiveRecord::Base.establish_connection db_conf[env]
|
26
|
+
logger = if %w[development test].include? env
|
27
|
+
log_dir = File.join(Dir.getwd, 'log')
|
28
|
+
log_file = File.join(log_dir, 'db.log')
|
29
|
+
FileUtils.mkdir(log_dir) unless Dir.exist?(log_dir)
|
30
|
+
Logger.new(File.open(log_file, 'a'))
|
31
|
+
else
|
32
|
+
Logger.new($stdout)
|
33
|
+
end
|
34
|
+
|
35
|
+
ActiveRecord::Base.logger = logger
|
36
|
+
|
37
|
+
# Middleware
|
38
|
+
module ActiveRecord
|
39
|
+
module Rack
|
40
|
+
# ActiveRecord >= 5 removes the Pool management
|
41
|
+
class ConnectionManagement
|
42
|
+
def initialize(app)
|
43
|
+
@app = app
|
44
|
+
end
|
45
|
+
|
46
|
+
def call(env)
|
47
|
+
response = @app.call(env)
|
48
|
+
response[2] = ::Rack::BodyProxy.new(response[2]) do
|
49
|
+
ActiveRecord::Base.clear_active_connections!
|
50
|
+
end
|
51
|
+
|
52
|
+
return response
|
53
|
+
rescue StandardError
|
54
|
+
ActiveRecord::Base.clear_active_connections!
|
55
|
+
raise
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
FILE
|
61
|
+
end
|
62
|
+
|
63
|
+
def rakefile
|
64
|
+
<<-FILE.strip_heredoc
|
65
|
+
# ActiveRecord migration tasks
|
66
|
+
require 'active_record'
|
67
|
+
include ActiveRecord::Tasks
|
68
|
+
config_dir = File.expand_path('../config', __FILE__)
|
69
|
+
config_content = File.join(config_dir, 'database.yml')
|
70
|
+
DatabaseTasks.env = ENV['RACK_ENV'] || 'development'
|
71
|
+
DatabaseTasks.database_configuration = YAML.load_file(config_content)
|
72
|
+
DatabaseTasks.db_dir = 'db'
|
73
|
+
DatabaseTasks.migrations_paths = File.join('db', 'migrate')
|
74
|
+
|
75
|
+
ActiveRecord::Base.configurations = DatabaseTasks.database_configuration
|
76
|
+
ActiveRecord::Base.establish_connection DatabaseTasks.env.to_sym
|
77
|
+
|
78
|
+
load 'active_record/railties/databases.rake'
|
79
|
+
FILE
|
80
|
+
end
|
81
|
+
|
82
|
+
def gemfile
|
83
|
+
<<-FILE.strip_heredoc
|
84
|
+
# DB stuff
|
85
|
+
gem 'activerecord', '>= 6'
|
86
|
+
gem 'pg'
|
87
|
+
FILE
|
88
|
+
end
|
89
|
+
|
90
|
+
def migration(klass_name, resource)
|
91
|
+
version = "#{::ActiveRecord::VERSION::MAJOR}.#{::ActiveRecord::VERSION::MINOR}"
|
92
|
+
<<-FILE.strip_heredoc
|
93
|
+
class Create#{klass_name} < ActiveRecord::Migration[#{version}]
|
94
|
+
def change
|
95
|
+
create_table :#{resource} do |t|
|
96
|
+
|
97
|
+
t.timestamps
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
FILE
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -5,24 +5,24 @@ module Starter
|
|
5
5
|
module BaseFile
|
6
6
|
# add it in api base
|
7
7
|
def add_mount_point
|
8
|
-
FileOps.call!(api_base_file_name) { |content| add_to_base(content) }
|
8
|
+
FileOps.call!(@naming.api_base_file_name) { |content| add_to_base(content) }
|
9
9
|
end
|
10
10
|
|
11
11
|
# adding mount point to base class
|
12
12
|
def add_to_base(file)
|
13
13
|
occurence = file.scan(/(\s+mount\s.*?\n)/).last.first
|
14
|
-
replacement = occurence + mount_point
|
14
|
+
replacement = occurence + @naming.mount_point
|
15
15
|
file.sub!(occurence, replacement)
|
16
16
|
end
|
17
17
|
|
18
18
|
# removes in api base
|
19
19
|
def remove_mount_point
|
20
|
-
FileOps.call!(api_base_file_name) { |content| remove_from_base(content) }
|
20
|
+
FileOps.call!(@naming.api_base_file_name) { |content| remove_from_base(content) }
|
21
21
|
end
|
22
22
|
|
23
23
|
# removes mount point from base class
|
24
24
|
def remove_from_base(file)
|
25
|
-
file.sub!(mount_point, '')
|
25
|
+
file.sub!(@naming.mount_point, '')
|
26
26
|
end
|
27
27
|
|
28
28
|
# parses out the prefix from base api file
|
@@ -2,53 +2,41 @@
|
|
2
2
|
|
3
3
|
module Starter
|
4
4
|
module Builder
|
5
|
-
|
5
|
+
class Names
|
6
|
+
def initialize(resource)
|
7
|
+
@resource = resource
|
8
|
+
end
|
9
|
+
|
6
10
|
def klass_name
|
7
11
|
for_klass = prepare_klass
|
8
12
|
singular? ? for_klass.classify : for_klass.classify.pluralize
|
9
13
|
end
|
10
14
|
|
11
|
-
def lib_klass_name
|
12
|
-
return klass_name unless @orm
|
13
|
-
|
14
|
-
case Starter::Config.read[:orm]
|
15
|
-
when 'sequel'
|
16
|
-
require 'starter/builder/templates/sequel'
|
17
|
-
extend(Starter::Builder::Templates::Sequel)
|
18
|
-
"#{klass_name} < #{model_klass}"
|
19
|
-
when 'activerecord', 'ar'
|
20
|
-
require 'starter/builder/templates/activerecord'
|
21
|
-
extend(Starter::Builder::Templates::ActiveRecord)
|
22
|
-
"#{klass_name} < #{model_klass}"
|
23
|
-
else
|
24
|
-
klass_name
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
15
|
# rubocop:disable Style/StringConcatenation
|
29
16
|
def base_file_name
|
30
17
|
@resource.tr('/', '-').downcase + '.rb'
|
31
18
|
end
|
32
19
|
# rubocop:enable Style/StringConcatenation
|
33
20
|
|
21
|
+
def api_base_file_name
|
22
|
+
File.join(Dir.getwd, 'api', 'base.rb')
|
23
|
+
end
|
24
|
+
|
34
25
|
def base_spec_name
|
35
26
|
base_file_name.gsub(/.rb$/, '_spec.rb')
|
36
27
|
end
|
37
28
|
|
29
|
+
# entry in api/base.rb
|
38
30
|
def mount_point
|
39
31
|
" mount Endpoints::#{klass_name}\n"
|
40
32
|
end
|
41
33
|
|
42
|
-
|
43
|
-
File.join(Dir.getwd, 'api', 'base.rb')
|
44
|
-
end
|
45
|
-
|
46
|
-
# resource file
|
34
|
+
# endpoints file
|
47
35
|
def api_file_name
|
48
36
|
File.join(Dir.getwd, 'api', 'endpoints', base_file_name)
|
49
37
|
end
|
50
38
|
|
51
|
-
#
|
39
|
+
# entities file
|
52
40
|
def entity_file_name
|
53
41
|
File.join(Dir.getwd, 'api', 'entities', base_file_name)
|
54
42
|
end
|
@@ -58,6 +46,22 @@ module Starter
|
|
58
46
|
File.join(Dir.getwd, 'lib', 'models', base_file_name)
|
59
47
|
end
|
60
48
|
|
49
|
+
# model entry in lib file
|
50
|
+
def lib_klass_name
|
51
|
+
return klass_name unless @orm
|
52
|
+
|
53
|
+
case Starter::Config.read[:orm]
|
54
|
+
when 'sequel'
|
55
|
+
extend(Starter::Builder::Sequel)
|
56
|
+
"#{klass_name} < #{model_klass}"
|
57
|
+
when 'activerecord', 'ar'
|
58
|
+
extend(Starter::Builder::ActiveRecord)
|
59
|
+
"#{klass_name} < #{model_klass}"
|
60
|
+
else
|
61
|
+
klass_name
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
61
65
|
# resource spec
|
62
66
|
def api_spec_name
|
63
67
|
File.join(Dir.getwd, 'spec', 'requests', base_spec_name)
|
@@ -68,15 +72,15 @@ module Starter
|
|
68
72
|
File.join(Dir.getwd, 'spec', 'lib', 'models', base_spec_name)
|
69
73
|
end
|
70
74
|
|
75
|
+
def singular?
|
76
|
+
@resource.singularize.inspect == @resource.inspect
|
77
|
+
end
|
78
|
+
|
71
79
|
private
|
72
80
|
|
73
81
|
def prepare_klass
|
74
82
|
@resource.tr('-', '/')
|
75
83
|
end
|
76
|
-
|
77
|
-
def singular?
|
78
|
-
@resource.singularize.inspect == @resource.inspect
|
79
|
-
end
|
80
84
|
end
|
81
85
|
end
|
82
86
|
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Starter
|
4
|
+
module Builder
|
5
|
+
module Sequel
|
6
|
+
def model_klass
|
7
|
+
'Sequel::Model'
|
8
|
+
end
|
9
|
+
|
10
|
+
def initializer
|
11
|
+
<<-FILE.strip_heredoc
|
12
|
+
# frozen_string_literal: true
|
13
|
+
|
14
|
+
require 'yaml'
|
15
|
+
|
16
|
+
# load Sequel Configuration
|
17
|
+
settings = YAML.load_file('config/database.yml')
|
18
|
+
DB = Sequel.connect(settings[ENV['RACK_ENV']])
|
19
|
+
|
20
|
+
env = ENV['RACK_ENV'] || 'development'
|
21
|
+
|
22
|
+
logger = if %w[development test].include? env
|
23
|
+
log_dir = File.join(Dir.getwd, 'log')
|
24
|
+
log_file = File.join(log_dir, 'db.log')
|
25
|
+
FileUtils.mkdir(log_dir) unless Dir.exist?(log_dir)
|
26
|
+
Logger.new(File.open(log_file, 'a'))
|
27
|
+
else
|
28
|
+
Logger.new($stdout)
|
29
|
+
end
|
30
|
+
|
31
|
+
DB.loggers << logger
|
32
|
+
|
33
|
+
# FIXME: maybe remove it later …
|
34
|
+
# see: https://groups.google.com/forum/#!topic/sequel-talk/QIIv5qoltjs
|
35
|
+
Sequel::Model.require_valid_table = false
|
36
|
+
Sequel::Model.plugin :force_encoding, 'UTF-8'
|
37
|
+
FILE
|
38
|
+
end
|
39
|
+
|
40
|
+
def rakefile
|
41
|
+
<<-FILE.strip_heredoc
|
42
|
+
# Sequel migration tasks
|
43
|
+
namespace :db do
|
44
|
+
Sequel.extension(:migration)
|
45
|
+
|
46
|
+
desc "Prints current schema version"
|
47
|
+
task version: :connect do
|
48
|
+
version = DB.tables.include?(:schema_info) ? DB[:schema_info].first[:version] : 0
|
49
|
+
|
50
|
+
$stdout.print 'Schema Version: '
|
51
|
+
$stdout.puts version
|
52
|
+
end
|
53
|
+
|
54
|
+
desc 'Run all migrations in db/migrations'
|
55
|
+
task migrate: :connect do
|
56
|
+
Sequel::Migrator.apply(DB, 'db/migrations')
|
57
|
+
Rake::Task['db:version'].execute
|
58
|
+
end
|
59
|
+
|
60
|
+
desc "Perform rollback to specified target or full rollback as default"
|
61
|
+
task :rollback, [:target] => :connect do |t, args|
|
62
|
+
args.with_defaults(:target => 0)
|
63
|
+
|
64
|
+
Sequel::Migrator.run(DB, 'db/migrations', :target => args[:target].to_i)
|
65
|
+
Rake::Task['db:version'].execute
|
66
|
+
end
|
67
|
+
|
68
|
+
desc "Perform migration reset (full rollback and migration)"
|
69
|
+
task reset: :connect do
|
70
|
+
Sequel::Migrator.run(DB, 'db/migrations', target: 0)
|
71
|
+
Sequel::Migrator.run(DB, 'db/migrations')
|
72
|
+
Rake::Task['db:version'].execute
|
73
|
+
end
|
74
|
+
|
75
|
+
task connect: :environment do
|
76
|
+
require './config/initializers/database'
|
77
|
+
end
|
78
|
+
end
|
79
|
+
FILE
|
80
|
+
end
|
81
|
+
|
82
|
+
def gemfile
|
83
|
+
<<-FILE.strip_heredoc
|
84
|
+
# DB stack
|
85
|
+
gem 'sequel'
|
86
|
+
gem 'pg'
|
87
|
+
FILE
|
88
|
+
end
|
89
|
+
|
90
|
+
def migration(_klass_name, resource)
|
91
|
+
<<-FILE.strip_heredoc
|
92
|
+
Sequel.migration do
|
93
|
+
change do
|
94
|
+
create_table :#{resource} do
|
95
|
+
primary_key :id
|
96
|
+
|
97
|
+
DateTime :created_at
|
98
|
+
DateTime :updated_at
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
FILE
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
data/lib/starter/orms.rb
CHANGED
@@ -69,11 +69,9 @@ module Starter
|
|
69
69
|
|
70
70
|
case @orm
|
71
71
|
when 'sequel'
|
72
|
-
|
73
|
-
extend(Starter::Builder::Templates::Sequel)
|
72
|
+
extend(Starter::Builder::Sequel)
|
74
73
|
when 'activerecord', 'ar'
|
75
|
-
|
76
|
-
extend(Starter::Builder::Templates::ActiveRecord)
|
74
|
+
extend(Starter::Builder::ActiveRecord)
|
77
75
|
else
|
78
76
|
@orm = nil
|
79
77
|
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Starter
|
4
|
+
module Templates
|
5
|
+
# defining the endpoints -> http methods of a resource
|
6
|
+
module Endpoints
|
7
|
+
def crud
|
8
|
+
%i[
|
9
|
+
post
|
10
|
+
get_all
|
11
|
+
get_specific
|
12
|
+
put_specific
|
13
|
+
patch_specific
|
14
|
+
delete_specific
|
15
|
+
]
|
16
|
+
end
|
17
|
+
|
18
|
+
def singular_one
|
19
|
+
%i[
|
20
|
+
post
|
21
|
+
get_one
|
22
|
+
put_one
|
23
|
+
patch_one
|
24
|
+
delete_one
|
25
|
+
]
|
26
|
+
end
|
27
|
+
|
28
|
+
# available API/HTTP methods
|
29
|
+
# POST
|
30
|
+
def post
|
31
|
+
"
|
32
|
+
desc 'create #{resource.singularize}' do
|
33
|
+
tags %w[#{resource.singularize}]
|
34
|
+
end
|
35
|
+
params do
|
36
|
+
# TODO: specify the parameters
|
37
|
+
end
|
38
|
+
post do
|
39
|
+
# your code goes here
|
40
|
+
end"
|
41
|
+
end
|
42
|
+
|
43
|
+
# GET
|
44
|
+
def get_all
|
45
|
+
"
|
46
|
+
desc 'get all of #{resource.pluralize}' do
|
47
|
+
is_array true
|
48
|
+
tags %w[#{resource.singularize}]
|
49
|
+
end
|
50
|
+
get do
|
51
|
+
# your code goes here
|
52
|
+
end"
|
53
|
+
end
|
54
|
+
|
55
|
+
# rubocop:disable Style/CombinableLoops
|
56
|
+
%w[get put patch delete].each do |verb|
|
57
|
+
define_method(:"#{verb}_one") do
|
58
|
+
"
|
59
|
+
desc '#{verb} #{resource.singularize}' do
|
60
|
+
tags %w[#{resource.singularize}]
|
61
|
+
end
|
62
|
+
#{verb} do
|
63
|
+
# your code goes here
|
64
|
+
end"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
%w[get put patch delete].each do |verb|
|
69
|
+
define_method(:"#{verb}_specific") do
|
70
|
+
"
|
71
|
+
desc '#{verb} specific #{resource.singularize}' do
|
72
|
+
tags %w[#{resource.singularize}]
|
73
|
+
end
|
74
|
+
params do
|
75
|
+
requires :id
|
76
|
+
end
|
77
|
+
#{verb} ':id' do
|
78
|
+
# your code goes here
|
79
|
+
end"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
# rubocop:enable Style/CombinableLoops
|
83
|
+
|
84
|
+
# request specs shared examples
|
85
|
+
#
|
86
|
+
def post_spec
|
87
|
+
"it_behaves_like 'POST', params: {}"
|
88
|
+
end
|
89
|
+
|
90
|
+
def get_all_spec
|
91
|
+
"it_behaves_like 'GET all'"
|
92
|
+
end
|
93
|
+
|
94
|
+
%w[get delete].each do |verb|
|
95
|
+
define_method(:"#{verb}_one_spec") do
|
96
|
+
"it_behaves_like '#{verb.upcase} one'"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
%w[put patch].each do |verb|
|
101
|
+
define_method(:"#{verb}_one_spec") do
|
102
|
+
"it_behaves_like '#{verb.upcase} one', params: {}"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
%w[get delete].each do |verb|
|
107
|
+
define_method(:"#{verb}_specific_spec") do
|
108
|
+
"it_behaves_like '#{verb.upcase} specific', key: 1"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
%w[put patch].each do |verb|
|
113
|
+
define_method(:"#{verb}_specific_spec") do
|
114
|
+
"it_behaves_like '#{verb.upcase} specific', key: 1, params: {}"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Starter
|
4
|
+
module Templates
|
5
|
+
module Files
|
6
|
+
# API template for resource
|
7
|
+
def api_file
|
8
|
+
<<-FILE.strip_heredoc
|
9
|
+
# frozen_string_literal: true
|
10
|
+
|
11
|
+
module Api
|
12
|
+
module Endpoints
|
13
|
+
class #{@naming.klass_name} < Grape::API
|
14
|
+
namespace :#{resource.downcase} do
|
15
|
+
#{endpoints}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
FILE
|
21
|
+
end
|
22
|
+
|
23
|
+
def entity_file
|
24
|
+
<<-FILE.strip_heredoc
|
25
|
+
# frozen_string_literal: true
|
26
|
+
|
27
|
+
module Api
|
28
|
+
module Entities
|
29
|
+
class #{@naming.klass_name} < Grape::Entity
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
FILE
|
34
|
+
end
|
35
|
+
|
36
|
+
# LIB template for resource
|
37
|
+
def base_namespace_file
|
38
|
+
<<-FILE.strip_heredoc
|
39
|
+
# frozen_string_literal: true
|
40
|
+
|
41
|
+
module #{@naming.klass_name}
|
42
|
+
VERSION = '0.1.0'
|
43
|
+
end
|
44
|
+
FILE
|
45
|
+
end
|
46
|
+
|
47
|
+
def lib_file
|
48
|
+
<<-FILE.strip_heredoc
|
49
|
+
# frozen_string_literal: true
|
50
|
+
|
51
|
+
module Models
|
52
|
+
class #{@naming.lib_klass_name}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
FILE
|
56
|
+
end
|
57
|
+
|
58
|
+
def api_spec
|
59
|
+
prefix = base_prefix ? "/#{base_prefix}" : ''
|
60
|
+
<<-FILE.strip_heredoc
|
61
|
+
# frozen_string_literal: true
|
62
|
+
|
63
|
+
require 'spec_helper'
|
64
|
+
|
65
|
+
RSpec.describe '#{prefix}/#{base_version}/#{resource.downcase}' do
|
66
|
+
#{endpoint_specs}
|
67
|
+
end
|
68
|
+
FILE
|
69
|
+
end
|
70
|
+
|
71
|
+
def lib_spec
|
72
|
+
<<-FILE.strip_heredoc
|
73
|
+
# frozen_string_literal: true
|
74
|
+
|
75
|
+
require 'spec_helper'
|
76
|
+
|
77
|
+
RSpec.describe Models::#{@naming.klass_name} do
|
78
|
+
pending 'write specs'
|
79
|
+
end
|
80
|
+
FILE
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/lib/starter/version.rb
CHANGED
data/lib/starter.rb
CHANGED
@@ -1,15 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'awesome_print'
|
4
|
-
|
5
4
|
require 'active_support'
|
6
5
|
require 'active_support/core_ext/string'
|
7
|
-
|
8
|
-
# require 'starter/version'
|
9
|
-
# require 'starter/builder'
|
10
|
-
# require 'starter/config'
|
11
|
-
|
12
6
|
require 'zeitwerk'
|
7
|
+
|
13
8
|
loader = Zeitwerk::Loader.for_gem
|
14
9
|
loader.setup
|
15
10
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-starter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.6.
|
4
|
+
version: 1.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- LeFnord
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: gli
|
@@ -111,17 +111,17 @@ files:
|
|
111
111
|
- grape-starter.gemspec
|
112
112
|
- lib/starter.rb
|
113
113
|
- lib/starter/build.rb
|
114
|
+
- lib/starter/builder/activerecord.rb
|
114
115
|
- lib/starter/builder/base_file.rb
|
115
116
|
- lib/starter/builder/names.rb
|
116
|
-
- lib/starter/builder/
|
117
|
-
- lib/starter/builder/templates/endpoints.rb
|
118
|
-
- lib/starter/builder/templates/files.rb
|
119
|
-
- lib/starter/builder/templates/sequel.rb
|
117
|
+
- lib/starter/builder/sequel.rb
|
120
118
|
- lib/starter/config.rb
|
121
119
|
- lib/starter/file_ops.rb
|
122
120
|
- lib/starter/orms.rb
|
123
121
|
- lib/starter/rake/grape_tasks.rb
|
124
122
|
- lib/starter/rspec/request_specs.rb
|
123
|
+
- lib/starter/templates/endpoints.rb
|
124
|
+
- lib/starter/templates/files.rb
|
125
125
|
- lib/starter/version.rb
|
126
126
|
- template/.gitignore
|
127
127
|
- template/.rubocop.yml
|
@@ -171,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
171
171
|
- !ruby/object:Gem::Version
|
172
172
|
version: '0'
|
173
173
|
requirements: []
|
174
|
-
rubygems_version: 3.4.
|
174
|
+
rubygems_version: 3.4.7
|
175
175
|
signing_key:
|
176
176
|
specification_version: 4
|
177
177
|
summary: Creates a Grape Rack skeleton
|
@@ -1,107 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'active_record'
|
4
|
-
|
5
|
-
module Starter
|
6
|
-
module Builder
|
7
|
-
module Templates
|
8
|
-
module ActiveRecord
|
9
|
-
def model_klass
|
10
|
-
'ActiveRecord::Base'
|
11
|
-
end
|
12
|
-
|
13
|
-
def initializer
|
14
|
-
<<-FILE.strip_heredoc
|
15
|
-
# frozen_string_literal: true
|
16
|
-
|
17
|
-
require 'yaml'
|
18
|
-
require 'erb'
|
19
|
-
require 'active_record'
|
20
|
-
|
21
|
-
config_content = File.read(File.join('config', 'database.yml'))
|
22
|
-
|
23
|
-
db_conf = YAML.safe_load(ERB.new(config_content).result)
|
24
|
-
env = ENV['RACK_ENV'] || 'development'
|
25
|
-
|
26
|
-
ActiveRecord::Base.establish_connection db_conf[env]
|
27
|
-
logger = if %w[development test].include? env
|
28
|
-
log_dir = File.join(Dir.getwd, 'log')
|
29
|
-
log_file = File.join(log_dir, 'db.log')
|
30
|
-
FileUtils.mkdir(log_dir) unless Dir.exist?(log_dir)
|
31
|
-
Logger.new(File.open(log_file, 'a'))
|
32
|
-
else
|
33
|
-
Logger.new($stdout)
|
34
|
-
end
|
35
|
-
|
36
|
-
ActiveRecord::Base.logger = logger
|
37
|
-
|
38
|
-
# Middleware
|
39
|
-
module ActiveRecord
|
40
|
-
module Rack
|
41
|
-
# ActiveRecord >= 5 removes the Pool management
|
42
|
-
class ConnectionManagement
|
43
|
-
def initialize(app)
|
44
|
-
@app = app
|
45
|
-
end
|
46
|
-
|
47
|
-
def call(env)
|
48
|
-
response = @app.call(env)
|
49
|
-
response[2] = ::Rack::BodyProxy.new(response[2]) do
|
50
|
-
ActiveRecord::Base.clear_active_connections!
|
51
|
-
end
|
52
|
-
|
53
|
-
return response
|
54
|
-
rescue StandardError
|
55
|
-
ActiveRecord::Base.clear_active_connections!
|
56
|
-
raise
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
FILE
|
62
|
-
end
|
63
|
-
|
64
|
-
def rakefile
|
65
|
-
<<-FILE.strip_heredoc
|
66
|
-
# ActiveRecord migration tasks
|
67
|
-
require 'active_record'
|
68
|
-
include ActiveRecord::Tasks
|
69
|
-
config_dir = File.expand_path('../config', __FILE__)
|
70
|
-
config_content = File.join(config_dir, 'database.yml')
|
71
|
-
DatabaseTasks.env = ENV['RACK_ENV'] || 'development'
|
72
|
-
DatabaseTasks.database_configuration = YAML.load_file(config_content)
|
73
|
-
DatabaseTasks.db_dir = 'db'
|
74
|
-
DatabaseTasks.migrations_paths = File.join('db', 'migrate')
|
75
|
-
|
76
|
-
ActiveRecord::Base.configurations = DatabaseTasks.database_configuration
|
77
|
-
ActiveRecord::Base.establish_connection DatabaseTasks.env.to_sym
|
78
|
-
|
79
|
-
load 'active_record/railties/databases.rake'
|
80
|
-
FILE
|
81
|
-
end
|
82
|
-
|
83
|
-
def gemfile
|
84
|
-
<<-FILE.strip_heredoc
|
85
|
-
# DB stuff
|
86
|
-
gem 'activerecord', '>= 6'
|
87
|
-
gem 'pg'
|
88
|
-
FILE
|
89
|
-
end
|
90
|
-
|
91
|
-
def migration(klass_name, resource)
|
92
|
-
version = "#{::ActiveRecord::VERSION::MAJOR}.#{::ActiveRecord::VERSION::MINOR}"
|
93
|
-
<<-FILE.strip_heredoc
|
94
|
-
class Create#{klass_name} < ActiveRecord::Migration[#{version}]
|
95
|
-
def change
|
96
|
-
create_table :#{resource} do |t|
|
97
|
-
|
98
|
-
t.timestamps
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
FILE
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
@@ -1,121 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Starter
|
4
|
-
module Builder
|
5
|
-
module Templates
|
6
|
-
# defining the endpoints -> http methods of a resource
|
7
|
-
module Endpoints
|
8
|
-
def crud
|
9
|
-
%i[
|
10
|
-
post
|
11
|
-
get_all
|
12
|
-
get_specific
|
13
|
-
put_specific
|
14
|
-
patch_specific
|
15
|
-
delete_specific
|
16
|
-
]
|
17
|
-
end
|
18
|
-
|
19
|
-
def singular_one
|
20
|
-
%i[
|
21
|
-
post
|
22
|
-
get_one
|
23
|
-
put_one
|
24
|
-
patch_one
|
25
|
-
delete_one
|
26
|
-
]
|
27
|
-
end
|
28
|
-
|
29
|
-
# available API/HTTP methods
|
30
|
-
# POST
|
31
|
-
def post
|
32
|
-
"
|
33
|
-
desc 'create #{resource.singularize}' do
|
34
|
-
tags %w[#{resource.singularize}]
|
35
|
-
end
|
36
|
-
params do
|
37
|
-
# TODO: specify the parameters
|
38
|
-
end
|
39
|
-
post do
|
40
|
-
# your code goes here
|
41
|
-
end"
|
42
|
-
end
|
43
|
-
|
44
|
-
# GET
|
45
|
-
def get_all
|
46
|
-
"
|
47
|
-
desc 'get all of #{resource.pluralize}' do
|
48
|
-
is_array true
|
49
|
-
tags %w[#{resource.singularize}]
|
50
|
-
end
|
51
|
-
get do
|
52
|
-
# your code goes here
|
53
|
-
end"
|
54
|
-
end
|
55
|
-
|
56
|
-
# rubocop:disable Style/CombinableLoops
|
57
|
-
%w[get put patch delete].each do |verb|
|
58
|
-
define_method(:"#{verb}_one") do
|
59
|
-
"
|
60
|
-
desc '#{verb} #{resource.singularize}' do
|
61
|
-
tags %w[#{resource.singularize}]
|
62
|
-
end
|
63
|
-
#{verb} do
|
64
|
-
# your code goes here
|
65
|
-
end"
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
%w[get put patch delete].each do |verb|
|
70
|
-
define_method(:"#{verb}_specific") do
|
71
|
-
"
|
72
|
-
desc '#{verb} specific #{resource.singularize}' do
|
73
|
-
tags %w[#{resource.singularize}]
|
74
|
-
end
|
75
|
-
params do
|
76
|
-
requires :id
|
77
|
-
end
|
78
|
-
#{verb} ':id' do
|
79
|
-
# your code goes here
|
80
|
-
end"
|
81
|
-
end
|
82
|
-
end
|
83
|
-
# rubocop:enable Style/CombinableLoops
|
84
|
-
|
85
|
-
# request specs shared examples
|
86
|
-
#
|
87
|
-
def post_spec
|
88
|
-
"it_behaves_like 'POST', params: {}"
|
89
|
-
end
|
90
|
-
|
91
|
-
def get_all_spec
|
92
|
-
"it_behaves_like 'GET all'"
|
93
|
-
end
|
94
|
-
|
95
|
-
%w[get delete].each do |verb|
|
96
|
-
define_method(:"#{verb}_one_spec") do
|
97
|
-
"it_behaves_like '#{verb.upcase} one'"
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
%w[put patch].each do |verb|
|
102
|
-
define_method(:"#{verb}_one_spec") do
|
103
|
-
"it_behaves_like '#{verb.upcase} one', params: {}"
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
%w[get delete].each do |verb|
|
108
|
-
define_method(:"#{verb}_specific_spec") do
|
109
|
-
"it_behaves_like '#{verb.upcase} specific', key: 1"
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
%w[put patch].each do |verb|
|
114
|
-
define_method(:"#{verb}_specific_spec") do
|
115
|
-
"it_behaves_like '#{verb.upcase} specific', key: 1, params: {}"
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
@@ -1,86 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Starter
|
4
|
-
module Builder
|
5
|
-
module Templates
|
6
|
-
module Files
|
7
|
-
# API template for resource
|
8
|
-
def api_file
|
9
|
-
<<-FILE.strip_heredoc
|
10
|
-
# frozen_string_literal: true
|
11
|
-
|
12
|
-
module Api
|
13
|
-
module Endpoints
|
14
|
-
class #{klass_name} < Grape::API
|
15
|
-
namespace :#{resource.downcase} do
|
16
|
-
#{endpoints}
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
FILE
|
22
|
-
end
|
23
|
-
|
24
|
-
def entity_file
|
25
|
-
<<-FILE.strip_heredoc
|
26
|
-
# frozen_string_literal: true
|
27
|
-
|
28
|
-
module Api
|
29
|
-
module Entities
|
30
|
-
class #{klass_name} < Grape::Entity
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
FILE
|
35
|
-
end
|
36
|
-
|
37
|
-
# LIB template for resource
|
38
|
-
def base_namespace_file
|
39
|
-
<<-FILE.strip_heredoc
|
40
|
-
# frozen_string_literal: true
|
41
|
-
|
42
|
-
module #{klass_name}
|
43
|
-
VERSION = '0.1.0'
|
44
|
-
end
|
45
|
-
FILE
|
46
|
-
end
|
47
|
-
|
48
|
-
def lib_file
|
49
|
-
<<-FILE.strip_heredoc
|
50
|
-
# frozen_string_literal: true
|
51
|
-
|
52
|
-
module Models
|
53
|
-
class #{lib_klass_name}
|
54
|
-
end
|
55
|
-
end
|
56
|
-
FILE
|
57
|
-
end
|
58
|
-
|
59
|
-
def api_spec
|
60
|
-
prefix = base_prefix ? "/#{base_prefix}" : ''
|
61
|
-
<<-FILE.strip_heredoc
|
62
|
-
# frozen_string_literal: true
|
63
|
-
|
64
|
-
require 'spec_helper'
|
65
|
-
|
66
|
-
RSpec.describe '#{prefix}/#{base_version}/#{resource.downcase}' do
|
67
|
-
#{endpoint_specs}
|
68
|
-
end
|
69
|
-
FILE
|
70
|
-
end
|
71
|
-
|
72
|
-
def lib_spec
|
73
|
-
<<-FILE.strip_heredoc
|
74
|
-
# frozen_string_literal: true
|
75
|
-
|
76
|
-
require 'spec_helper'
|
77
|
-
|
78
|
-
RSpec.describe Models::#{klass_name} do
|
79
|
-
pending 'write specs'
|
80
|
-
end
|
81
|
-
FILE
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
@@ -1,108 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Starter
|
4
|
-
module Builder
|
5
|
-
module Templates
|
6
|
-
module Sequel
|
7
|
-
def model_klass
|
8
|
-
'Sequel::Model'
|
9
|
-
end
|
10
|
-
|
11
|
-
def initializer
|
12
|
-
<<-FILE.strip_heredoc
|
13
|
-
# frozen_string_literal: true
|
14
|
-
|
15
|
-
require 'yaml'
|
16
|
-
|
17
|
-
# load Sequel Configuration
|
18
|
-
settings = YAML.load_file('config/database.yml')
|
19
|
-
DB = Sequel.connect(settings[ENV['RACK_ENV']])
|
20
|
-
|
21
|
-
env = ENV['RACK_ENV'] || 'development'
|
22
|
-
|
23
|
-
logger = if %w[development test].include? env
|
24
|
-
log_dir = File.join(Dir.getwd, 'log')
|
25
|
-
log_file = File.join(log_dir, 'db.log')
|
26
|
-
FileUtils.mkdir(log_dir) unless Dir.exist?(log_dir)
|
27
|
-
Logger.new(File.open(log_file, 'a'))
|
28
|
-
else
|
29
|
-
Logger.new($stdout)
|
30
|
-
end
|
31
|
-
|
32
|
-
DB.loggers << logger
|
33
|
-
|
34
|
-
# FIXME: maybe remove it later …
|
35
|
-
# see: https://groups.google.com/forum/#!topic/sequel-talk/QIIv5qoltjs
|
36
|
-
Sequel::Model.require_valid_table = false
|
37
|
-
Sequel::Model.plugin :force_encoding, 'UTF-8'
|
38
|
-
FILE
|
39
|
-
end
|
40
|
-
|
41
|
-
def rakefile
|
42
|
-
<<-FILE.strip_heredoc
|
43
|
-
# Sequel migration tasks
|
44
|
-
namespace :db do
|
45
|
-
Sequel.extension(:migration)
|
46
|
-
|
47
|
-
desc "Prints current schema version"
|
48
|
-
task version: :connect do
|
49
|
-
version = DB.tables.include?(:schema_info) ? DB[:schema_info].first[:version] : 0
|
50
|
-
|
51
|
-
$stdout.print 'Schema Version: '
|
52
|
-
$stdout.puts version
|
53
|
-
end
|
54
|
-
|
55
|
-
desc 'Run all migrations in db/migrations'
|
56
|
-
task migrate: :connect do
|
57
|
-
Sequel::Migrator.apply(DB, 'db/migrations')
|
58
|
-
Rake::Task['db:version'].execute
|
59
|
-
end
|
60
|
-
|
61
|
-
desc "Perform rollback to specified target or full rollback as default"
|
62
|
-
task :rollback, [:target] => :connect do |t, args|
|
63
|
-
args.with_defaults(:target => 0)
|
64
|
-
|
65
|
-
Sequel::Migrator.run(DB, 'db/migrations', :target => args[:target].to_i)
|
66
|
-
Rake::Task['db:version'].execute
|
67
|
-
end
|
68
|
-
|
69
|
-
desc "Perform migration reset (full rollback and migration)"
|
70
|
-
task reset: :connect do
|
71
|
-
Sequel::Migrator.run(DB, 'db/migrations', target: 0)
|
72
|
-
Sequel::Migrator.run(DB, 'db/migrations')
|
73
|
-
Rake::Task['db:version'].execute
|
74
|
-
end
|
75
|
-
|
76
|
-
task connect: :environment do
|
77
|
-
require './config/initializers/database'
|
78
|
-
end
|
79
|
-
end
|
80
|
-
FILE
|
81
|
-
end
|
82
|
-
|
83
|
-
def gemfile
|
84
|
-
<<-FILE.strip_heredoc
|
85
|
-
# DB stack
|
86
|
-
gem 'sequel'
|
87
|
-
gem 'pg'
|
88
|
-
FILE
|
89
|
-
end
|
90
|
-
|
91
|
-
def migration(_klass_name, resource)
|
92
|
-
<<-FILE.strip_heredoc
|
93
|
-
Sequel.migration do
|
94
|
-
change do
|
95
|
-
create_table :#{resource} do
|
96
|
-
primary_key :id
|
97
|
-
|
98
|
-
DateTime :created_at
|
99
|
-
DateTime :updated_at
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
FILE
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|