easy_gen 1.3.2 → 1.4.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 +4 -4
- data/README.md +35 -1
- data/lib/easy_gen/strategy/USAGE +14 -0
- data/lib/easy_gen/strategy/strategy_generator.rb +52 -0
- data/lib/easy_gen/strategy/templates/application_context.rb.tt +3 -0
- data/lib/easy_gen/strategy/templates/application_strategy.rb.tt +3 -0
- data/lib/easy_gen/strategy/templates/context.rb.tt +26 -0
- data/lib/easy_gen/strategy/templates/context_test.rb.tt +11 -0
- data/lib/easy_gen/strategy/templates/strategy.rb.tt +5 -0
- data/lib/easy_gen/strategy/templates/strategy_test.rb.tt +14 -0
- data/lib/easy_gen/version.rb +1 -1
- metadata +9 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '039f3a56535913793c5650392d33a758ed535b5408d3378d0333931d8d4ad7c2'
|
4
|
+
data.tar.gz: 88b749acf53e01644f352b29c70f3313c46461d964121c2b4c61fa59deaf4de5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e5bc98279170412cc346fb9c0e3cdd0b2b73bd53411bbb767ddc0bafcd613db800ad73735b0dc3ba4848f9601603f4ff04ecbecf1d98821a58d1290a99af3d3
|
7
|
+
data.tar.gz: 94ed33ad98ae628b1d7b8b471fe0ce75c9603f676861003f83722f8c2a6ba740ee300ba379b5513dedda24fe2a2dc608447d3e46c50dd7de50ad492fbf1b6637
|
data/README.md
CHANGED
@@ -84,5 +84,39 @@ The command above:
|
|
84
84
|
|
85
85
|
Module option is also supported.
|
86
86
|
|
87
|
+
### Strategy Classes
|
88
|
+
(See this link for typical usage: https://refactoring.guru/design-patterns/strategy/ruby/example)
|
89
|
+
|
90
|
+
```sh
|
91
|
+
bundle exec rails g strategy context strategy1 strategy2 ...
|
92
|
+
```
|
93
|
+
|
94
|
+
|
95
|
+
Example:
|
96
|
+
|
97
|
+
```sh
|
98
|
+
bundle exec rails g strategy fishing hook line net
|
99
|
+
```
|
100
|
+
|
101
|
+
The command above:
|
102
|
+
|
103
|
+
- Creates '/app/strategies' directory if it doesnt exist.
|
104
|
+
- Creates '/app/strategies/fishing' directory if it doesnt exist.
|
105
|
+
- Installs new application context class in '/app/strategies' with the name 'ApplicationContext' in file '/app/strategies/application_context.rb.'.
|
106
|
+
- Installs new application strategy class in '/app/strategies' with the name 'ApplicationStrategy' in file '/app/strategies/application_strategy.rb.'.
|
107
|
+
- 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.'
|
108
|
+
- 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.'
|
109
|
+
- 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.'
|
110
|
+
- 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.'
|
111
|
+
- 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.'
|
112
|
+
- Creates '/test/strategies' directory if it doesnt exist.
|
113
|
+
- Creates '/test/strategies/fishing' directory if it doesnt exist.
|
114
|
+
- Installs new test class in '/test/strategies' with the name 'FishingContext' in the file '/test/strategies/fishing_context_test.rb'.
|
115
|
+
- Installs new test class in '/test/strategies/fishing' with the name 'FishingStrategy' in the file '/test/strategies/fishing_strategy_test.rb'.
|
116
|
+
- Installs new test class in '/test/strategies/fishing' with the name 'HookStrategy' in the file '/test/strategies/hook_strategy_test.rb'.
|
117
|
+
- Installs new test class in '/test/strategies/fishing' with the name 'LineStrategy' in the file '/test/strategies/line_strategy_test.rb'.
|
118
|
+
- Installs new test class in '/test/strategies/fishing' with the name 'NetStrategy' in the file '/test/strategies/net_strategy_test.rb'.
|
119
|
+
|
120
|
+
|
87
121
|
## Summary
|
88
|
-
**
|
122
|
+
** 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,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,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
|
data/lib/easy_gen/version.rb
CHANGED
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.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simon Stearn
|
@@ -37,6 +37,14 @@ 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
|
40
48
|
- lib/easy_gen/version.rb
|
41
49
|
homepage: https://rubygems.org/gems/easy_gen
|
42
50
|
licenses:
|