easy_gen 1.2.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: da5081ab9fcb9c258551212d713efb95c53fb2a4d2ca36d9c3dbcce0877e8795
4
- data.tar.gz: 9acc796a3b32b21a01221ba75946f07b2dcf8b700fb7477bba151eec55a49f25
3
+ metadata.gz: bf7fdedf390132cd01a86266cf8754bf95eec1dd241e10d4340ffb359c25a1c6
4
+ data.tar.gz: 2ee0232d6cee49802f26a05959c2cef19b9f5f398fed2c171ba4219bfef3d5eb
5
5
  SHA512:
6
- metadata.gz: 4960a5de4a1f2cc4e907f10f8521555d0d16c22c92ca69676b7becef5ac9e54bb7145c5e74d1fb1e9af2fc193e36d10cb9f35f484729fbb52e2e99a6c75400bd
7
- data.tar.gz: a934ff6004b73cede4d86cfa5f8fcbfd9e4e02f127f4fcdf64e0f19e5b862315a45b8aaa9d2832c99a1e8052362bb64fe65eaa154a4439165f7afbcbdec3c0a0
6
+ metadata.gz: 10fe25a13542af70057797ef83e98f1535a926d2f88c5552c9d6c33e1a35b6b9c6a6ce359006799816aa8a3aa884bf7e2cbec1f2f88455fe5c98192435c7f7cf
7
+ data.tar.gz: 517e0f682f27cb98a763a0389a942bd393af72acbb52ec264f26079f1ba9d34825dfb3deb68776c584002c122a019285dc932f0da26c488a63e3879caed9fba8
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
- # easy_gen : Simple generator to add service class and minitest to rails projects
1
+ ![gem version badge](https://badge.fury.io/rb/easy_gen.svg)
2
2
 
3
- Save typing when generating service objects in a standard pattern.
3
+ # easy_gen : Simple generator to add various classes with barebones minitest to rails projects
4
+
5
+ Save typing when generating certain classes in somewhat standard patterns.
6
+
7
+ ## Installation
4
8
 
5
9
  Add the following to the development group in your Gemfile:
6
10
 
@@ -18,7 +22,8 @@ In the usual way.
18
22
 
19
23
  ## Using generators
20
24
 
21
- #Service Object Classes (See this link for example usage: https://www.honeybadger.io/blog/refactor-ruby-rails-service-object/):
25
+ ### Service Object Classes
26
+ (See this link for example usage: https://www.honeybadger.io/blog/refactor-ruby-rails-service-object/):
22
27
 
23
28
  ```sh
24
29
  bundle exec rails g service serviceclassname
@@ -32,7 +37,20 @@ The command above:
32
37
  - Creates '/test/services' directory if it doesnt exist.
33
38
  - Installs new test class in '/test/services' with the name 'ServiceClassNameTest' in the file '/test/services/service_class_name_test.rb'.
34
39
 
35
- #Null Object Classes (See this link for typical usage: https://medium.com/@kelseydh/using-the-null-object-pattern-with-ruby-on-rails-b645ebf79785 ):
40
+ optionally run with the module option to embed within a module namespace. For example:
41
+
42
+ ```sh
43
+ bundle exec rails g service serviceclassname --module cool
44
+ ```
45
+
46
+ - Creates '/app/services/cool' directory if it doesnt exist.
47
+ - Installs new application service class in '/app/services' with the name 'ApplicationService' in file '/app/services/application_service.rb.'
48
+ - Installs new service class in '/app/services/cool' with the class name 'Cool::ServiceClassName' in the file /app/services/cool/service_class_name.rb. This will inherit from /app/services/application_services.rb.'
49
+ - Creates '/test/services/cool' directory if it doesnt exist.
50
+ - Installs new test class in '/test/services/cool' with the name 'ServiceClassNameTest' in the file '/test/services/cool/service_class_name_test.rb'.
51
+
52
+ ### Null Object Classes
53
+ (See this link for typical usage: https://medium.com/@kelseydh/using-the-null-object-pattern-with-ruby-on-rails-b645ebf79785 ):
36
54
 
37
55
  ```sh
38
56
  bundle exec rails g null modelname
@@ -44,9 +62,12 @@ The command above:
44
62
  - Installs new application domain class in '/app/domain' with the name 'ApplicationNull' in file '/app/domain/application_null.rb.'
45
63
  - Installs new domain class in '/app/domain' with the class name 'ModelnameNull' in the file /app/domain/modelname_null.rb. This will inherit from /app/domain/application_null.rb.'
46
64
  - Creates '/test/domain' directory if it doesnt exist.
47
- - Installs new test class in '/test/domain' with the name 'ModelnameNullTest' in the file '/test/domain/modelname_null_test.rb'.
65
+ - Installs new test class in '/test/domain' with the name 'ModelnameNullTest' in the file '/test/domain/modelname_null_test.rb'. Tests include 'stubbing' of all model 'columns' based on data type, with assertions (coverage baby).
66
+
67
+ Module option is also supported.
48
68
 
49
- #Decorator Classes (See this link for typical usage: https://www.thegreatcodeadventure.com/rails-refactoring-part-iii-the-decorator-pattern/)
69
+ ### Decorator Classes
70
+ (See this link for typical usage: https://www.thegreatcodeadventure.com/rails-refactoring-part-iii-the-decorator-pattern/)
50
71
 
51
72
  ```sh
52
73
  bundle exec rails g decorator modelname
@@ -61,5 +82,7 @@ The command above:
61
82
  - Installs new test class in '/test/decorators' with the name 'ModelnameDecoratorTest' in the file '/test/decorators/modelname_decorator_test.rb'.
62
83
 
63
84
 
85
+ Module option is also supported.
64
86
 
87
+ ## Summary
65
88
  ** Nothing clever - just saves a bit of typing
@@ -4,7 +4,7 @@ require 'fileutils'
4
4
  template_dir = File.expand_path('templates', __dir__)
5
5
 
6
6
  class DecoratorGenerator < Rails::Generators::NamedBase
7
- include AbstractGenerator
7
+ include EasyGenGenerator
8
8
 
9
9
  # bundle exec rails g service MyService
10
10
  # bundle exec rails d service MyService
@@ -15,6 +15,7 @@ class DecoratorGenerator < Rails::Generators::NamedBase
15
15
  source_root File.expand_path("templates", __dir__)
16
16
 
17
17
  argument :model, type: :string, default: "ERROR", banner: "model"
18
+ class_option :module, type: :string
18
19
 
19
20
  def main
20
21
  raise "No such model - please check naming" unless model_exists?
@@ -1,6 +1,6 @@
1
- class <%= class_name %>Decorator < ApplicationDecorator
1
+ class <%= module_prefix %><%= class_name %>Decorator < ApplicationDecorator
2
2
 
3
3
  # Expects to be created from <%= class_name %> like this:
4
- # <%= class_name %>Decorator.new(@my_<%= class_name.downcase %>)
4
+ # <%= module_prefix %><%= class_name %>Decorator.new(@my_<%= class_name.downcase %>)
5
5
 
6
6
  end
@@ -2,7 +2,7 @@ require "test_helper"
2
2
 
3
3
  class <%= class_name %>DecoratorTest < ActiveSupport::TestCase
4
4
  setup do
5
- @decorator = <%= class_name %>Decorator.new(<%= class_name%>.first)
5
+ @decorator = <%= module_prefix %><%= class_name %>Decorator.new(<%= class_name%>.first)
6
6
  end
7
7
 
8
8
  end
@@ -0,0 +1,89 @@
1
+ require 'rails/generators'
2
+ require 'fileutils'
3
+
4
+ module EasyGenGenerator
5
+
6
+ def copy_templates
7
+ unless File.exist? "app/#{base_location}/application_#{generator_type}.rb"
8
+ template "application_#{generator_type}.rb", "app/#{base_location}/application_#{generator_type}.rb"
9
+ end
10
+ template "#{generator_type}.rb", "app/#{location}/#{file_path}_#{generator_type}.rb"
11
+ template "#{generator_type}_test.rb", "test/#{location}/#{file_path}_#{generator_type}_test.rb"
12
+
13
+ if no_files?
14
+ teardown ['app', 'test']
15
+ end
16
+
17
+ if no_files_for_module?
18
+ teardown ['app', 'test']
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def module?
25
+ options[:module].present?
26
+ end
27
+
28
+ def module_name
29
+ @module_name ||= module? ? options[:module] : ""
30
+ end
31
+
32
+ def module_prefix
33
+ @module_prefix ||= module? ? "#{module_name.camelize}::" : ""
34
+ end
35
+
36
+ def module_dir
37
+ @module_dir ||= module? ? "/#{module_name.downcase}" : ""
38
+ end
39
+
40
+ def base_location
41
+ @base_location ||= self.class::LOCATION
42
+ end
43
+
44
+ def location
45
+ @location ||= base_location + module_dir
46
+ end
47
+
48
+ def generator_type
49
+ @generator_type ||= self.class::TYPE
50
+ end
51
+
52
+ def no_files?
53
+ only_one_file? && file_is_abstract_class? && no_tests?
54
+ end
55
+
56
+ def only_one_file?
57
+ Dir["./app/#{base_location}/**/*"].reject { | path | File.directory?(path) }.length == 1
58
+ end
59
+
60
+ def file_is_abstract_class?
61
+ File.exist?("app/#{base_location}/application_#{generator_type}.rb")
62
+ end
63
+
64
+ def no_tests?
65
+ Dir["./test/#{base_location}/**/*"].reject { | path | File.directory?(path) }.length == 0
66
+ end
67
+
68
+ def no_files_for_module?
69
+ no_app_files_for_module? && no_test_files_for_module?
70
+ end
71
+
72
+ def no_test_files_for_module?
73
+ Dir["./test/#{location}/**/*"].reject { | path | File.directory?(path) }.length == 0
74
+ end
75
+
76
+ def no_app_files_for_module?
77
+ Dir["./app/#{location}/**/*"].reject { | path | File.directory?(path) }.length == 0
78
+ end
79
+
80
+ def teardown(places)
81
+ print "Removing directories: "
82
+ places.each do | place |
83
+ print place + '/' + location + " "
84
+ FileUtils.rm_rf(Rails.root.join(place,"#{location}"))
85
+ end
86
+ puts "- done"
87
+ end
88
+
89
+ end
@@ -4,7 +4,7 @@ require 'fileutils'
4
4
  template_dir = File.expand_path('templates', __dir__)
5
5
 
6
6
  class NullGenerator < Rails::Generators::NamedBase
7
- include AbstractGenerator
7
+ include EasyGenGenerator
8
8
  # bundle exec rails g null MyModel
9
9
  # bundle exec rails d null MyModel
10
10
 
@@ -26,6 +26,7 @@ class NullGenerator < Rails::Generators::NamedBase
26
26
  source_root File.expand_path("templates", __dir__)
27
27
 
28
28
  argument :model, type: :string, default: "ERROR", banner: "model"
29
+ class_option :module, type: :string
29
30
 
30
31
  def main
31
32
  raise "No such model - please check naming" unless model_exists?
@@ -1,4 +1,4 @@
1
- class <%= class_name %>Null < ApplicationNull
1
+ class <%= module_prefix %><%= class_name %>Null < ApplicationNull
2
2
 
3
3
  # Generated fake attributes
4
4
  <% clazz_columns.each do |column, ar_column| %>
@@ -1,8 +1,8 @@
1
1
  require "test_helper"
2
2
 
3
- class <%= class_name %>NullTest < ActiveSupport::TestCase
3
+ class <%= module_prefix %><%= class_name %>NullTest < ActiveSupport::TestCase
4
4
  setup do
5
- @null = <%= class_name %>Null.new
5
+ @null = <%= module_prefix %><%= class_name %>Null.new
6
6
  end
7
7
 
8
8
  test 'id raises' do
@@ -4,7 +4,7 @@ require 'fileutils'
4
4
  template_dir = File.expand_path('templates', __dir__)
5
5
 
6
6
  class ServiceGenerator < Rails::Generators::NamedBase
7
- include AbstractGenerator
7
+ include EasyGenGenerator
8
8
 
9
9
  # bundle exec rails g service MyService
10
10
  # bundle exec rails d service MyService
@@ -14,6 +14,8 @@ class ServiceGenerator < Rails::Generators::NamedBase
14
14
 
15
15
  source_root File.expand_path("templates", __dir__)
16
16
 
17
+ class_option :module, type: :string
18
+
17
19
  def main
18
20
  copy_templates
19
21
  end
@@ -1,4 +1,4 @@
1
- class <%= class_name %>Service < ApplicationService
1
+ class <%= module_prefix %><%= class_name %>Service < ApplicationService
2
2
 
3
3
  def initialize
4
4
  end
@@ -1,3 +1,3 @@
1
1
  module EasyGen
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.1"
3
3
  end
data/lib/easy_gen.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require "easy_gen/version"
2
- require "easy_gen/abstract_generator"
2
+ require "easy_gen/easy_gen_generator"
3
3
  require 'easy_gen/railtie'
4
4
 
5
5
  module EasyGen
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_gen
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Stearn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-21 00:00:00.000000000 Z
11
+ date: 2023-03-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple generator for /app/services classes and matching minitests
14
14
  email: simonstearn@gmail.com
@@ -19,12 +19,12 @@ files:
19
19
  - LICENSE.md
20
20
  - README.md
21
21
  - lib/easy_gen.rb
22
- - lib/easy_gen/abstract_generator.rb
23
22
  - lib/easy_gen/decorator/USAGE
24
23
  - lib/easy_gen/decorator/decorator_generator.rb
25
24
  - lib/easy_gen/decorator/templates/application_decorator.rb.tt
26
25
  - lib/easy_gen/decorator/templates/decorator.rb.tt
27
26
  - lib/easy_gen/decorator/templates/decorator_test.rb.tt
27
+ - lib/easy_gen/easy_gen_generator.rb
28
28
  - lib/easy_gen/null/USAGE
29
29
  - lib/easy_gen/null/null_generator.rb
30
30
  - lib/easy_gen/null/templates/application_null.rb.tt
@@ -1,41 +0,0 @@
1
- require 'rails/generators'
2
- require 'fileutils'
3
-
4
- module AbstractGenerator
5
-
6
- def copy_templates
7
- unless File.exist? "app/#{location}/application_#{generator_type}.rb"
8
- template "application_#{generator_type}.rb", "app/#{location}/application_#{generator_type}.rb"
9
- end
10
- template "#{generator_type}.rb", "app/#{location}/#{file_path}_#{generator_type}.rb"
11
- template "#{generator_type}_test.rb", "test/#{location}/#{file_path}_#{generator_type}_test.rb"
12
-
13
- if no_files?
14
- teardown ['app', 'test']
15
- end
16
- end
17
-
18
- private
19
-
20
- def location
21
- self.class::LOCATION
22
- end
23
-
24
- def generator_type
25
- self.class::TYPE
26
- end
27
-
28
- def no_files?
29
- Dir["./app/#{location}/*"].length == 1 && File.exist?("app/#{location}/application_#{generator_type}.rb") && Dir["./test/#{location}/*"].length == 0
30
- end
31
-
32
- def teardown(places)
33
- print "Removing:"
34
- places.each do | place |
35
- print place + " "
36
- FileUtils.rm_rf(Rails.root.join(place,"#{location}"))
37
- end
38
- puts "- done"
39
- end
40
-
41
- end