strategic 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +20 -0
- data/README.md +167 -0
- data/lib/strategic.rb +56 -0
- metadata +172 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5b79636eeccc8c6b813d95c05e0aba4e864d8cbc
|
4
|
+
data.tar.gz: 4fb40967c29ffa5c788db5432c0a80444ab5c3db
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 654d0e603dbe10f748ba3025664bf0480a4833d62be091ffb3f6b807754487a8284f2687f7f9c40658d499e99342f408c2c282c8590962664c16cf6504453aa9
|
7
|
+
data.tar.gz: 74f1272d40abb8e22520914614bf99a80455f99480007f4b7bc54b0b785065afcf8762822cf34bae977108e42895971c4bf9ac822d18f81620f9914925f461bd
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2017 Andy Maleh
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
# Strategic (Painless Strategy Pattern in Ruby and Rails)
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/strategic.svg)](http://badge.fury.io/rb/strategic)
|
3
|
+
|
4
|
+
if/case conditionals can get really hairy in highly sophisticated business domains.
|
5
|
+
Domain model inheritance can help remedy the problem, but dumping all
|
6
|
+
logic variations in the same domain models can cause a maintenance nightmare.
|
7
|
+
Thankfully, Strategy Pattern as per the Gang of Four solves the problem by externalizing logic variations to
|
8
|
+
separate classes outside the domain models.
|
9
|
+
|
10
|
+
Still, there are a number of challenges with repeated implementation of Strategy Pattern:
|
11
|
+
- Making domain models aware of newly added strategies without touching their
|
12
|
+
code (Open/Closed Principle).
|
13
|
+
- Fetching the right strategy without use of conditionals.
|
14
|
+
- Avoiding duplication of strategy dispatch code for multiple domain models
|
15
|
+
- Have different strategies mirror an existing domain model hierarchy
|
16
|
+
|
17
|
+
`strategic` solves these problems by offering:
|
18
|
+
- Strategy Pattern support through a Ruby mixin and strategy path/name convention
|
19
|
+
- Automatic discovery of strategies based on path/name convention
|
20
|
+
- Ability to fetch needed strategy without use of conditionals
|
21
|
+
- Ability to fetch a strategy by name or by object type to mirror
|
22
|
+
- Plain Ruby and Ruby on Rails support
|
23
|
+
|
24
|
+
`strategic` enables you to make any existing domain model "strategic",
|
25
|
+
externalizing all logic concerning algorithmic variations into separate strategy
|
26
|
+
classes that are easy to find, maintain and extend.
|
27
|
+
|
28
|
+
## Instructions
|
29
|
+
|
30
|
+
### Option 1: Bundler
|
31
|
+
|
32
|
+
Add the following to bundler's `Gemfile`.
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
gem 'strategic', '~> 0.8.0'
|
36
|
+
```
|
37
|
+
|
38
|
+
### Option 2: Manual
|
39
|
+
|
40
|
+
Or manually install and require library.
|
41
|
+
|
42
|
+
```bash
|
43
|
+
gem install strategic -v0.8.0
|
44
|
+
```
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
require 'strategic'
|
48
|
+
```
|
49
|
+
|
50
|
+
### Usage
|
51
|
+
|
52
|
+
Steps:
|
53
|
+
1. Have the original class you'd like to strategize include Strategic
|
54
|
+
2. Create a directory matching the class underscored file name minus the '.rb' extension
|
55
|
+
3. Create a strategy class under that directory, which:
|
56
|
+
- Lives under the original class namespace
|
57
|
+
- Extends the original class to strategize
|
58
|
+
- Has a class name that ends with `Strategy` suffix (e.g. `NewCustomerStrategy`)
|
59
|
+
4. Get needed strategy class using `strategy_class_for` class method taking strategy name (any case) or related object/type (can call `strategy_names` class method to obtain strategy names)
|
60
|
+
5. Instantiate strategy with needed constructor parameters
|
61
|
+
6. Invoke strategy method needed
|
62
|
+
|
63
|
+
Alternative approach:
|
64
|
+
|
65
|
+
Combine steps 4 and 5 using `new_strategy` method, which takes both strategy name
|
66
|
+
and constructor parameters
|
67
|
+
|
68
|
+
Passing an invalid strategy name to `strategy_class_for` returns original class as the default
|
69
|
+
strategy.
|
70
|
+
|
71
|
+
### Example
|
72
|
+
|
73
|
+
1. Class to strategize is: `TaxCalculator`
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class TaxCalculator
|
77
|
+
include Strategic
|
78
|
+
|
79
|
+
def tax_for(amount)
|
80
|
+
amount * 0.09
|
81
|
+
end
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
2. Directory to create strategies under: `tax_calculator`
|
86
|
+
|
87
|
+
3. Strategy class:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
class TaxCalculator::UsStrategy < TaxCalculator
|
91
|
+
def initialize(state)
|
92
|
+
@state = state
|
93
|
+
end
|
94
|
+
def tax_for(amount)
|
95
|
+
amount * state_rate
|
96
|
+
end
|
97
|
+
# ... more code follows
|
98
|
+
end
|
99
|
+
|
100
|
+
class TaxCalculator::CanadaStrategy < TaxCalculator
|
101
|
+
def initialize(province)
|
102
|
+
@province = province
|
103
|
+
end
|
104
|
+
def tax_for(amount)
|
105
|
+
amount * (gst + qst)
|
106
|
+
end
|
107
|
+
# ... more code follows
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
4. Get needed strategy:
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
tax_calculator_strategy_class = TaxCalculator.strategy_class_for('us')
|
115
|
+
```
|
116
|
+
|
117
|
+
5. Instantiate strategy:
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
tax_calculator_strategy = strategy_class.new('IL')
|
121
|
+
```
|
122
|
+
|
123
|
+
6. Invoke strategy method:
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
tax = tax_calculator_strategy.tax_for(39.78)
|
127
|
+
```
|
128
|
+
|
129
|
+
**Alternative approach using `new_strategy`:**
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
tax_calculator_strategy = TaxCalculator.new_strategy('US', 'IL')
|
133
|
+
tax = tax_calculator_strategy.tax_for(39.78)
|
134
|
+
```
|
135
|
+
|
136
|
+
**Default strategy for a strategy name that has no strategy class is TaxCalculator**
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
tax_calculator_strategy_class = TaxCalculator.strategy_class_for('France')
|
140
|
+
tax_calculator_strategy = tax_calculator_strategy_class.new
|
141
|
+
tax = tax_calculator_strategy.tax_for(100.0) # returns 9.0 from TaxCalculator
|
142
|
+
```
|
143
|
+
|
144
|
+
## Release Notes
|
145
|
+
|
146
|
+
**0.8.0:** Initial version with `strategy_class_for`, `new_strategy`, `strategies`, and `strategy_names`
|
147
|
+
|
148
|
+
## TODO
|
149
|
+
|
150
|
+
None
|
151
|
+
|
152
|
+
## Contributing
|
153
|
+
|
154
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
155
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
156
|
+
* Fork the project.
|
157
|
+
* Change directory into project
|
158
|
+
* Run `gem install bundler && bundle && rake` and make sure RSpec tests are passing
|
159
|
+
* Start a feature/bugfix branch.
|
160
|
+
* Write RSpec tests, Code, Commit and push until you are happy with your contribution.
|
161
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
162
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
163
|
+
|
164
|
+
## Copyright
|
165
|
+
|
166
|
+
Copyright (c) 2020 Andy Maleh. See LICENSE.txt for
|
167
|
+
further details.
|
data/lib/strategic.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
module Strategic
|
2
|
+
def self.included(klass)
|
3
|
+
klass.extend(ClassMethods)
|
4
|
+
klass.require_strategies
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def require_strategies
|
9
|
+
klass_path = caller[1].split(':').first
|
10
|
+
strategy_path = File.expand_path(File.join(klass_path, '..', Strategic.underscore(self.name), '**', '*.rb'))
|
11
|
+
Dir.glob(strategy_path) do |strategy|
|
12
|
+
Object.const_defined?(:Rails) ? require_dependency(strategy) : require(strategy)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def strategy_class_for(string_or_class_or_object)
|
17
|
+
if string_or_class_or_object.is_a?(String)
|
18
|
+
strategy_class_name = string_or_class_or_object.downcase
|
19
|
+
elsif string_or_class_or_object.is_a?(Class)
|
20
|
+
strategy_class_name = string_or_class_or_object.name
|
21
|
+
else
|
22
|
+
strategy_class_name = string_or_class_or_object.class.name
|
23
|
+
end
|
24
|
+
class_name ||= "::#{self.name}::#{Strategic.classify(strategy_class_name)}Strategy"
|
25
|
+
class_eval(class_name)
|
26
|
+
rescue NameError
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def new_strategy(string_or_class_or_object, *args, &block)
|
31
|
+
strategy_class_for(string_or_class_or_object).new(*args, &block)
|
32
|
+
end
|
33
|
+
|
34
|
+
def strategies
|
35
|
+
constants.map do |constant_symbol|
|
36
|
+
const_get(constant_symbol)
|
37
|
+
end.select do |constant|
|
38
|
+
constant.respond_to?(:ancestors) && constant.ancestors.include?(self)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def strategy_names
|
43
|
+
strategies.map(&:name).map { |class_name| Strategic.underscore(class_name.split(':').last).sub(/_strategy$/, '') }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def self.classify(text)
|
50
|
+
text.split("_").map {|word| "#{word[0].upcase}#{word[1..-1]}"}.join
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.underscore(text)
|
54
|
+
text.chars.reduce('') {|output,c| !output.empty? && c.match(/[A-Z]/) ? output + '_' + c : output + c}.downcase
|
55
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,172 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: strategic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.8.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andy Maleh
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-01-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.5.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.5.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec-mocks
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.5.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.5.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rdoc
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.12'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.12'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: jeweler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 2.3.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 2.3.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: coveralls
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.8.5
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.8.5
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.10.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.10.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: puts_debuggerer
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.8.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.8.0
|
125
|
+
description: |
|
126
|
+
if/case conditionals can get really hairy in highly sophisticated business domains.
|
127
|
+
Domain model inheritance can help remedy the problem, but you don't want to dump all
|
128
|
+
logic variations in the same domain models.
|
129
|
+
Strategy Pattern solves that problem by externalizing logic variations to
|
130
|
+
separate classes outside the domain models.
|
131
|
+
One difficulty with implementing Strategy Pattern is making domain models aware
|
132
|
+
of newly added strategies without touching their code (Open/Closed Principle).
|
133
|
+
Strategic solves that problem by supporting Strategy Pattern with automatic discovery
|
134
|
+
of strategies and ability fetch the right strategy without conditionals.
|
135
|
+
This allows you to make any domain model "strategic" by simply following a convention
|
136
|
+
in the directory/namespace structure you create your strategies under so that the domain
|
137
|
+
model automatically discovers all available strategies.
|
138
|
+
email: andy.am@gmail.com
|
139
|
+
executables: []
|
140
|
+
extensions: []
|
141
|
+
extra_rdoc_files:
|
142
|
+
- LICENSE.txt
|
143
|
+
- README.md
|
144
|
+
files:
|
145
|
+
- LICENSE.txt
|
146
|
+
- README.md
|
147
|
+
- lib/strategic.rb
|
148
|
+
homepage: http://github.com/AndyObtiva/strategic
|
149
|
+
licenses:
|
150
|
+
- MIT
|
151
|
+
metadata: {}
|
152
|
+
post_install_message:
|
153
|
+
rdoc_options: []
|
154
|
+
require_paths:
|
155
|
+
- lib
|
156
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
157
|
+
requirements:
|
158
|
+
- - ">="
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
version: '0'
|
161
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
162
|
+
requirements:
|
163
|
+
- - ">="
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '0'
|
166
|
+
requirements: []
|
167
|
+
rubyforge_project:
|
168
|
+
rubygems_version: 2.6.10
|
169
|
+
signing_key:
|
170
|
+
specification_version: 4
|
171
|
+
summary: Painless Strategy Pattern for Ruby and Rails
|
172
|
+
test_files: []
|