easy_gen 1.2.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: da5081ab9fcb9c258551212d713efb95c53fb2a4d2ca36d9c3dbcce0877e8795
4
- data.tar.gz: 9acc796a3b32b21a01221ba75946f07b2dcf8b700fb7477bba151eec55a49f25
3
+ metadata.gz: 84e5d8b3dd221105fc7faee88424a7449da87521eaf72b53a5c9a92a7aad079d
4
+ data.tar.gz: efeae962cbe6e7f33c1a6778c3b525154e3a5c59622ba024432b107ec5ca3a24
5
5
  SHA512:
6
- metadata.gz: 4960a5de4a1f2cc4e907f10f8521555d0d16c22c92ca69676b7becef5ac9e54bb7145c5e74d1fb1e9af2fc193e36d10cb9f35f484729fbb52e2e99a6c75400bd
7
- data.tar.gz: a934ff6004b73cede4d86cfa5f8fcbfd9e4e02f127f4fcdf64e0f19e5b862315a45b8aaa9d2832c99a1e8052362bb64fe65eaa154a4439165f7afbcbdec3c0a0
6
+ metadata.gz: 5c0d9d64439a1ec66a43760ca9810de56247ae7d9800cd32a2fe9d7fac8f0e186e3c8f538b55f40d49c125ddf9b988e5f3f90693532b21d23439578d870ebe02
7
+ data.tar.gz: 18995f5c9dfde49f9d252ecf83d5f40559f22200f7d0b570ab9f9576e9b92d9e6bce7513399c113e41e58690f51a7dd45b2603302b0299dfb5e0cb6a88a20067
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
- # easy_gen : Simple generator to add service class and minitest to rails projects
1
+ ![gem version badge](https://badge.fury.io/rb/easy_gen.svg)
2
2
 
3
- Save typing when generating service objects in a standard pattern.
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
- #Service Object Classes (See this link for example usage: https://www.honeybadger.io/blog/refactor-ruby-rails-service-object/):
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
- #Null Object Classes (See this link for typical usage: https://medium.com/@kelseydh/using-the-null-object-pattern-with-ruby-on-rails-b645ebf79785 ):
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,9 +62,12 @@ 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.
48
68
 
49
- #Decorator Classes (See this link for typical usage: https://www.thegreatcodeadventure.com/rails-refactoring-part-iii-the-decorator-pattern/)
69
+ ### Decorator Classes
70
+ (See this link for typical usage: https://www.thegreatcodeadventure.com/rails-refactoring-part-iii-the-decorator-pattern/)
50
71
 
51
72
  ```sh
52
73
  bundle exec rails g decorator modelname
@@ -61,5 +82,7 @@ The command above:
61
82
  - Installs new test class in '/test/decorators' with the name 'ModelnameDecoratorTest' in the file '/test/decorators/modelname_decorator_test.rb'.
62
83
 
63
84
 
85
+ Module option is also supported.
64
86
 
87
+ ## Summary
65
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/#{location}/application_#{generator_type}.rb"
8
- template "application_#{generator_type}.rb", "app/#{location}/application_#{generator_type}.rb"
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
- self.class::LOCATION
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
- Dir["./app/#{location}/*"].length == 1 && File.exist?("app/#{location}/application_#{generator_type}.rb") && Dir["./test/#{location}/*"].length == 0
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"
@@ -15,6 +15,7 @@ class DecoratorGenerator < Rails::Generators::NamedBase
15
15
  source_root File.expand_path("templates", __dir__)
16
16
 
17
17
  argument :model, type: :string, default: "ERROR", banner: "model"
18
+ class_option :module, type: :string
18
19
 
19
20
  def main
20
21
  raise "No such model - please check naming" unless model_exists?
@@ -1,6 +1,6 @@
1
- class <%= class_name %>Decorator < ApplicationDecorator
1
+ class <%= module_prefix %><%= class_name %>Decorator < ApplicationDecorator
2
2
 
3
3
  # Expects to be created from <%= class_name %> like this:
4
- # <%= class_name %>Decorator.new(@my_<%= class_name.downcase %>)
4
+ # <%= module_prefix %><%= class_name %>Decorator.new(@my_<%= class_name.downcase %>)
5
5
 
6
6
  end
@@ -2,7 +2,7 @@ require "test_helper"
2
2
 
3
3
  class <%= class_name %>DecoratorTest < ActiveSupport::TestCase
4
4
  setup do
5
- @decorator = <%= class_name %>Decorator.new(<%= class_name%>.first)
5
+ @decorator = <%= module_prefix %><%= class_name %>Decorator.new(<%= class_name%>.first)
6
6
  end
7
7
 
8
8
  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,4 +1,4 @@
1
- class <%= class_name %>Null < ApplicationNull
1
+ class <%= module_prefix %><%= class_name %>Null < ApplicationNull
2
2
 
3
3
  # Generated fake attributes
4
4
  <% clazz_columns.each do |column, ar_column| %>
@@ -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
@@ -14,6 +14,8 @@ class ServiceGenerator < Rails::Generators::NamedBase
14
14
 
15
15
  source_root File.expand_path("templates", __dir__)
16
16
 
17
+ class_option :module, type: :string
18
+
17
19
  def main
18
20
  copy_templates
19
21
  end
@@ -1,4 +1,4 @@
1
- class <%= class_name %>Service < ApplicationService
1
+ class <%= module_prefix %><%= class_name %>Service < ApplicationService
2
2
 
3
3
  def initialize
4
4
  end
@@ -1,3 +1,3 @@
1
1
  module EasyGen
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
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.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Stearn