grape-starter 1.6.0 → 1.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -2
- data/lib/starter/build.rb +2 -9
- data/lib/starter/builder/activerecord.rb +105 -0
- data/lib/starter/builder/names.rb +2 -4
- 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: 3f03aba0838fc7a1877d9628a5a0fcdd3a29eb16836c9c41b29950814c7e5e6b
|
4
|
+
data.tar.gz: 64b0c86ecdcca53c866f275be224efe3937eb02dca2f299e4c7d416804c2a511
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68c307c7b2d86729422ff90cdde27b6a46a77c52a37b8759cbe885e6e3fa547bb7e07b04c48473cc7e7591ffc77fe8a705d7c85193f6f10f93e22d9749f3baf6
|
7
|
+
data.tar.gz: 78dbcdbf71393a23687be2703ac207a13ad617c04148e0813e2a49df7e3241f8a1f6d32ba3c9b3dd4865e826ee358c242c9310da424a6e32b2e7b14697f91bf2
|
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,18 +1,11 @@
|
|
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
5
|
extend Builder::Names
|
13
6
|
extend Builder::BaseFile
|
14
|
-
extend
|
15
|
-
extend
|
7
|
+
extend Templates::Files
|
8
|
+
extend Templates::Endpoints
|
16
9
|
|
17
10
|
class << self
|
18
11
|
attr_reader :prefix, :resource, :set, :force, :entity, :destination, :orm
|
@@ -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
|
@@ -13,12 +13,10 @@ module Starter
|
|
13
13
|
|
14
14
|
case Starter::Config.read[:orm]
|
15
15
|
when 'sequel'
|
16
|
-
|
17
|
-
extend(Starter::Builder::Templates::Sequel)
|
16
|
+
extend(Starter::Builder::Sequel)
|
18
17
|
"#{klass_name} < #{model_klass}"
|
19
18
|
when 'activerecord', 'ar'
|
20
|
-
|
21
|
-
extend(Starter::Builder::Templates::ActiveRecord)
|
19
|
+
extend(Starter::Builder::ActiveRecord)
|
22
20
|
"#{klass_name} < #{model_klass}"
|
23
21
|
else
|
24
22
|
klass_name
|
@@ -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 #{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 #{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 #{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 #{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::#{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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- LeFnord
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-02-
|
11
|
+
date: 2023-02-27 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
|