active_module 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +45 -23
- data/lib/active_module/comparison.rb +3 -3
- data/lib/active_module/module_refinement.rb +4 -0
- data/lib/active_module/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 191f7c2deb3a07261247a4302436cc734541ab62c3db9dd742974d58320dd763
|
4
|
+
data.tar.gz: bd20cb4d1aa7b6437facfaefc8213485571497df5e9e607ce19d16b81db246b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc48c3f11e8f71321ab4abece1b5d58f671c96de7e494a5f2f7a8e55b9946c6e88af9e61970bdd2332eba12ee74dadec13485555dff0f05a4f74209c1327fd96
|
7
|
+
data.tar.gz: 126c9c4a2d885cadbe472dc23caa5025415f2806411e9eecce37a5b40c28ce990c5956246646292e9780b6f30d6f77705ab681899a7532940dbe0cfe59f2a471
|
data/README.md
CHANGED
@@ -154,6 +154,19 @@ module YourClassOrModuleThatWantsToCompare
|
|
154
154
|
end
|
155
155
|
```
|
156
156
|
|
157
|
+
or like this, if you don't want to use the refinement:
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
ActiveModule::Comparison.compare(my_ar_object.module_field, :MyModule1)
|
161
|
+
```
|
162
|
+
|
163
|
+
but in this last case it would probably make more sense to simply use a module literal:
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
my_ar_object.module_field == MyClass::MyModule1
|
167
|
+
```
|
168
|
+
|
169
|
+
|
157
170
|
## Examples
|
158
171
|
|
159
172
|
### Strategy Pattern (composition-based polymorphism)
|
@@ -161,6 +174,19 @@ end
|
|
161
174
|
[The Strategy design pattern](https://en.wikipedia.org/wiki/Strategy_pattern) allows composition based polymorphism. This enables runtime polymorphism (by changing the strategy in runtime),
|
162
175
|
and multiple-polymorphism (by composing an object of multiple strategies).
|
163
176
|
|
177
|
+
If you want to use classes this will do:
|
178
|
+
```ruby
|
179
|
+
class MyARObject < ActiveRecord::Base
|
180
|
+
attribute :strategy, :active_module, possible_modules: StrategySuperclass.subclasses
|
181
|
+
|
182
|
+
def run_strategy!(args)
|
183
|
+
strategy.new(some_args).call(other_args)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
```
|
187
|
+
|
188
|
+
But if you are not in the mood to define a class hierarchy for it, you can use modules instead:
|
189
|
+
|
164
190
|
```ruby
|
165
191
|
class MyARObject < ActiveRecord::Base
|
166
192
|
module Strategy1
|
@@ -179,16 +205,13 @@ class MyARObject < ActiveRecord::Base
|
|
179
205
|
:active_module,
|
180
206
|
possible_modules: [Strategy1, Strategy2]
|
181
207
|
|
182
|
-
def run_strategy!
|
183
|
-
|
184
|
-
# in this case strategies were classes we could also
|
185
|
-
# instantiate them
|
186
|
-
strategy.call
|
208
|
+
def run_strategy!(some_args)
|
209
|
+
strategy.call(some_args, other_args)
|
187
210
|
end
|
188
211
|
end
|
189
212
|
|
190
213
|
MyARObject.create!(module_field: :Strategy1).run_strategy! #=> "strategy1 called"
|
191
|
-
MyARObject.create!(module_field: :
|
214
|
+
MyARObject.create!(module_field: :Strategy2).run_strategy! #=> "strategy2 called"
|
192
215
|
```
|
193
216
|
|
194
217
|
|
@@ -250,6 +273,10 @@ this is how you could do so:
|
|
250
273
|
# Provider domain Object
|
251
274
|
module Provider
|
252
275
|
# As if the domain model class
|
276
|
+
def self.all
|
277
|
+
[Ebay, Amazon]
|
278
|
+
end
|
279
|
+
|
253
280
|
module Base
|
254
281
|
def do_something!
|
255
282
|
"do something with #{something_from_an_instance}"
|
@@ -274,11 +301,6 @@ module Provider
|
|
274
301
|
"the amazon provider config"
|
275
302
|
end
|
276
303
|
end
|
277
|
-
|
278
|
-
# As if the domain model class
|
279
|
-
def self.all
|
280
|
-
[Ebay, Amazon]
|
281
|
-
end
|
282
304
|
end
|
283
305
|
```
|
284
306
|
|
@@ -345,6 +367,18 @@ Java/C# enums allow defining methods on the enum, which are shared across all en
|
|
345
367
|
|
346
368
|
```ruby
|
347
369
|
module PipelineStage
|
370
|
+
module_function
|
371
|
+
|
372
|
+
def all
|
373
|
+
[InitialContact, InNegotiations, LostDeal, PaidOut]
|
374
|
+
end
|
375
|
+
|
376
|
+
def cast(stage)
|
377
|
+
self.all.map(&:external_provider_code).find{|code| code == stage} ||
|
378
|
+
self.all.map(&:database_representation).find{|code| code == stage} ||
|
379
|
+
self.all.map(&:frontend_representation).find{|code| code == stage}
|
380
|
+
end
|
381
|
+
|
348
382
|
module Base
|
349
383
|
def external_provider_code
|
350
384
|
@external_provider_code ||= self.name.underscore
|
@@ -374,18 +408,6 @@ module PipelineStage
|
|
374
408
|
module PaidOut
|
375
409
|
extend Base
|
376
410
|
end
|
377
|
-
|
378
|
-
module_function
|
379
|
-
|
380
|
-
def all
|
381
|
-
[InitialContact, InNegotiations, LostDeal, PaidOut]
|
382
|
-
end
|
383
|
-
|
384
|
-
def cast(stage)
|
385
|
-
self.all.map(&:external_provider_code).find{|code| code == stage} ||
|
386
|
-
self.all.map(&:database_representation).find{|code| code == stage} ||
|
387
|
-
self.all.map(&:frontend_representation).find{|code| code == stage}
|
388
|
-
end
|
389
411
|
end
|
390
412
|
|
391
413
|
class MyARObject < ActiveRecord::Base
|
@@ -7,10 +7,10 @@ module ActiveModule
|
|
7
7
|
|
8
8
|
def =~(other)
|
9
9
|
case other
|
10
|
-
when ::String
|
11
|
-
(@possible_names ||= possible_names).include?(other)
|
12
10
|
when ::Symbol
|
13
|
-
|
11
|
+
possible_symbol_names_set.include?(other)
|
12
|
+
when ::String
|
13
|
+
possible_symbol_names_set.include?(other.to_sym)
|
14
14
|
else
|
15
15
|
self == other
|
16
16
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_module
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pedro Rolo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-12-
|
11
|
+
date: 2024-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|