easy_gen 1.3.2 → 1.5.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: 49aba729d4c64cdd2a07c27e9fa4119bfefbf02b30cc44e58407a9deaa050b2e
4
- data.tar.gz: f6a74fba29331225739f45d117e3385a455966072b9aa02222fdd27b9f46bd8a
3
+ metadata.gz: 9c04ab1432a5a0b018928023b51ad5e6b2c106bf459365872754b28888328023
4
+ data.tar.gz: ebb5079ef9cdc195f1c1260955ce428ebe94a47a19bb3d4c4d594a520025c229
5
5
  SHA512:
6
- metadata.gz: 6666acd9b19b906459ca3e8a865f017fb93599f7c22294aff9218e533ddee212e7aea7762632840109eb7d0eca6e25a2152353e309b434005294945afae1eab7
7
- data.tar.gz: 238b7af7e54c068b9c643c4ea3f515326ff1ce32765a7f744b6219ebefce2b661ebcb3a3a9039086c8f22f94e3bcc44decd8cea89d3103acfb5066e7d9906b68
6
+ metadata.gz: ea06ed2048dd9e0a15225009b82fa1f128dd939533c1918a62a2b6397a9f58e38ec80908a6498af7366b603eff956ca24e4c57cf68cd803e4d6b3403fee4dc77
7
+ data.tar.gz: eaccb356574eb88f88d7cb8b4eb93ed216f3c86de6eb66297ac0974d22dd1826acacb3230afd755a0b630fd98ab8ed1ab51037ee3c7bad86a6e7971cee9ad18b
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
 
@@ -84,5 +101,39 @@ The command above:
84
101
 
85
102
  Module option is also supported.
86
103
 
104
+ ### Strategy Classes
105
+ (See this link for typical usage: https://refactoring.guru/design-patterns/strategy/ruby/example)
106
+
107
+ ```sh
108
+ bundle exec rails g strategy context strategy1 strategy2 ...
109
+ ```
110
+
111
+
112
+ Example:
113
+
114
+ ```sh
115
+ bundle exec rails g strategy fishing hook line net
116
+ ```
117
+
118
+ The command above:
119
+
120
+ - Creates '/app/strategies' directory if it doesnt exist.
121
+ - Creates '/app/strategies/fishing' directory if it doesnt exist.
122
+ - Installs new application context class in '/app/strategies' with the name 'ApplicationContext' in file '/app/strategies/application_context.rb.'.
123
+ - Installs new application strategy class in '/app/strategies' with the name 'ApplicationStrategy' in file '/app/strategies/application_strategy.rb.'.
124
+ - Installs new context module and class in '/app/strategies' with the class name 'Fishing::Context' in the file /app/strategies/fishing_context.rb. This will inherit from /app/strategies/application_context.rb.'
125
+ - Installs new strategy class in '/app/strategies/fishing' with the class name 'Fishing::FishingStrategy' in the file /app/strategies/fishing/fishing_strategy.rb. This will inherit from /app/strategies/application_strategy.rb.'
126
+ - Installs new strategy class in '/app/strategies/fishing' with the class name 'Fishing::HookStrategy' in the file /app/strategies/fishing/hook_strategy.rb. This will inherit from /app/strategies/fishing/fishing_strategy.rb.'
127
+ - Installs new strategy class in '/app/strategies/fishing' with the class name 'Fishing::LineStrategy' in the file /app/strategies/fishing/line_strategy.rb. This will inherit from /app/strategies/fishing/fishing_strategy.rb.'
128
+ - Installs new strategy class in '/app/strategies/fishing' with the class name 'Fishing::NetStrategy' in the file /app/strategies/fishing/net_strategy.rb. This will inherit from /app/strategies/fishing/fishing_strategy.rb.'
129
+ - Creates '/test/strategies' directory if it doesnt exist.
130
+ - Creates '/test/strategies/fishing' directory if it doesnt exist.
131
+ - Installs new test class in '/test/strategies' with the name 'FishingContext' in the file '/test/strategies/fishing_context_test.rb'.
132
+ - Installs new test class in '/test/strategies/fishing' with the name 'FishingStrategy' in the file '/test/strategies/fishing_strategy_test.rb'.
133
+ - Installs new test class in '/test/strategies/fishing' with the name 'HookStrategy' in the file '/test/strategies/hook_strategy_test.rb'.
134
+ - Installs new test class in '/test/strategies/fishing' with the name 'LineStrategy' in the file '/test/strategies/line_strategy_test.rb'.
135
+ - Installs new test class in '/test/strategies/fishing' with the name 'NetStrategy' in the file '/test/strategies/net_strategy_test.rb'.
136
+
137
+
87
138
  ## Summary
88
- ** Nothing clever - just saves a bit of typing
139
+ ** Saves a bit of typing
@@ -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,52 @@
1
+ require 'rails/generators'
2
+ require 'fileutils'
3
+
4
+ template_dir = File.expand_path('templates', __dir__)
5
+
6
+ class StrategyGenerator < Rails::Generators::NamedBase
7
+ include EasyGenGenerator
8
+
9
+ # bundle exec rails g service MyService
10
+ # bundle exec rails d service MyService
11
+
12
+ LOCATION = "strategies"
13
+ TYPE = "strategy"
14
+
15
+ source_root File.expand_path("templates", __dir__)
16
+ argument :strategies, type: :array, default: [], banner: "strategy strategy.."
17
+
18
+
19
+ def main
20
+ copy_templates
21
+ end
22
+
23
+ private
24
+
25
+ def copy_templates
26
+ unless File.exist? "app/#{base_location}/application_#{generator_type}.rb"
27
+ template "application_context.rb", "app/#{base_location}/application_context.rb"
28
+ template "application_#{generator_type}.rb", "app/#{base_location}/application_#{generator_type}.rb"
29
+ end
30
+ template "context.rb", "app/#{location}/#{file_path}_context.rb"
31
+ template "context_test.rb", "test/#{location}/#{file_path}_context_test.rb"
32
+
33
+ @parent_strategy = ""
34
+ @strategy = class_name.camelize
35
+ template "#{generator_type}.rb", "app/#{location}/#{class_name.downcase}/#{class_name.downcase}_#{generator_type}.rb"
36
+
37
+ @parent_strategy = @strategy
38
+ strategies.each do | strategy |
39
+ @strategy = strategy.camelize
40
+ template "#{generator_type}.rb", "app/#{location}/#{class_name.downcase}/#{strategy.downcase}_#{generator_type}.rb"
41
+ template "#{generator_type}_test.rb", "test/#{location}/#{class_name.downcase}/#{strategy.downcase}_#{generator_type}_test.rb"
42
+ end
43
+
44
+ if no_files?
45
+ teardown ['app', 'test']
46
+ end
47
+
48
+ if no_files_for_module?
49
+ teardown ['app', 'test']
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationContext
2
+
3
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationStrategy
2
+
3
+ end
@@ -0,0 +1,26 @@
1
+ module <%= class_name %>
2
+ class Context < ApplicationContext
3
+ # The Context maintains a reference to one of the Strategy objects. The
4
+ # Context does not know the concrete class of a strategy. It should work with
5
+ # all strategies via the Strategy interface.
6
+ attr_writer :strategy
7
+
8
+ # Usually, the Context accepts a strategy through the constructor, but also
9
+ # provides a setter to change it at runtime.
10
+ def initialize(strategy)
11
+ @strategy = strategy
12
+ end
13
+
14
+ # Usually, the Context allows replacing a Strategy object at runtime.
15
+ def strategy=(strategy)
16
+ @strategy = strategy
17
+ end
18
+
19
+ # The Context delegates some work to the Strategy object instead of
20
+ # implementing multiple versions of the algorithm on its own.
21
+ def do_some_business_logic
22
+ # ...
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,11 @@
1
+ require "test_helper"
2
+
3
+ class <%= class_name %>ContextTest < ActiveSupport::TestCase
4
+ setup do
5
+ @context = <%= class_name %>::Context.new()
6
+ <% strategies.each do | strategy | %>
7
+ @<%= strategy %>_strategy = <%= class_name.camelize %>::<%= strategy.camelize %>Strategy.new()
8
+ <% end %>
9
+ end
10
+
11
+ end
@@ -0,0 +1,5 @@
1
+ class <%= class_name %>::<%= @strategy %>Strategy < <%= @parent_strategy.blank? ? "Application" : "#{@parent_strategy}::#{@parent_strategy}" %>Strategy
2
+ def call(data)
3
+ raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ require "test_helper"
2
+
3
+ class <%= class_name.camelize %><%= @strategy %>StrategyTest < ActiveSupport::TestCase
4
+ setup do
5
+ @strategy = <%= class_name %>::<%= @strategy.camelize %>Strategy.new()
6
+ end
7
+
8
+ test 'Raises <%= class_name.camelize %><%= @strategy %> not implemented' do
9
+ assert_raises NotImplementedError do
10
+ @strategy.call(0)
11
+ end
12
+ end
13
+
14
+ end
@@ -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.3.2"
2
+ VERSION = "1.5.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.3.2
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Stearn
@@ -37,6 +37,19 @@ files:
37
37
  - lib/easy_gen/service/templates/application_service.rb.tt
38
38
  - lib/easy_gen/service/templates/service.rb.tt
39
39
  - lib/easy_gen/service/templates/service_test.rb.tt
40
+ - lib/easy_gen/strategy/USAGE
41
+ - lib/easy_gen/strategy/strategy_generator.rb
42
+ - lib/easy_gen/strategy/templates/application_context.rb.tt
43
+ - lib/easy_gen/strategy/templates/application_strategy.rb.tt
44
+ - lib/easy_gen/strategy/templates/context.rb.tt
45
+ - lib/easy_gen/strategy/templates/context_test.rb.tt
46
+ - lib/easy_gen/strategy/templates/strategy.rb.tt
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
40
53
  - lib/easy_gen/version.rb
41
54
  homepage: https://rubygems.org/gems/easy_gen
42
55
  licenses: