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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1473d88ff3d53e8948747dc7827ad2836478a0c6db139c54e11f2a96df58e758
4
- data.tar.gz: 045e7b7689734cf7c98654ec1283a747063d1defba78c47e3efbd115c5046675
3
+ metadata.gz: 191f7c2deb3a07261247a4302436cc734541ab62c3db9dd742974d58320dd763
4
+ data.tar.gz: bd20cb4d1aa7b6437facfaefc8213485571497df5e9e607ce19d16b81db246b8
5
5
  SHA512:
6
- metadata.gz: 67321659d14a0aff48e5917e2242d42773dbc6f86a8fa8cef935956bbda4717858128a405264ff39cde9bd06e223ee266b08a4f4b07a62f600ee3b6de8c54714
7
- data.tar.gz: bf7305eabb2c4d81e0514542c7172e440aaf5b17ea5d4bf732705100f8a92cc01f772aae9b3ba0f864c575f1040626d7730f5831d7d7889aec0ed88980f5271a
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
- # here we could pass arguments to the strategy, and if
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: :Strategy1).run_strategy! #=> "strategy2 called"
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
- (@possible_names ||= possible_names).include?(other.to_s)
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
@@ -14,6 +14,10 @@ module ActiveModule
14
14
  end
15
15
  end
16
16
 
17
+ def possible_symbol_names_set
18
+ @possible_symbol_names_set ||= Set.new(possible_names.map(&:to_sym))
19
+ end
20
+
17
21
  def qualified_name
18
22
  "::#{name}"
19
23
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveModule
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.7"
5
5
  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.6
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-17 00:00:00.000000000 Z
11
+ date: 2024-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord