easy_gen 1.0.1 → 1.1.1

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: 73f1ce33e410e6ad3997a84d409cbebf17f67a1b6d29c65aa98e2b7266f3aa67
4
- data.tar.gz: 533bd5fca3036da27a8a2c02f64069a1afbe2ebdcc5bbfa255584cf58eccdb9e
3
+ metadata.gz: 78c3340c938e79d367a536ce3baa166f48f29ab4841248f684365326c984e38a
4
+ data.tar.gz: a7bb4d826a15b058dd4e1b3d4f4a4f0c1e266496e4176d12780ed05e48bb5f58
5
5
  SHA512:
6
- metadata.gz: e9c780b6c6f9ffa8b58cf1a7caa1ee12c2d0877327ad85347981c320c836c70a9802b2626ea5ec332b2f9086eb67f50340d9a0f1aa0f5323b5e71f5fb1985240
7
- data.tar.gz: 4fda618be443a171c34bd6209a5723ac074bc1d4b861c647de96345b83bf4742ff86aa88e2115152a236f78240dce557af23ed988cd827ed3554296678cb46b7
6
+ metadata.gz: 445c0290345dae4a65c8f8d13b1255cce639074d909da9b15bfdf23100e8785137caaadf4210f81e47d3b0e2933170ad5db219b4892dd20dae867399d16a3194
7
+ data.tar.gz: 231e436f75e3e287316991dc9095f644d37710f138251a091865e4d1edc5a56404b786463465252d31a77a9bcc63d62e0247434307b00f3f037e5c039e8fb5f8
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,18 +18,33 @@ In the usual way.
18
18
 
19
19
  ## Using generators
20
20
 
21
- All you need to do is to run:
21
+ #Service Objects (See this link for example usage: https://www.honeybadger.io/blog/refactor-ruby-rails-service-object/):
22
22
 
23
23
  ```sh
24
- bundle exec rails g service ServiceClassName
24
+ bundle exec rails g service serviceclassname
25
25
  ```
26
26
 
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 'ModelnameNull' in the file /app/domain/modelname_null.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 'ModelnameNullTest' in the file '/test/domain/modelname_null_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
@@ -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
- def copy_templates
17
- unless File.exist? "app/services/application_service.rb"
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
- if no_services?
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 no_services?
32
- Dir["./app/services/*"].length == 1 && File.exist?("app/services/application_service.rb") && Dir["./test/services/*"].length == 0
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
@@ -1,3 +1,3 @@
1
1
  module EasyGen
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.1"
3
3
  end
data/lib/easy_gen.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "easy_gen/version"
2
+ require "easy_gen/abstract_generator"
2
3
  require 'easy_gen/railtie'
3
4
 
4
5
  module EasyGen
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.1
4
+ version: 1.1.1
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
@@ -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