easy_gen 1.1.2 → 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 +43 -5
- data/lib/easy_gen/abstract_generator.rb +55 -7
- data/lib/easy_gen/decorator/USAGE +14 -0
- data/lib/easy_gen/decorator/decorator_generator.rb +38 -0
- data/lib/easy_gen/decorator/templates/application_decorator.rb.tt +3 -0
- data/lib/easy_gen/decorator/templates/decorator.rb.tt +6 -0
- data/lib/easy_gen/decorator/templates/decorator_test.rb.tt +8 -0
- 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 +6 -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,7 +62,27 @@ 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.
|
68
|
+
|
69
|
+
### Decorator Classes
|
70
|
+
(See this link for typical usage: https://www.thegreatcodeadventure.com/rails-refactoring-part-iii-the-decorator-pattern/)
|
71
|
+
|
72
|
+
```sh
|
73
|
+
bundle exec rails g decorator modelname
|
74
|
+
```
|
75
|
+
|
76
|
+
The command above:
|
77
|
+
|
78
|
+
- Creates '/app/decorators' directory if it doesnt exist.
|
79
|
+
- Installs new application decorator class in '/app/decorators' with the name 'ApplicationDecorator' in file '/app/decorators/application_decorator.rb.'. This inherits from Rubys SimpleDelegator class.
|
80
|
+
- Installs new decorators class in '/app/decorators' with the class name 'ModelnameDecorator' in the file /app/decorators/modelname_decorator.rb. This will inherit from /app/decorators/application_decorator.rb.'
|
81
|
+
- Creates '/test/decorators' directory if it doesnt exist.
|
82
|
+
- Installs new test class in '/test/decorators' with the name 'ModelnameDecoratorTest' in the file '/test/decorators/modelname_decorator_test.rb'.
|
83
|
+
|
48
84
|
|
85
|
+
Module option is also supported.
|
49
86
|
|
87
|
+
## Summary
|
50
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"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Description:
|
2
|
+
Lays out a new service object under /app/decorators, with a test file under /test/decorators.
|
3
|
+
The service class will inherit from /app/decorators/application_decorator class which will be created.
|
4
|
+
if you use the generator to delete decorator classes, these will be removed together with directories once
|
5
|
+
the count of non abstract classes == 0.
|
6
|
+
|
7
|
+
Example:
|
8
|
+
`rails generate decorator Mydecorator`
|
9
|
+
|
10
|
+
This will create:
|
11
|
+
app/decorators/application_decorator.rb
|
12
|
+
app/decorators/my_decorator.rb
|
13
|
+
test/decorators/my_decorator_test.rb
|
14
|
+
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
template_dir = File.expand_path('templates', __dir__)
|
5
|
+
|
6
|
+
class DecoratorGenerator < Rails::Generators::NamedBase
|
7
|
+
include AbstractGenerator
|
8
|
+
|
9
|
+
# bundle exec rails g service MyService
|
10
|
+
# bundle exec rails d service MyService
|
11
|
+
|
12
|
+
LOCATION = "decorators"
|
13
|
+
TYPE = "decorator"
|
14
|
+
|
15
|
+
source_root File.expand_path("templates", __dir__)
|
16
|
+
|
17
|
+
argument :model, type: :string, default: "ERROR", banner: "model"
|
18
|
+
class_option :module, type: :string
|
19
|
+
|
20
|
+
def main
|
21
|
+
raise "No such model - please check naming" unless model_exists?
|
22
|
+
copy_templates
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def model_exists?
|
28
|
+
files = Dir[Rails.root + 'app/models/*.rb']
|
29
|
+
models = files.map{ |m| File.basename(m, '.rb').camelize }
|
30
|
+
|
31
|
+
models.include? model_name.camelize
|
32
|
+
end
|
33
|
+
|
34
|
+
def model_name
|
35
|
+
model == "ERROR" ? class_name : model
|
36
|
+
end
|
37
|
+
|
38
|
+
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
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_gen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simon Stearn
|
@@ -20,6 +20,11 @@ files:
|
|
20
20
|
- README.md
|
21
21
|
- lib/easy_gen.rb
|
22
22
|
- lib/easy_gen/abstract_generator.rb
|
23
|
+
- lib/easy_gen/decorator/USAGE
|
24
|
+
- lib/easy_gen/decorator/decorator_generator.rb
|
25
|
+
- lib/easy_gen/decorator/templates/application_decorator.rb.tt
|
26
|
+
- lib/easy_gen/decorator/templates/decorator.rb.tt
|
27
|
+
- lib/easy_gen/decorator/templates/decorator_test.rb.tt
|
23
28
|
- lib/easy_gen/null/USAGE
|
24
29
|
- lib/easy_gen/null/null_generator.rb
|
25
30
|
- lib/easy_gen/null/templates/application_null.rb.tt
|