easy_gen 1.2.0 → 1.3.0
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 +4 -4
- data/README.md +29 -6
- data/lib/easy_gen/abstract_generator.rb +55 -7
- data/lib/easy_gen/decorator/decorator_generator.rb +1 -0
- data/lib/easy_gen/decorator/templates/decorator.rb.tt +2 -2
- data/lib/easy_gen/decorator/templates/decorator_test.rb.tt +1 -1
- data/lib/easy_gen/null/null_generator.rb +1 -0
- data/lib/easy_gen/null/templates/null.rb.tt +1 -1
- data/lib/easy_gen/null/templates/null_test.rb.tt +2 -2
- data/lib/easy_gen/service/service_generator.rb +2 -0
- data/lib/easy_gen/service/templates/service.rb.tt +1 -1
- data/lib/easy_gen/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84e5d8b3dd221105fc7faee88424a7449da87521eaf72b53a5c9a92a7aad079d
|
4
|
+
data.tar.gz: efeae962cbe6e7f33c1a6778c3b525154e3a5c59622ba024432b107ec5ca3a24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c0d9d64439a1ec66a43760ca9810de56247ae7d9800cd32a2fe9d7fac8f0e186e3c8f538b55f40d49c125ddf9b988e5f3f90693532b21d23439578d870ebe02
|
7
|
+
data.tar.gz: 18995f5c9dfde49f9d252ecf83d5f40559f22200f7d0b570ab9f9576e9b92d9e6bce7513399c113e41e58690f51a7dd45b2603302b0299dfb5e0cb6a88a20067
|
data/README.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
|
-
|
1
|
+

|
2
2
|
|
3
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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,8 +4,8 @@ require 'fileutils'
|
|
4
4
|
module AbstractGenerator
|
5
5
|
|
6
6
|
def copy_templates
|
7
|
-
unless File.exist? "app/#{
|
8
|
-
template "application_#{generator_type}.rb", "app/#{
|
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
9
|
end
|
10
10
|
template "#{generator_type}.rb", "app/#{location}/#{file_path}_#{generator_type}.rb"
|
11
11
|
template "#{generator_type}_test.rb", "test/#{location}/#{file_path}_#{generator_type}_test.rb"
|
@@ -13,26 +13,74 @@ module AbstractGenerator
|
|
13
13
|
if no_files?
|
14
14
|
teardown ['app', 'test']
|
15
15
|
end
|
16
|
+
|
17
|
+
if no_files_for_module?
|
18
|
+
teardown ['app', 'test']
|
19
|
+
end
|
16
20
|
end
|
17
21
|
|
18
22
|
private
|
19
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
|
+
|
20
44
|
def location
|
21
|
-
|
45
|
+
@location ||= base_location + module_dir
|
22
46
|
end
|
23
47
|
|
24
48
|
def generator_type
|
25
|
-
self.class::TYPE
|
49
|
+
@generator_type ||= self.class::TYPE
|
26
50
|
end
|
27
51
|
|
28
52
|
def no_files?
|
29
|
-
|
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
|
30
78
|
end
|
31
79
|
|
32
80
|
def teardown(places)
|
33
|
-
print "Removing:"
|
81
|
+
print "Removing directories: "
|
34
82
|
places.each do | place |
|
35
|
-
print place + " "
|
83
|
+
print place + '/' + location + " "
|
36
84
|
FileUtils.rm_rf(Rails.root.join(place,"#{location}"))
|
37
85
|
end
|
38
86
|
puts "- done"
|
@@ -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
|
@@ -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,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
|
data/lib/easy_gen/version.rb
CHANGED