easy_gen 1.0.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e2d07dc8b6f85a2b78e8c91b2d89fa4cd6fcf832e3d138dc8ca3e5c9de18b832
4
- data.tar.gz: 8c316ff1f599fbaa893e2360c25aab5cd1ef5266cf5668bfcf07075781a5f13e
3
+ metadata.gz: 95fcbb1b48e4b5858a5854dadc9fe36ba71f9f3398ca4af37d1c2ca3034ca59d
4
+ data.tar.gz: 99967e9153f72da95d478f89588c9a146632fd4620774bad171839dd327e3062
5
5
  SHA512:
6
- metadata.gz: 49e6f40b59eb2def1cf616009b88926383b53a213f60e17636559e0ca055ef23d7d2d234ce4832c815be0feb02e5cdaf69560d43b2d6c8e096a98e16e297dcfc
7
- data.tar.gz: 1b489b8a5d176975a4dfc2ae3737ab82e58a57eb49061f25bdfdd525927618240b3849cba596658bf658e0c74dbbaa9440fd3b858a6796c2ab05fee5ac861428
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
- All you need to do is to run:
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.rb' in file '/app/services/application_service.rb.'
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 '/app/services' with the name 'ServiceClassNameTest' in the file '/test/services/service_class_name_test.rb'.
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,10 @@
1
+ class <%= class_name %>Null < ApplicationNull
2
+
3
+ # Generated fake attributes
4
+ <% clazz_columns.each do |column, ar_column| %>
5
+ <%= "def #{column}" %>
6
+ <%= " return #{default_value(ar_column.type)}" %>
7
+ <%= "end" %>
8
+ <% end %>
9
+
10
+ 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
@@ -0,0 +1,25 @@
1
+ require 'pathname'
2
+
3
+ module EasyGen
4
+ class EasyGenRailtie < Rails::Railtie
5
+
6
+ initializer "easy_gen_railtie.configure_generators" do
7
+
8
+ generator_dirs = Pathname(File.dirname(__dir__) + '/easy_gen/').children.select(&:directory?)
9
+
10
+
11
+ @@generator_files = []
12
+ generator_dirs.each do | dir |
13
+ @@generator_files << Dir["#{dir}/*_generator.rb"]
14
+ end
15
+ @@generator_files.flatten!
16
+
17
+ end
18
+
19
+ generators do
20
+ @@generator_files.each do | file |
21
+ require file
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ Description:
2
+ Lays out a new service object under /app/services, with a test file under /test/services.
3
+ The service class will inherit from /app/service/application_service class which will be created.
4
+ if you use the generator to delete service classes, these will be removed together with directories once
5
+ the count of non abstract classes == 0.
6
+
7
+ Example:
8
+ `rails generate service MyService`
9
+
10
+ This will create:
11
+ app/services/application_service.rb
12
+ app/services/my_service.rb
13
+ test/services/my_service_test.rb
14
+
@@ -0,0 +1,21 @@
1
+ require 'rails/generators'
2
+ require 'fileutils'
3
+
4
+ template_dir = File.expand_path('templates', __dir__)
5
+
6
+ class ServiceGenerator < Rails::Generators::NamedBase
7
+ include AbstractGenerator
8
+
9
+ # bundle exec rails g service MyService
10
+ # bundle exec rails d service MyService
11
+
12
+ LOCATION = "services"
13
+ TYPE = "service"
14
+
15
+ source_root File.expand_path("templates", __dir__)
16
+
17
+ def main
18
+ copy_templates
19
+ end
20
+
21
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationService
2
+
3
+ end
@@ -0,0 +1,9 @@
1
+ class <%= class_name %>Service < ApplicationService
2
+
3
+ def initialize
4
+ end
5
+
6
+ def call
7
+ end
8
+
9
+ end
@@ -0,0 +1,7 @@
1
+ require "test_helper"
2
+
3
+ class <%= class_name %>ServiceTest < ActiveSupport::TestCase
4
+ setup do
5
+ end
6
+
7
+ end
@@ -0,0 +1,3 @@
1
+ module EasyGen
2
+ VERSION = "1.1.0"
3
+ end
data/lib/easy_gen.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "easy_gen/version"
2
+ require "easy_gen/abstract_generator"
3
+ require 'easy_gen/railtie'
4
+
5
+ module EasyGen
6
+ end
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.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-20 00:00:00.000000000 Z
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
@@ -18,6 +18,20 @@ extra_rdoc_files: []
18
18
  files:
19
19
  - LICENSE.md
20
20
  - README.md
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
28
+ - lib/easy_gen/railtie.rb
29
+ - lib/easy_gen/service/USAGE
30
+ - lib/easy_gen/service/service_generator.rb
31
+ - lib/easy_gen/service/templates/application_service.rb.tt
32
+ - lib/easy_gen/service/templates/service.rb.tt
33
+ - lib/easy_gen/service/templates/service_test.rb.tt
34
+ - lib/easy_gen/version.rb
21
35
  homepage: https://rubygems.org/gems/easy_gen
22
36
  licenses:
23
37
  - MIT