active_module 0.1.5 → 0.1.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 31dff5b0baa89fa4624fc7b974f5261d483cdaf48f7b7fe9a5d1f0a7f256d7fa
4
- data.tar.gz: fbb6655e9de6c118ee99e199ed4ce78441cd55d151d839f7bf0040fa48519df5
3
+ metadata.gz: 191f7c2deb3a07261247a4302436cc734541ab62c3db9dd742974d58320dd763
4
+ data.tar.gz: bd20cb4d1aa7b6437facfaefc8213485571497df5e9e607ce19d16b81db246b8
5
5
  SHA512:
6
- metadata.gz: 0ed009054605141713db46f3d768dc044f42d5bc8ac479ae8004408eaa9ad4657694d661299ba600868975bf7f254da00315d64564d1dfcd96a942ad6622dada
7
- data.tar.gz: 4053a0f71c9b2b8db76c1f7637a989646c7b8ad531fa5da680c9795e61ec967fe9db13c74b566350039c13cdaf16d1a0c80f58e04944650e5e2c2f78d73a017f
6
+ metadata.gz: bc48c3f11e8f71321ab4abece1b5d58f671c96de7e494a5f2f7a8e55b9946c6e88af9e61970bdd2332eba12ee74dadec13485555dff0f05a4f74209c1327fd96
7
+ data.tar.gz: 126c9c4a2d885cadbe472dc23caa5025415f2806411e9eecce37a5b40c28ce990c5956246646292e9780b6f30d6f77705ab681899a7532940dbe0cfe59f2a471
data/README.md CHANGED
@@ -17,6 +17,35 @@ This is a very generic mechanism that enables many possible utilizations, for in
17
17
 
18
18
  You can find examples of these in [Usage -> Examples :](#Examples)
19
19
 
20
+ ## TL;DR
21
+
22
+ Declare module attributes like this:
23
+ ```ruby
24
+ class MyARObject < ActiveRecord::Base
25
+ attribute :module_field,
26
+ :active_module,
27
+ possible_modules: [MyModule1, MyClass, Nested::Module]
28
+ end
29
+ ```
30
+
31
+ Assign them like this:
32
+ ```ruby
33
+ object.module_field = Nested::Module
34
+ object.module_field = :Module
35
+ object.module_field = "Module"
36
+ object.module_field #=> Nested::Module:Module
37
+ ```
38
+
39
+ And compare them like this (optional):
40
+ ```ruby
41
+ module MyNameSpace
42
+ using ActiveModule::Comparison
43
+
44
+ object.module_field =~ :Module1
45
+ object.module_field =~ "Module1"
46
+ end
47
+ ```
48
+
20
49
  ## Installation
21
50
 
22
51
  Add to your gemfile - and if you are using rails - that's all you need:
@@ -54,7 +83,7 @@ class MyARObject < ActiveRecord::Base
54
83
  module MyModule1; end
55
84
  module MyModule2; end
56
85
  class MyClass;
57
- module Module1; end
86
+ module MyModule1; end
58
87
  end
59
88
  end
60
89
  ```
@@ -63,13 +92,13 @@ You can make the field refer to one of these modules/classes like this:
63
92
  class MyARObject < ActiveRecord::Base
64
93
  attribute :module_field,
65
94
  :active_module,
66
- possible_modules: [MyModule1, MyModule2, MyClass, MyClass::Module1]
95
+ possible_modules: [MyModule1, MyModule2, MyClass, MyClass::MyModule1]
67
96
  end
68
97
  ```
69
98
  And this is it! Easy!<br>
70
99
 
71
100
  ### Assigning and querying module attributes
72
- Now you can use this attribute in many handy ways:
101
+ Now you can use this attribute in many handy ways!
73
102
  <br>
74
103
  <br>
75
104
  For instance, you may refer to it using module literals:
@@ -118,10 +147,25 @@ only available within the namespace that includes the refinement.
118
147
  ```ruby
119
148
  module YourClassOrModuleThatWantsToCompare
120
149
  using ActiveModule::Comparison
150
+
121
151
  def method_that_compares
122
152
  my_ar_object.module_field =~ :MyModule1
123
153
  end
124
154
  end
155
+ ```
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
+
125
169
 
126
170
  ## Examples
127
171
 
@@ -130,6 +174,19 @@ end
130
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),
131
175
  and multiple-polymorphism (by composing an object of multiple strategies).
132
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
+
133
190
  ```ruby
134
191
  class MyARObject < ActiveRecord::Base
135
192
  module Strategy1
@@ -148,16 +205,13 @@ class MyARObject < ActiveRecord::Base
148
205
  :active_module,
149
206
  possible_modules: [Strategy1, Strategy2]
150
207
 
151
- def run_strategy!
152
- # here we could pass arguments to the strategy, and if
153
- # in this case strategies were classes we could also
154
- # instantiate them
155
- strategy.call
208
+ def run_strategy!(some_args)
209
+ strategy.call(some_args, other_args)
156
210
  end
157
211
  end
158
212
 
159
213
  MyARObject.create!(module_field: :Strategy1).run_strategy! #=> "strategy1 called"
160
- MyARObject.create!(module_field: :Strategy1).run_strategy! #=> "strategy2 called"
214
+ MyARObject.create!(module_field: :Strategy2).run_strategy! #=> "strategy2 called"
161
215
  ```
162
216
 
163
217
 
@@ -219,6 +273,10 @@ this is how you could do so:
219
273
  # Provider domain Object
220
274
  module Provider
221
275
  # As if the domain model class
276
+ def self.all
277
+ [Ebay, Amazon]
278
+ end
279
+
222
280
  module Base
223
281
  def do_something!
224
282
  "do something with #{something_from_an_instance}"
@@ -243,11 +301,6 @@ module Provider
243
301
  "the amazon provider config"
244
302
  end
245
303
  end
246
-
247
- # As if the domain model class
248
- def self.all
249
- [Ebay, Amazon]
250
- end
251
304
  end
252
305
  ```
253
306
 
@@ -314,6 +367,18 @@ Java/C# enums allow defining methods on the enum, which are shared across all en
314
367
 
315
368
  ```ruby
316
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
+
317
382
  module Base
318
383
  def external_provider_code
319
384
  @external_provider_code ||= self.name.underscore
@@ -343,18 +408,6 @@ module PipelineStage
343
408
  module PaidOut
344
409
  extend Base
345
410
  end
346
-
347
- module_function
348
-
349
- def all
350
- [InitialContact, InNegotiations, LostDeal, PaidOut]
351
- end
352
-
353
- def cast(stage)
354
- self.all.map(&:external_provider_code).find{|code| code == stage} ||
355
- self.all.map(&:database_representation).find{|code| code == stage} ||
356
- self.all.map(&:frontend_representation).find{|code| code == stage}
357
- end
358
411
  end
359
412
 
360
413
  class MyARObject < ActiveRecord::Base
@@ -7,16 +7,12 @@ module ActiveModule
7
7
 
8
8
  def =~(other)
9
9
  case other
10
- when nil
11
- false
12
- when ::Module
13
- super(other)
14
- when ::String
15
- (@possible_names ||= possible_names).include?(other)
16
10
  when ::Symbol
17
- (@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)
18
14
  else
19
- false
15
+ self == other
20
16
  end
21
17
  end
22
18
  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.5"
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.5
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