grape-starter 1.5.1 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1bf6bb14f7920a1c45c3bbd5737167d239f752b31d2977416647ac96f9ef7207
4
- data.tar.gz: 6d9a2bb32927b585a1f32bae838f79edadb634420492f5c5b149c50754dc4c33
3
+ metadata.gz: 70eb1ef044b58a9637cb920fcc9129a12ca25392d5d160de75a6ee3ea9483052
4
+ data.tar.gz: c700ad85e12e2406b11a647d8584d3070d7e6e9c10742fba0909a8182264dff5
5
5
  SHA512:
6
- metadata.gz: 439b3e8c90b6e79c3b61da51b27ff4158e5958271035da0aa8181a0c8664cda39f4140fd32708cc013893932b5e795c42d3e9a8101776594fde0258c0986c610
7
- data.tar.gz: ec9da624c7c41f6c0af7660650ea53da8b3da609e7354e878e07dbfd80e1dca0aa4a3092a6243ef5786547d1d3f4e10efb49fc9e5e256dc8dbe968a902102436
6
+ metadata.gz: c46ea6564c067d4174c29d705482068b991589503928bb5de57f0cfb5b0034485d48a8f3dc7f4b52621110727d6b78f7d86544682ea2d9ebf36054ba00029da8
7
+ data.tar.gz: ba9e7eb94b6e5412baba003037927310686dae982b439fffb5241d800101c405999c4246ad22c44b175ba3d676c52af632c8f24eb6241cfacab42995e36297d0
@@ -12,10 +12,10 @@ jobs:
12
12
  rubocop:
13
13
  runs-on: ubuntu-latest
14
14
  steps:
15
- - uses: actions/checkout@v2
15
+ - uses: actions/checkout@v3
16
16
  - uses: ruby/setup-ruby@v1
17
17
  with:
18
- ruby-version: '3.1'
18
+ ruby-version: '3.2'
19
19
  bundler-cache: true
20
20
  - name: Run rubocop
21
21
  run: bundle exec rubocop --parallel --format progress
@@ -25,10 +25,10 @@ jobs:
25
25
  needs: ['rubocop']
26
26
  strategy:
27
27
  matrix:
28
- ruby-version: ['2.7', '3.0', '3.1', head]
28
+ ruby-version: ['3.0', '3.1', '3.2', head]
29
29
 
30
30
  steps:
31
- - uses: actions/checkout@v2
31
+ - uses: actions/checkout@v3
32
32
  - name: Set up Ruby
33
33
  uses: ruby/setup-ruby@v1
34
34
  with:
data/.rubocop.yml CHANGED
@@ -14,7 +14,7 @@ AllCops:
14
14
  - template/spec/spec-helper.rb
15
15
  UseCache: true
16
16
  NewCops: enable
17
- TargetRubyVersion: 3.0
17
+ TargetRubyVersion: 3.2
18
18
  SuggestExtensions: false
19
19
 
20
20
  Layout/IndentationWidth:
@@ -45,3 +45,6 @@ Style/AsciiComments:
45
45
 
46
46
  Style/Documentation:
47
47
  Enabled: false
48
+
49
+ Style/HashSyntax:
50
+ Enabled: false # to support ruby 3.0
data/bin/grape-starter CHANGED
@@ -38,7 +38,7 @@ command :new do |c|
38
38
  starter_gem = Gem::Specification.find_by_name('grape-starter').gem_dir
39
39
  src = File.join(starter_gem, 'template', '.')
40
40
 
41
- Starter::Builder.new!(args.first, src, dest, options)
41
+ Starter::Build.new!(args.first, src, dest, options)
42
42
  $stdout.puts "created: #{args.first}"
43
43
 
44
44
  # after creating tasks
@@ -71,7 +71,7 @@ command :add do |c|
71
71
 
72
72
  begin
73
73
  builder_options = global_options.merge(set: set).merge(options)
74
- created_files = Starter::Builder.add!(resource, builder_options)
74
+ created_files = Starter::Build.add!(resource, builder_options)
75
75
 
76
76
  `bundle exec rubocop -a #{created_files.join(' ')}`
77
77
  $stdout.puts "added resource: #{resource}"
@@ -87,11 +87,11 @@ command :rm do |c|
87
87
  c.desc 'removes also entity file'
88
88
  c.switch [:e, :entity], negatable: false
89
89
 
90
- c.action do |global_options, options, args|
90
+ c.action do |_global_options, options, args|
91
91
  exit_now! 'no resource given' if args.empty?
92
92
  resource = args.first
93
93
 
94
- Starter::Builder.remove!(resource, options)
94
+ Starter::Build.remove!(resource, options)
95
95
 
96
96
  $stdout.puts "removed resource: #{resource}"
97
97
  end
@@ -28,4 +28,5 @@ Gem::Specification.new do |spec|
28
28
  spec.add_dependency 'activesupport', '>= 6', '< 8'
29
29
  spec.add_dependency 'rubocop', '~> 1.24'
30
30
  spec.add_dependency 'awesome_print', '~> 1.9'
31
+ spec.add_dependency 'zeitwerk'
31
32
  end
@@ -1,18 +1,18 @@
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
- class Builder
12
- extend Names
13
- extend BaseFile
14
- extend Templates::Files
15
- extend Templates::Endpoints
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
+ class Build
12
+ extend Builder::Names
13
+ extend Builder::BaseFile
14
+ extend Builder::Templates::Files
15
+ extend Builder::Templates::Endpoints
16
16
 
17
17
  class << self
18
18
  attr_reader :prefix, :resource, :set, :force, :entity, :destination, :orm
@@ -120,7 +120,7 @@ module Starter
120
120
  # creates a new file in lib folder as namespace, includind the version
121
121
  def add_namespace_with_version
122
122
  new_lib = File.join(destination, 'lib', base_file_name)
123
- FileFoo.write_file(new_lib, base_namespace_file.strip_heredoc)
123
+ FileOps.write_file(new_lib, base_namespace_file.strip_heredoc)
124
124
  end
125
125
 
126
126
  #
@@ -128,7 +128,7 @@ module Starter
128
128
  def replace_static(file, replacement)
129
129
  file_path = File.join(destination, file)
130
130
 
131
- FileFoo.call!(file_path) { |content| content.gsub!('{{{grape-starter}}}', replacement.to_s) }
131
+ FileOps.call!(file_path) { |content| content.gsub!('{{{grape-starter}}}', replacement.to_s) }
132
132
  end
133
133
 
134
134
  # #add! a new resource releated helper methods
@@ -159,7 +159,7 @@ module Starter
159
159
  def save_file(new_file)
160
160
  new_file_name = send("#{new_file}_name")
161
161
  should_raise?(new_file_name)
162
- FileFoo.write_file(new_file_name, send(new_file.strip_heredoc))
162
+ FileOps.write_file(new_file_name, send(new_file.strip_heredoc))
163
163
  end
164
164
 
165
165
  #
@@ -1,45 +1,47 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Starter
4
- module BaseFile
5
- # add it in api base
6
- def add_mount_point
7
- FileFoo.call!(api_base_file_name) { |content| add_to_base(content) }
8
- end
9
-
10
- # adding mount point to base class
11
- def add_to_base(file)
12
- occurence = file.scan(/(\s+mount\s.*?\n)/).last.first
13
- replacement = occurence + mount_point
14
- file.sub!(occurence, replacement)
15
- end
16
-
17
- # removes in api base
18
- def remove_mount_point
19
- FileFoo.call!(api_base_file_name) { |content| remove_from_base(content) }
20
- end
21
-
22
- # removes mount point from base class
23
- def remove_from_base(file)
24
- file.sub!(mount_point, '')
25
- end
26
-
27
- # parses out the prefix from base api file
28
- def base_prefix
29
- Starter::Config.read[:prefix]
30
- end
31
-
32
- # parses out the version from base api file
33
- def base_version
34
- base_file
35
-
36
- base_file.scan(/version\s+(.+),/).first.first.delete!("'")
37
- end
38
-
39
- # get api base file as string
40
- def base_file
41
- file = File.join(Dir.getwd, 'api', 'base.rb')
42
- FileFoo.read_file(file)
4
+ module Builder
5
+ module BaseFile
6
+ # add it in api base
7
+ def add_mount_point
8
+ FileOps.call!(api_base_file_name) { |content| add_to_base(content) }
9
+ end
10
+
11
+ # adding mount point to base class
12
+ def add_to_base(file)
13
+ occurence = file.scan(/(\s+mount\s.*?\n)/).last.first
14
+ replacement = occurence + mount_point
15
+ file.sub!(occurence, replacement)
16
+ end
17
+
18
+ # removes in api base
19
+ def remove_mount_point
20
+ FileOps.call!(api_base_file_name) { |content| remove_from_base(content) }
21
+ end
22
+
23
+ # removes mount point from base class
24
+ def remove_from_base(file)
25
+ file.sub!(mount_point, '')
26
+ end
27
+
28
+ # parses out the prefix from base api file
29
+ def base_prefix
30
+ Starter::Config.read[:prefix]
31
+ end
32
+
33
+ # parses out the version from base api file
34
+ def base_version
35
+ base_file
36
+
37
+ base_file.scan(/version\s+(.+),/).first.first.delete!("'")
38
+ end
39
+
40
+ # get api base file as string
41
+ def base_file
42
+ file = File.join(Dir.getwd, 'api', 'base.rb')
43
+ FileOps.read_file(file)
44
+ end
43
45
  end
44
46
  end
45
47
  end
@@ -1,80 +1,82 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Starter
4
- module Names
5
- def klass_name
6
- for_klass = prepare_klass
7
- singular? ? for_klass.classify : for_klass.classify.pluralize
8
- end
4
+ module Builder
5
+ module Names
6
+ def klass_name
7
+ for_klass = prepare_klass
8
+ singular? ? for_klass.classify : for_klass.classify.pluralize
9
+ end
9
10
 
10
- def lib_klass_name
11
- return klass_name unless @orm
11
+ def lib_klass_name
12
+ return klass_name unless @orm
12
13
 
13
- case ::Starter::Config.read[:orm]
14
- when 'sequel'
15
- require 'starter/builder/templates/sequel'
16
- extend(::Starter::Templates::Sequel)
17
- "#{klass_name} < #{model_klass}"
18
- when 'activerecord', 'ar'
19
- require 'starter/builder/templates/activerecord'
20
- extend(::Starter::Templates::ActiveRecord)
21
- "#{klass_name} < #{model_klass}"
22
- else
23
- klass_name
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
24
26
  end
25
- end
26
27
 
27
- # rubocop:disable Style/StringConcatenation
28
- def base_file_name
29
- @resource.tr('/', '-').downcase + '.rb'
30
- end
31
- # rubocop:enable Style/StringConcatenation
28
+ # rubocop:disable Style/StringConcatenation
29
+ def base_file_name
30
+ @resource.tr('/', '-').downcase + '.rb'
31
+ end
32
+ # rubocop:enable Style/StringConcatenation
32
33
 
33
- def base_spec_name
34
- base_file_name.gsub(/.rb$/, '_spec.rb')
35
- end
34
+ def base_spec_name
35
+ base_file_name.gsub(/.rb$/, '_spec.rb')
36
+ end
36
37
 
37
- def mount_point
38
- " mount Endpoints::#{klass_name}\n"
39
- end
38
+ def mount_point
39
+ " mount Endpoints::#{klass_name}\n"
40
+ end
40
41
 
41
- def api_base_file_name
42
- File.join(Dir.getwd, 'api', 'base.rb')
43
- end
42
+ def api_base_file_name
43
+ File.join(Dir.getwd, 'api', 'base.rb')
44
+ end
44
45
 
45
- # resource file
46
- def api_file_name
47
- File.join(Dir.getwd, 'api', 'endpoints', base_file_name)
48
- end
46
+ # resource file
47
+ def api_file_name
48
+ File.join(Dir.getwd, 'api', 'endpoints', base_file_name)
49
+ end
49
50
 
50
- # resource file
51
- def entity_file_name
52
- File.join(Dir.getwd, 'api', 'entities', base_file_name)
53
- end
51
+ # resource file
52
+ def entity_file_name
53
+ File.join(Dir.getwd, 'api', 'entities', base_file_name)
54
+ end
54
55
 
55
- # lib file
56
- def lib_file_name
57
- File.join(Dir.getwd, 'lib', 'models', base_file_name)
58
- end
56
+ # lib file
57
+ def lib_file_name
58
+ File.join(Dir.getwd, 'lib', 'models', base_file_name)
59
+ end
59
60
 
60
- # resource spec
61
- def api_spec_name
62
- File.join(Dir.getwd, 'spec', 'requests', base_spec_name)
63
- end
61
+ # resource spec
62
+ def api_spec_name
63
+ File.join(Dir.getwd, 'spec', 'requests', base_spec_name)
64
+ end
64
65
 
65
- # lib spec
66
- def lib_spec_name
67
- File.join(Dir.getwd, 'spec', 'lib', 'models', base_spec_name)
68
- end
66
+ # lib spec
67
+ def lib_spec_name
68
+ File.join(Dir.getwd, 'spec', 'lib', 'models', base_spec_name)
69
+ end
69
70
 
70
- private
71
+ private
71
72
 
72
- def prepare_klass
73
- @resource.tr('-', '/')
74
- end
73
+ def prepare_klass
74
+ @resource.tr('-', '/')
75
+ end
75
76
 
76
- def singular?
77
- @resource.singularize.inspect == @resource.inspect
77
+ def singular?
78
+ @resource.singularize.inspect == @resource.inspect
79
+ end
78
80
  end
79
81
  end
80
82
  end
@@ -3,102 +3,104 @@
3
3
  require 'active_record'
4
4
 
5
5
  module Starter
6
- module Templates
7
- module ActiveRecord
8
- def model_klass
9
- 'ActiveRecord::Base'
10
- end
6
+ module Builder
7
+ module Templates
8
+ module ActiveRecord
9
+ def model_klass
10
+ 'ActiveRecord::Base'
11
+ end
11
12
 
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
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
45
52
 
46
- def call(env)
47
- response = @app.call(env)
48
- response[2] = ::Rack::BodyProxy.new(response[2]) do
53
+ return response
54
+ rescue StandardError
49
55
  ActiveRecord::Base.clear_active_connections!
56
+ raise
50
57
  end
51
-
52
- return response
53
- rescue StandardError
54
- ActiveRecord::Base.clear_active_connections!
55
- raise
56
58
  end
57
59
  end
58
60
  end
61
+ FILE
59
62
  end
60
- FILE
61
- end
62
63
 
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
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
81
82
 
82
- def gemfile
83
- <<-FILE.strip_heredoc
84
- # DB stuff
85
- gem 'activerecord', '>= 6'
86
- gem 'pg'
87
- FILE
88
- end
83
+ def gemfile
84
+ <<-FILE.strip_heredoc
85
+ # DB stuff
86
+ gem 'activerecord', '>= 6'
87
+ gem 'pg'
88
+ FILE
89
+ end
89
90
 
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|
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|
96
97
 
97
- t.timestamps
98
+ t.timestamps
99
+ end
98
100
  end
99
101
  end
102
+ FILE
100
103
  end
101
- FILE
102
104
  end
103
105
  end
104
106
  end