strategic 0.9.0 → 0.9.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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/README.md +13 -13
  4. data/lib/strategic.rb +35 -23
  5. metadata +7 -21
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d4031c3d360c08e12ac58e42d6e56b97c177c342e64a1680275f50ad0add7e08
4
- data.tar.gz: 5b4b783eca5301aa05f6043729531c7458b40806ed3b768c7b341b283d77ccda
3
+ metadata.gz: 2d5c5c2a7b15c1c3e826a37c64b0b4b90f8b4a1dd8d93aaeb5f6c81c796faa93
4
+ data.tar.gz: ef054a37db19f752d92e2344f7da9f6269732bcf897babcce4c756e426ca90e2
5
5
  SHA512:
6
- metadata.gz: b5edee9e7e0f491e5ecda04465372bb9e45274ecc906d07798e627a4485f8776f1625c8175ff403b3f8a25a8e16781f20dd6d02c76b0600cc41b7a9c4354b920
7
- data.tar.gz: 1c7ebfb4ce224c80d3181b55e4c9137ac7d1015733211592ec95218a581cedafd56abe235fab07de92c2cdf2343873eb2ad5a7789ebc18d97f05f84b88528c13
6
+ metadata.gz: 18fe37a88abab52119d44e03402ea2c005c3235d8709b3802dca8cebaf863af712bbc62c68bed47940ca7846d310135a685e416bc94c4c4f7d6984094ab5071b
7
+ data.tar.gz: 959739994722aa145afa46180cad9ef556fe27e57f39e9b49b631074dd788b2cfcc60dda26f564f8d09a2ce94c1100dc6d5994718d3b1421599dbc0665c1bbee
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.9.1
4
+
5
+ - `strategy_name` returns parsed strategy name of current strategy class
6
+ - `strategy_matcher` ignores a strategy if it found another strategy already matching by strategy_alias
7
+
3
8
  ## 0.9.0
4
9
 
5
10
  - `strategy_matcher` block support that enables any strategy to specify a custom matcher (or the superclass of all strategies instead)
data/README.md CHANGED
@@ -1,10 +1,9 @@
1
- # Strategic 0.9.0
1
+ # Strategic 0.9.1
2
2
  ## Painless Strategy Pattern in Ruby and Rails
3
3
  [![Gem Version](https://badge.fury.io/rb/strategic.svg)](http://badge.fury.io/rb/strategic)
4
- [![Build Status](https://travis-ci.com/AndyObtiva/strategic.svg?branch=master)](https://travis-ci.com/AndyObtiva/strategic?branch=master)
4
+ [![rspec](https://github.com/AndyObtiva/strategic/actions/workflows/ruby.yml/badge.svg)](https://github.com/AndyObtiva/strategic/actions/workflows/ruby.yml)
5
5
  [![Coverage Status](https://coveralls.io/repos/github/AndyObtiva/strategic/badge.svg?branch=master)](https://coveralls.io/github/AndyObtiva/strategic?branch=master)
6
-
7
- (Note: this gem is a very early alpha work in progress and may change API in the future)
6
+ [![Maintainability](https://api.codeclimate.com/v1/badges/0e638e392c21500c4fbe/maintainability)](https://codeclimate.com/github/AndyObtiva/strategic/maintainability)
8
7
 
9
8
  `if`/`case` conditionals can get really hairy in highly sophisticated business domains.
10
9
  Object-oriented inheritance helps remedy the problem, but dumping all
@@ -113,7 +112,7 @@ tax = tax_calculator_strategy.tax_for(100.0) # returns 9.0 from TaxCalculator
113
112
  Add the following to bundler's `Gemfile`.
114
113
 
115
114
  ```ruby
116
- gem 'strategic', '~> 0.9.0'
115
+ gem 'strategic', '~> 0.9.1'
117
116
  ```
118
117
 
119
118
  ### Option 2: Manual
@@ -121,7 +120,7 @@ gem 'strategic', '~> 0.9.0'
121
120
  Or manually install and require library.
122
121
 
123
122
  ```bash
124
- gem install strategic -v0.9.0
123
+ gem install strategic -v0.9.1
125
124
  ```
126
125
 
127
126
  ```ruby
@@ -151,13 +150,14 @@ strategy.
151
150
 
152
151
  ## API
153
152
 
154
- - `StrategicSuperClass::strategy_class_for(string_or_class_or_object)`: selects a strategy class based on a string (e.g. 'us' selects USStrategy) or alternatively a class/object if you have a mirror hierarchy for the strategy hierarchy
155
- - `StrategicSuperClass::new_strategy(string_or_class_or_object, *args, &block)`: instantiates a strategy based on a string/class/object and strategy constructor args
156
- - `StrategicSuperClass::strategies`: returns list of strategies discovered by convention (nested under a namespace matching the superclass name)
157
- - `StrategicSuperClass::strategy_names`: returns list of strategy names (strings) discovered by convention (nested under a namespace matching the superclass name)
158
- - `StrategicSuperClass::strategy_matcher`: custom match (e.g. `strategy_matcher {|string| string.start_with?('C') && string.end_with?('o')}`)
159
- - `StrategicSuperClass::strategy_exclusion`: exclusion from custom matcher (e.g. `strategy_exclusion 'Cio'`)
160
- - `StrategicSuperClass::strategy_alias`: alias for strategy in addition to strategy's name derived from class name by convention (e.g. `strategy_alias 'USA'` for `UsStrategy`)
153
+ - `StrategicClass::strategy_class_for(string_or_class_or_object)`: selects a strategy class based on a string (e.g. 'us' selects USStrategy) or alternatively a class/object if you have a mirror hierarchy for the strategy hierarchy
154
+ - `StrategicClass::new_strategy(string_or_class_or_object, *args, &block)`: instantiates a strategy based on a string/class/object and strategy constructor args
155
+ - `StrategicClass::strategies`: returns list of strategies discovered by convention (nested under a namespace matching the superclass name)
156
+ - `StrategicClass::strategy_names`: returns list of strategy names (strings) discovered by convention (nested under a namespace matching the superclass name)
157
+ - `StrategicClass::strategy_name`: returns parsed strategy name of current strategy class
158
+ - `StrategicClass::strategy_matcher`: custom match (e.g. `strategy_matcher {|string| string.start_with?('C') && string.end_with?('o')}`)
159
+ - `StrategicClass::strategy_exclusion`: exclusion from custom matcher (e.g. `strategy_exclusion 'Cio'`)
160
+ - `StrategicClass::strategy_alias`: alias for strategy in addition to strategy's name derived from class name by convention (e.g. `strategy_alias 'USA'` for `UsStrategy`)
161
161
 
162
162
  ## TODO
163
163
 
data/lib/strategic.rb CHANGED
@@ -49,6 +49,10 @@ module Strategic
49
49
  @strategy_matcher
50
50
  end
51
51
  end
52
+
53
+ def strategy_matcher_for_any_strategy?
54
+ !!(strategy_matcher || strategies.any?(&:strategy_matcher))
55
+ end
52
56
 
53
57
  def require_strategies
54
58
  klass_path = caller[1].split(':').first
@@ -59,31 +63,35 @@ module Strategic
59
63
  end
60
64
 
61
65
  def strategy_class_for(string_or_class_or_object)
62
- strategy_class = nil
63
- if strategy_matcher
64
- strategy_class = strategies.detect do |strategy|
65
- match = strategy&.strategy_matcher&.call(string_or_class_or_object)
66
- match ||= strategy.instance_exec(string_or_class_or_object, &strategy_matcher)
67
- match unless strategy.strategy_exclusions.include?(string_or_class_or_object)
68
- end
69
- else
70
- if string_or_class_or_object.is_a?(String)
71
- strategy_class_name = string_or_class_or_object.downcase
72
- elsif string_or_class_or_object.is_a?(Class)
73
- strategy_class_name = string_or_class_or_object.name
74
- else
75
- strategy_class_name = string_or_class_or_object.class.name
76
- end
77
- begin
78
- class_name = "::#{self.name}::#{Strategic.classify(strategy_class_name)}Strategy"
79
- strategy_class = class_eval(class_name)
80
- rescue NameError
81
- # No Op
82
- end
83
- end
66
+ strategy_class = strategy_matcher_for_any_strategy? ? strategy_class_with_strategy_matcher(string_or_class_or_object) : strategy_class_without_strategy_matcher(string_or_class_or_object)
84
67
  strategy_class ||= strategies.detect { |strategy| strategy.strategy_aliases.include?(string_or_class_or_object) }
85
68
  strategy_class ||= self
86
69
  end
70
+
71
+ def strategy_class_with_strategy_matcher(string_or_class_or_object)
72
+ strategies.detect do |strategy|
73
+ match = strategy.strategy_aliases.include?(string_or_class_or_object)
74
+ match ||= strategy&.strategy_matcher&.call(string_or_class_or_object) || (strategy_matcher && strategy.instance_exec(string_or_class_or_object, &strategy_matcher))
75
+ # match unless excluded or included by another strategy as an alias
76
+ match unless strategy.strategy_exclusions.include?(string_or_class_or_object) || (strategies - [strategy]).map(&:strategy_aliases).flatten.include?(string_or_class_or_object)
77
+ end
78
+ end
79
+
80
+ def strategy_class_without_strategy_matcher(string_or_class_or_object)
81
+ if string_or_class_or_object.is_a?(String)
82
+ strategy_class_name = string_or_class_or_object.downcase
83
+ elsif string_or_class_or_object.is_a?(Class)
84
+ strategy_class_name = string_or_class_or_object.name
85
+ else
86
+ strategy_class_name = string_or_class_or_object.class.name
87
+ end
88
+ begin
89
+ class_name = "::#{self.name}::#{Strategic.classify(strategy_class_name)}Strategy"
90
+ class_eval(class_name)
91
+ rescue NameError
92
+ # No Op
93
+ end
94
+ end
87
95
 
88
96
  def new_strategy(string_or_class_or_object, *args, &block)
89
97
  strategy_class_for(string_or_class_or_object).new(*args, &block)
@@ -98,7 +106,11 @@ module Strategic
98
106
  end
99
107
 
100
108
  def strategy_names
101
- strategies.map(&:name).map { |class_name| Strategic.underscore(class_name.split(':').last).sub(/_strategy$/, '') }
109
+ strategies.map(&:strategy_name)
110
+ end
111
+
112
+ def strategy_name
113
+ Strategic.underscore(name.split(':').last).sub(/_strategy$/, '')
102
114
  end
103
115
  end
104
116
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strategic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Maleh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-15 00:00:00.000000000 Z
11
+ date: 2021-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -42,14 +42,14 @@ dependencies:
42
42
  name: rdoc
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '3.12'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.12'
55
55
  - !ruby/object:Gem::Dependency
@@ -80,20 +80,6 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
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.23
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.23
97
83
  - !ruby/object:Gem::Dependency
98
84
  name: simplecov
99
85
  requirement: !ruby/object:Gem::Requirement
@@ -109,19 +95,19 @@ dependencies:
109
95
  - !ruby/object:Gem::Version
110
96
  version: 0.16.1
111
97
  - !ruby/object:Gem::Dependency
112
- name: simplecov-lcov
98
+ name: coveralls
113
99
  requirement: !ruby/object:Gem::Requirement
114
100
  requirements:
115
101
  - - "~>"
116
102
  - !ruby/object:Gem::Version
117
- version: 0.7.0
103
+ version: 0.8.23
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
107
  requirements:
122
108
  - - "~>"
123
109
  - !ruby/object:Gem::Version
124
- version: 0.7.0
110
+ version: 0.8.23
125
111
  - !ruby/object:Gem::Dependency
126
112
  name: puts_debuggerer
127
113
  requirement: !ruby/object:Gem::Requirement