easy_gen 1.1.1 → 1.2.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 +17 -2
- data/lib/easy_gen/decorator/USAGE +14 -0
- data/lib/easy_gen/decorator/decorator_generator.rb +37 -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 +8 -0
- 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: da5081ab9fcb9c258551212d713efb95c53fb2a4d2ca36d9c3dbcce0877e8795
|
4
|
+
data.tar.gz: 9acc796a3b32b21a01221ba75946f07b2dcf8b700fb7477bba151eec55a49f25
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4960a5de4a1f2cc4e907f10f8521555d0d16c22c92ca69676b7becef5ac9e54bb7145c5e74d1fb1e9af2fc193e36d10cb9f35f484729fbb52e2e99a6c75400bd
|
7
|
+
data.tar.gz: a934ff6004b73cede4d86cfa5f8fcbfd9e4e02f127f4fcdf64e0f19e5b862315a45b8aaa9d2832c99a1e8052362bb64fe65eaa154a4439165f7afbcbdec3c0a0
|
data/README.md
CHANGED
@@ -18,7 +18,7 @@ In the usual way.
|
|
18
18
|
|
19
19
|
## Using generators
|
20
20
|
|
21
|
-
#Service
|
21
|
+
#Service Object Classes (See this link for example usage: https://www.honeybadger.io/blog/refactor-ruby-rails-service-object/):
|
22
22
|
|
23
23
|
```sh
|
24
24
|
bundle exec rails g service serviceclassname
|
@@ -32,7 +32,7 @@ The command above:
|
|
32
32
|
- Creates '/test/services' directory if it doesnt exist.
|
33
33
|
- Installs new test class in '/test/services' with the name 'ServiceClassNameTest' in the file '/test/services/service_class_name_test.rb'.
|
34
34
|
|
35
|
-
#Null
|
35
|
+
#Null Object Classes (See this link for typical usage: https://medium.com/@kelseydh/using-the-null-object-pattern-with-ruby-on-rails-b645ebf79785 ):
|
36
36
|
|
37
37
|
```sh
|
38
38
|
bundle exec rails g null modelname
|
@@ -46,5 +46,20 @@ The command above:
|
|
46
46
|
- Creates '/test/domain' directory if it doesnt exist.
|
47
47
|
- Installs new test class in '/test/domain' with the name 'ModelnameNullTest' in the file '/test/domain/modelname_null_test.rb'.
|
48
48
|
|
49
|
+
#Decorator Classes (See this link for typical usage: https://www.thegreatcodeadventure.com/rails-refactoring-part-iii-the-decorator-pattern/)
|
50
|
+
|
51
|
+
```sh
|
52
|
+
bundle exec rails g decorator modelname
|
53
|
+
```
|
54
|
+
|
55
|
+
The command above:
|
56
|
+
|
57
|
+
- Creates '/app/decorators' directory if it doesnt exist.
|
58
|
+
- 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.
|
59
|
+
- 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.'
|
60
|
+
- Creates '/test/decorators' directory if it doesnt exist.
|
61
|
+
- Installs new test class in '/test/decorators' with the name 'ModelnameDecoratorTest' in the file '/test/decorators/modelname_decorator_test.rb'.
|
62
|
+
|
63
|
+
|
49
64
|
|
50
65
|
** Nothing clever - just saves a bit of typing
|
@@ -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,37 @@
|
|
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
|
+
|
19
|
+
def main
|
20
|
+
raise "No such model - please check naming" unless model_exists?
|
21
|
+
copy_templates
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def model_exists?
|
27
|
+
files = Dir[Rails.root + 'app/models/*.rb']
|
28
|
+
models = files.map{ |m| File.basename(m, '.rb').camelize }
|
29
|
+
|
30
|
+
models.include? model_name.camelize
|
31
|
+
end
|
32
|
+
|
33
|
+
def model_name
|
34
|
+
model == "ERROR" ? class_name : model
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -28,11 +28,19 @@ class NullGenerator < Rails::Generators::NamedBase
|
|
28
28
|
argument :model, type: :string, default: "ERROR", banner: "model"
|
29
29
|
|
30
30
|
def main
|
31
|
+
raise "No such model - please check naming" unless model_exists?
|
31
32
|
copy_templates
|
32
33
|
end
|
33
34
|
|
34
35
|
private
|
35
36
|
|
37
|
+
def model_exists?
|
38
|
+
files = Dir[Rails.root + 'app/models/*.rb']
|
39
|
+
models = files.map{ |m| File.basename(m, '.rb').camelize }
|
40
|
+
|
41
|
+
models.include? model_name.camelize
|
42
|
+
end
|
43
|
+
|
36
44
|
def model_name
|
37
45
|
model == "ERROR" ? class_name : model
|
38
46
|
end
|
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.2.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
|