easy_gen 1.0.1 → 1.1.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 +19 -4
- data/lib/easy_gen/abstract_generator.rb +41 -0
- data/lib/easy_gen/null/USAGE +14 -0
- data/lib/easy_gen/null/null_generator.rb +51 -0
- data/lib/easy_gen/null/templates/application_null.rb.tt +35 -0
- data/lib/easy_gen/null/templates/null.rb.tt +10 -0
- data/lib/easy_gen/null/templates/null_test.rb.tt +57 -0
- data/lib/easy_gen/railtie.rb +0 -3
- data/lib/easy_gen/service/service_generator.rb +7 -24
- data/lib/easy_gen/version.rb +1 -1
- data/lib/easy_gen.rb +1 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 95fcbb1b48e4b5858a5854dadc9fe36ba71f9f3398ca4af37d1c2ca3034ca59d
|
4
|
+
data.tar.gz: 99967e9153f72da95d478f89588c9a146632fd4620774bad171839dd327e3062
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b22fa93da43aa81936bd2d697b2019b951ef391d2baad23ba9c3144d893459af4fbb2f0cd24f770f2b3a63753428aa41d7f4c6bdaeb44f6ca31a92cd27667ac
|
7
|
+
data.tar.gz: 1a9b12cd25981af1c3db68fef2f6e0fb1ef7179bc6b33cefe02e3cfbba7128f19b7bc75a5cb7667694c2c07dce0a00ac4aad4843d8785b0f95f579d727325351
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Save typing when generating service objects in a standard pattern.
|
4
4
|
|
5
|
-
Add the following to your Gemfile:
|
5
|
+
Add the following to the development group in your Gemfile:
|
6
6
|
|
7
7
|
```ruby
|
8
8
|
gem "easy_gen"
|
@@ -18,7 +18,7 @@ In the usual way.
|
|
18
18
|
|
19
19
|
## Using generators
|
20
20
|
|
21
|
-
|
21
|
+
#Service Objects:
|
22
22
|
|
23
23
|
```sh
|
24
24
|
bundle exec rails g service ServiceClassName
|
@@ -27,9 +27,24 @@ bundle exec rails g service ServiceClassName
|
|
27
27
|
The command above:
|
28
28
|
|
29
29
|
- Creates '/app/services' directory if it doesnt exist.
|
30
|
-
- Installs new application service class in '/app/services' with the name 'ApplicationService
|
30
|
+
- Installs new application service class in '/app/services' with the name 'ApplicationService' in file '/app/services/application_service.rb.'
|
31
31
|
- Installs new service class in '/app/services' with the class name 'ServiceClassName' in the file /app/services/service_class_name.rb. This will inherit from /app/services/application_services.rb.'
|
32
32
|
- Creates '/test/services' directory if it doesnt exist.
|
33
|
-
- Installs new test class in '/
|
33
|
+
- Installs new test class in '/test/services' with the name 'ServiceClassNameTest' in the file '/test/services/service_class_name_test.rb'.
|
34
|
+
|
35
|
+
#Null Objects (See this link for typical usage https://medium.com/@kelseydh/using-the-null-object-pattern-with-ruby-on-rails-b645ebf79785 ):
|
36
|
+
|
37
|
+
```sh
|
38
|
+
bundle exec rails g null modelname
|
39
|
+
```
|
40
|
+
|
41
|
+
The command above:
|
42
|
+
|
43
|
+
- Creates '/app/domain' directory if it doesnt exist.
|
44
|
+
- Installs new application domain class in '/app/domain' with the name 'ApplicationNull' in file '/app/domain/application_null.rb.'
|
45
|
+
- Installs new domain class in '/app/domain' with the class name 'NullModelname' in the file /app/domain/null_modelname.rb. This will inherit from /app/domain/application_null.rb.'
|
46
|
+
- Creates '/test/domain' directory if it doesnt exist.
|
47
|
+
- Installs new test class in '/test/domain' with the name 'ServiceClassNameTest' in the file '/test/domain/null_modelname_test.rb'.
|
48
|
+
|
34
49
|
|
35
50
|
** Nothing clever - just saves a bit of typing
|
@@ -0,0 +1,41 @@
|
|
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
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Description:
|
2
|
+
Lays out a new null value object under /app/domain, with a test file under /test/domain.
|
3
|
+
The null value class will inherit from /app/domain/application_null class which will be created.
|
4
|
+
if you use the generator to delete null classes, these will be removed together with directories once
|
5
|
+
the count of non abstract classes == 0.
|
6
|
+
|
7
|
+
Example:
|
8
|
+
`rails generate null MyModel`
|
9
|
+
|
10
|
+
This will create:
|
11
|
+
app/domain/application_null.rb
|
12
|
+
app/domain/null_my_model.rb
|
13
|
+
test/domain/null_my_model_test.rb
|
14
|
+
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
template_dir = File.expand_path('templates', __dir__)
|
5
|
+
|
6
|
+
class NullGenerator < Rails::Generators::NamedBase
|
7
|
+
include AbstractGenerator
|
8
|
+
# bundle exec rails g null MyModel
|
9
|
+
# bundle exec rails d null MyModel
|
10
|
+
|
11
|
+
LOCATION = "domain"
|
12
|
+
TYPE = "null"
|
13
|
+
|
14
|
+
AR_DEFAULTS = {integer: 0,
|
15
|
+
string: '"No Value"',
|
16
|
+
text: '"No Value"',
|
17
|
+
float: 0.0,
|
18
|
+
decimal: 0.0,
|
19
|
+
binary: false,
|
20
|
+
boolean: false,
|
21
|
+
date: 'Date.parse("1 Jan 1970")',
|
22
|
+
datetime: 'Date.parse("1 Jan 1970")',
|
23
|
+
timestamp: 'Date.parse("1 Jan 1970")',
|
24
|
+
time: 'Time.parse("00:00:00")' }
|
25
|
+
|
26
|
+
source_root File.expand_path("templates", __dir__)
|
27
|
+
|
28
|
+
argument :model, type: :string, default: "ERROR", banner: "model"
|
29
|
+
|
30
|
+
def main
|
31
|
+
copy_templates
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def model_name
|
37
|
+
model == "ERROR" ? class_name : model
|
38
|
+
end
|
39
|
+
|
40
|
+
def clazz
|
41
|
+
Kernel.const_get(model_name)
|
42
|
+
end
|
43
|
+
|
44
|
+
def default_value(ar_type)
|
45
|
+
return AR_DEFAULTS[ar_type.to_sym]
|
46
|
+
end
|
47
|
+
|
48
|
+
def clazz_columns
|
49
|
+
clazz.columns_hash.reject { | key, value | key == "id" }
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class ApplicationNull
|
2
|
+
|
3
|
+
def id
|
4
|
+
raise "You are calling id on an object that doesn't exist."
|
5
|
+
end
|
6
|
+
|
7
|
+
def valid?
|
8
|
+
false
|
9
|
+
end
|
10
|
+
|
11
|
+
def present?
|
12
|
+
false
|
13
|
+
end
|
14
|
+
|
15
|
+
def blank?
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
19
|
+
def save
|
20
|
+
raise ActiveRecord::RecordInvalid
|
21
|
+
end
|
22
|
+
|
23
|
+
def save!
|
24
|
+
raise ActiveRecord::RecordInvalid
|
25
|
+
end
|
26
|
+
|
27
|
+
def update
|
28
|
+
raise ActiveRecord::RecordInvalid
|
29
|
+
end
|
30
|
+
|
31
|
+
def update!
|
32
|
+
raise ActiveRecord::RecordInvalid
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class <%= class_name %>NullTest < ActiveSupport::TestCase
|
4
|
+
setup do
|
5
|
+
@null = <%= class_name %>Null.new
|
6
|
+
end
|
7
|
+
|
8
|
+
test 'id raises' do
|
9
|
+
assert_raises RuntimeError do
|
10
|
+
@null.id
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'is invalid' do
|
15
|
+
refute @null.valid?
|
16
|
+
end
|
17
|
+
|
18
|
+
test 'is not present' do
|
19
|
+
refute @null.present?
|
20
|
+
end
|
21
|
+
|
22
|
+
test 'is blank' do
|
23
|
+
assert @null.blank?
|
24
|
+
end
|
25
|
+
|
26
|
+
test 'save raises' do
|
27
|
+
assert_raises ActiveRecord::RecordInvalid do
|
28
|
+
@null.save
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
test 'save! raises' do
|
33
|
+
assert_raises ActiveRecord::RecordInvalid do
|
34
|
+
@null.save!
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
test 'update raises' do
|
39
|
+
assert_raises ActiveRecord::RecordInvalid do
|
40
|
+
@null.update
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
test 'update! raises' do
|
45
|
+
assert_raises ActiveRecord::RecordInvalid do
|
46
|
+
@null.update!
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Generated fake attributes
|
51
|
+
<% clazz_columns.each do |column, ar_column| %>
|
52
|
+
<%= "test '#{column} is #{default_value(ar_column.type)}' do" %>
|
53
|
+
<%= " assert @null.#{column} == #{default_value(ar_column.type)}" %>
|
54
|
+
<%= "end" %>
|
55
|
+
<% end %>
|
56
|
+
|
57
|
+
end
|
data/lib/easy_gen/railtie.rb
CHANGED
@@ -13,15 +13,12 @@ module EasyGen
|
|
13
13
|
@@generator_files << Dir["#{dir}/*_generator.rb"]
|
14
14
|
end
|
15
15
|
@@generator_files.flatten!
|
16
|
-
puts "#{@@generator_files}"
|
17
16
|
|
18
17
|
end
|
19
18
|
|
20
19
|
generators do
|
21
20
|
@@generator_files.each do | file |
|
22
|
-
puts "requiring #{file}"
|
23
21
|
require file
|
24
|
-
puts "Done require"
|
25
22
|
end
|
26
23
|
end
|
27
24
|
end
|
@@ -2,37 +2,20 @@ require 'rails/generators'
|
|
2
2
|
require 'fileutils'
|
3
3
|
|
4
4
|
template_dir = File.expand_path('templates', __dir__)
|
5
|
-
puts "Templates in: #{template_dir}"
|
6
5
|
|
7
6
|
class ServiceGenerator < Rails::Generators::NamedBase
|
7
|
+
include AbstractGenerator
|
8
|
+
|
8
9
|
# bundle exec rails g service MyService
|
9
|
-
# bundle exec rails generate service MyService
|
10
10
|
# bundle exec rails d service MyService
|
11
|
-
# bundle exec rails delete service MyService
|
12
|
-
#
|
13
|
-
|
14
|
-
source_root File.expand_path("templates", __dir__)
|
15
11
|
|
16
|
-
|
17
|
-
|
18
|
-
template "application_service.rb", "app/services/application_service.rb"
|
19
|
-
end
|
20
|
-
template "service.rb", "app/services/#{file_path}_service.rb"
|
21
|
-
template "service_test.rb", "test/services/#{file_path}_service_test.rb"
|
12
|
+
LOCATION = "services"
|
13
|
+
TYPE = "service"
|
22
14
|
|
23
|
-
|
24
|
-
teardown 'app'
|
25
|
-
teardown 'test'
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
15
|
+
source_root File.expand_path("templates", __dir__)
|
30
16
|
|
31
|
-
def
|
32
|
-
|
17
|
+
def main
|
18
|
+
copy_templates
|
33
19
|
end
|
34
20
|
|
35
|
-
def teardown(location)
|
36
|
-
FileUtils.rm_rf(Rails.root.join(location,'services'))
|
37
|
-
end
|
38
21
|
end
|
data/lib/easy_gen/version.rb
CHANGED
data/lib/easy_gen.rb
CHANGED
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.0
|
4
|
+
version: 1.1.0
|
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-
|
11
|
+
date: 2023-03-21 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,6 +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
|
+
- lib/easy_gen/null/USAGE
|
24
|
+
- lib/easy_gen/null/null_generator.rb
|
25
|
+
- lib/easy_gen/null/templates/application_null.rb.tt
|
26
|
+
- lib/easy_gen/null/templates/null.rb.tt
|
27
|
+
- lib/easy_gen/null/templates/null_test.rb.tt
|
22
28
|
- lib/easy_gen/railtie.rb
|
23
29
|
- lib/easy_gen/service/USAGE
|
24
30
|
- lib/easy_gen/service/service_generator.rb
|