easy_gen 1.4.0 → 1.5.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: '039f3a56535913793c5650392d33a758ed535b5408d3378d0333931d8d4ad7c2'
4
- data.tar.gz: 88b749acf53e01644f352b29c70f3313c46461d964121c2b4c61fa59deaf4de5
3
+ metadata.gz: afdc2c7d450b407a19e0b5a82f98940bd963e5c2834d60e21779dfd9d13d957b
4
+ data.tar.gz: 4fc94179b298f2d5fcf1d86616ff0a97f305f4a0f44251d7a5624087fa5e7969
5
5
  SHA512:
6
- metadata.gz: 3e5bc98279170412cc346fb9c0e3cdd0b2b73bd53411bbb767ddc0bafcd613db800ad73735b0dc3ba4848f9601603f4ff04ecbecf1d98821a58d1290a99af3d3
7
- data.tar.gz: 94ed33ad98ae628b1d7b8b471fe0ce75c9603f676861003f83722f8c2a6ba740ee300ba379b5513dedda24fe2a2dc608447d3e46c50dd7de50ad492fbf1b6637
6
+ metadata.gz: 30a98e94af6ca028739c7e19ae5eb9e2c830faf35ad2ba55627317cf7ea52dd4e4f7d772ded27626db44771e06c5dc55e9000e0d8a8d98a5e474418a9b6f1084
7
+ data.tar.gz: eb49eaf5177912c6f5820797523b437435c2c8cf9f36c2d1577199956da1b8e2bf95847f15bf76037d42e47c3d375f536c595b255ec13d3c4884b00b2d6875b1
data/README.md CHANGED
@@ -22,7 +22,7 @@ In the usual way.
22
22
 
23
23
  ## Using generators
24
24
 
25
- ### Service Object Classes
25
+ ### Service Classes
26
26
  (See this link for example usage: https://www.honeybadger.io/blog/refactor-ruby-rails-service-object/):
27
27
 
28
28
  ```sh
@@ -49,6 +49,23 @@ bundle exec rails g service serviceclassname --module cool
49
49
  - Creates '/test/services/cool' directory if it doesnt exist.
50
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
51
 
52
+ ### Validator Classes
53
+ (See this link for example usage: https://womanonrails.com/custom-rails-validators ):
54
+
55
+ ```sh
56
+ bundle exec rails g validator target
57
+ ```
58
+
59
+ The command above:
60
+
61
+ - Creates '/app/validators' directory if it doesnt exist.
62
+ - Installs new application validator class in '/app/validator' with the name 'ApplicationValidator' in file '/app/services/application_validator.rb.' which inherits from ActiveModel::EachValidator.
63
+ - Installs new validator class in '/app/validators' with the class name 'TargetValidator' in the file /app/validators/target_validator.rb. This will inherit from /app/services/application_validator.rb.'
64
+ - Creates '/test/validators' directory if it doesnt exist.
65
+ - Installs new test class in '/test/validators' with the name 'TargetValidatorTest' in the file '/test/validators/target_validator_test.rb'.
66
+
67
+ Yeah I know there are other types of validators - pull requests gratefully received. I have only ever used EachValidators, and them many times..
68
+
52
69
  ### Null Object Classes
53
70
  (See this link for typical usage: https://medium.com/@kelseydh/using-the-null-object-pattern-with-ruby-on-rails-b645ebf79785 ):
54
71
 
@@ -18,21 +18,8 @@ class DecoratorGenerator < Rails::Generators::NamedBase
18
18
  class_option :module, type: :string
19
19
 
20
20
  def main
21
- raise "No such model - please check naming" unless model_exists?
21
+ raise "No such model - #{model_name} - in #{model_path} (#{Dir.pwd})- please check naming" unless model_exists?
22
22
  copy_templates
23
23
  end
24
24
 
25
- private
26
-
27
- def model_exists?
28
- files = Dir[Rails.root + 'app/models/*.rb']
29
- models = files.map{ |m| File.basename(m, '.rb').camelize }
30
-
31
- models.include? model_name.camelize
32
- end
33
-
34
- def model_name
35
- model == "ERROR" ? class_name : model
36
- end
37
-
38
25
  end
@@ -49,16 +49,19 @@ module EasyGenGenerator
49
49
  @generator_type ||= self.class::TYPE
50
50
  end
51
51
 
52
+ def abstract_base_class_count
53
+ Object.const_defined?("#{self.class}::BASE_CLASSES") ? self.class::BASE_CLASSES : 1
54
+ end
52
55
  def no_files?
53
- only_one_file? && file_is_abstract_class? && no_tests?
56
+ count_of_files_indicates_empty? && files_are_abstract_class? && no_tests?
54
57
  end
55
58
 
56
- def only_one_file?
57
- Dir["./app/#{base_location}/**/*"].reject { | path | File.directory?(path) }.length == 1
59
+ def count_of_files_indicates_empty?
60
+ Dir["./app/#{base_location}/**/*"].reject { | path | File.directory?(path) }.length == abstract_base_class_count
58
61
  end
59
62
 
60
- def file_is_abstract_class?
61
- File.exist?("app/#{base_location}/application_#{generator_type}.rb")
63
+ def files_are_abstract_class?
64
+ Dir["app/#{base_location}/application_*.rb"].length == abstract_base_class_count
62
65
  end
63
66
 
64
67
  def no_tests?
@@ -77,13 +80,40 @@ module EasyGenGenerator
77
80
  Dir["./app/#{location}/**/*"].reject { | path | File.directory?(path) }.length == 0
78
81
  end
79
82
 
83
+ def gem_testing?
84
+ Rails.root.blank?
85
+ end
86
+
87
+ def application_root_path
88
+ gem_testing? ? Pathname.new("./") : Rails.root
89
+ end
90
+
80
91
  def teardown(places)
81
- print "Removing directories: "
92
+ return if (places.blank? || location.blank?)
82
93
  places.each do | place |
83
- print place + '/' + location + " "
84
- FileUtils.rm_rf(Rails.root.join(place,"#{location}"))
94
+ FileUtils.rm_rf(application_root_path.join(place,"#{location}"))
95
+ end
96
+ end
97
+
98
+ def model_exists?
99
+ @files ||= Dir[model_path + '*.rb']
100
+ @models ||= @files.map{ |m| File.basename(m, '.rb').camelize }
101
+
102
+ @models.include? model_name.camelize
103
+ end
104
+
105
+ def model_path
106
+ Pathname(application_root_path.to_s + '/app/models/')
107
+ end
108
+
109
+ def model_name
110
+ model == "ERROR" ? class_name : model
111
+ end
112
+
113
+ def eg_debug(message)
114
+ File.open("/Users/simon/Documents/Personal/Code/easy_gen/tmp/log.txt", "a") do |f|
115
+ f.write message + "\n"
85
116
  end
86
- puts "- done"
87
117
  end
88
118
 
89
119
  end
@@ -1,3 +1,4 @@
1
+ require 'rails/all'
1
2
  require 'rails/generators'
2
3
  require 'fileutils'
3
4
 
@@ -35,26 +36,18 @@ class NullGenerator < Rails::Generators::NamedBase
35
36
 
36
37
  private
37
38
 
38
- def model_exists?
39
- files = Dir[Rails.root + 'app/models/*.rb']
40
- models = files.map{ |m| File.basename(m, '.rb').camelize }
41
39
 
42
- models.include? model_name.camelize
43
- end
44
-
45
- def model_name
46
- model == "ERROR" ? class_name : model
40
+ def clazz_columns
41
+ clazz.columns_hash.reject { | key, value | key == "id" }
47
42
  end
48
43
 
49
44
  def clazz
50
- Kernel.const_get(model_name)
45
+ eg_debug "**** Model: #{model_name} - #{model_name.class} - #{model_name.method(:constantize).source_location} ****"
46
+
47
+ model_name.constantize
51
48
  end
52
49
 
53
50
  def default_value(ar_type)
54
51
  return AR_DEFAULTS[ar_type.to_sym]
55
52
  end
56
-
57
- def clazz_columns
58
- clazz.columns_hash.reject { | key, value | key == "id" }
59
- end
60
53
  end
@@ -11,6 +11,7 @@ class StrategyGenerator < Rails::Generators::NamedBase
11
11
 
12
12
  LOCATION = "strategies"
13
13
  TYPE = "strategy"
14
+ BASE_CLASSES = 2
14
15
 
15
16
  source_root File.expand_path("templates", __dir__)
16
17
  argument :strategies, type: :array, default: [], banner: "strategy strategy.."
@@ -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,3 @@
1
+ class ApplicationValidator < ActiveModel::EachValidator
2
+
3
+ end
@@ -0,0 +1,11 @@
1
+ class <%= class_name %>Validator < ApplicationValidator
2
+
3
+ def validate_each(record, attribute, value)
4
+ return false if value.blank?
5
+ return true
6
+ end
7
+
8
+
9
+ private
10
+
11
+ end
@@ -0,0 +1,23 @@
1
+ require "test_helper"
2
+
3
+
4
+ class <%= class_name %>Validatable
5
+ include ActiveModel::Validations
6
+ attr_accessor :<%= class_name.downcase %>
7
+
8
+ validates :<%= class_name.downcase %>, <%= class_name.downcase %>: true
9
+
10
+ end
11
+
12
+
13
+ class <%= class_name %>ValidatorTest < ActiveSupport::TestCase
14
+ setup do
15
+ @testable = <%= class_name %>Validatable.new
16
+ end
17
+
18
+ test 'Confirms there is a value' do
19
+ @testable.<%= class_name.downcase %> = 1
20
+ assert @testable.valid?
21
+ end
22
+
23
+ end
@@ -0,0 +1,23 @@
1
+ require 'rails/generators'
2
+ require 'fileutils'
3
+
4
+ template_dir = File.expand_path('templates', __dir__)
5
+
6
+ class ValidatorGenerator < Rails::Generators::NamedBase
7
+ include EasyGenGenerator
8
+
9
+ # bundle exec rails g service MyService
10
+ # bundle exec rails d service MyService
11
+
12
+ LOCATION = "validators"
13
+ TYPE = "validator"
14
+
15
+ source_root File.expand_path("templates", __dir__)
16
+
17
+ def main
18
+ copy_templates
19
+ end
20
+
21
+ private
22
+
23
+ end
@@ -1,3 +1,3 @@
1
1
  module EasyGen
2
- VERSION = "1.4.0"
2
+ VERSION = "1.5.1"
3
3
  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.4.0
4
+ version: 1.5.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-22 00:00:00.000000000 Z
11
+ date: 2023-03-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple generator for some standard design pattern classes and matching
14
14
  minitests.
@@ -45,6 +45,11 @@ files:
45
45
  - lib/easy_gen/strategy/templates/context_test.rb.tt
46
46
  - lib/easy_gen/strategy/templates/strategy.rb.tt
47
47
  - lib/easy_gen/strategy/templates/strategy_test.rb.tt
48
+ - lib/easy_gen/validator/USAGE
49
+ - lib/easy_gen/validator/templates/application_validator.rb.tt
50
+ - lib/easy_gen/validator/templates/validator.rb.tt
51
+ - lib/easy_gen/validator/templates/validator_test.rb.tt
52
+ - lib/easy_gen/validator/validator_generator.rb
48
53
  - lib/easy_gen/version.rb
49
54
  homepage: https://rubygems.org/gems/easy_gen
50
55
  licenses: