active_module 0.10.0 → 0.10.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.
- checksums.yaml +4 -4
- data/README.md +92 -43
- data/lib/active_module/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: aa23c059556f8dd038668b7869fc36ed340a580852d0900ccc762c5d98fee918
|
|
4
|
+
data.tar.gz: d9a57951f8a69d340c1cddb24504ad6dbae73af4a1e2d5561174aba0da626456
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4c2f5665b1f81f317521ff8abf65c2e63259451d5e76f6220ff5458863053a2113dbbea6ae136075d2a2ac4105b20490c7b00866550bdc68547fa2613bc48472
|
|
7
|
+
data.tar.gz: 925d463454bd986f9335d6a299883f1f6d1e6783547438d3b8c8d173ffeccaab1ee7329403b4c18936feed8c12553620726aae22f22bcbd497bdbdd001ed83d8
|
data/README.md
CHANGED
|
@@ -44,7 +44,8 @@ Assign them like this:
|
|
|
44
44
|
object.module_field = Nested::Module
|
|
45
45
|
object.module_field = :Module
|
|
46
46
|
object.module_field = "Module"
|
|
47
|
-
object.module_field = :nested_module
|
|
47
|
+
object.module_field = :nested_module
|
|
48
|
+
object.module_field = :module
|
|
48
49
|
object.module_field #=> Nested::Module
|
|
49
50
|
```
|
|
50
51
|
|
|
@@ -54,7 +55,8 @@ Query them like this:
|
|
|
54
55
|
MyARObject.where(module_field: Nested::Module)
|
|
55
56
|
MyARObject.where(module_field: :Module)
|
|
56
57
|
MyARObject.where(module_field: "Module")
|
|
57
|
-
MyARObject.where(module_field: :nested_module)
|
|
58
|
+
MyARObject.where(module_field: :nested_module)
|
|
59
|
+
MyARObject.where(module_field: :module)
|
|
58
60
|
```
|
|
59
61
|
|
|
60
62
|
And compare them like this:
|
|
@@ -67,7 +69,9 @@ module MyNameSpace
|
|
|
67
69
|
|
|
68
70
|
object.module_field =~ :Module
|
|
69
71
|
object.module_field =~ "Module"
|
|
70
|
-
object.module_field =~ :nested_module
|
|
72
|
+
object.module_field =~ :nested_module
|
|
73
|
+
object.module_field =~ :module
|
|
74
|
+
|
|
71
75
|
end
|
|
72
76
|
```
|
|
73
77
|
|
|
@@ -76,7 +80,7 @@ end
|
|
|
76
80
|
Add to your gemfile - and if you are using rails - that's all you need:
|
|
77
81
|
|
|
78
82
|
```ruby
|
|
79
|
-
gem "active_module", "~> 0.
|
|
83
|
+
gem "active_module", "~> 0.10"
|
|
80
84
|
```
|
|
81
85
|
|
|
82
86
|
If you are not using rails, just issue this command after loading active record
|
|
@@ -159,10 +163,13 @@ The most ergonomic way is to use underscored symbols. For flat modules,
|
|
|
159
163
|
use the underscored name directly:
|
|
160
164
|
|
|
161
165
|
```ruby
|
|
166
|
+
MyARObject.create!(module_field: MyARObject::MyModule1)
|
|
162
167
|
MyARObject.create!(module_field: :my_module1)
|
|
163
168
|
|
|
169
|
+
MyARObject.where(module_field: MyARObject::MyModule1)
|
|
164
170
|
MyARObject.where(module_field: :my_module1)
|
|
165
171
|
|
|
172
|
+
my_ar_object.module_field = MyARObject::MyModule1
|
|
166
173
|
my_ar_object.module_field = :my_module1
|
|
167
174
|
|
|
168
175
|
my_ar_object.module_field #=> MyARObject::MyModule1
|
|
@@ -172,10 +179,13 @@ Nested modules can be referenced using underscored symbols at any
|
|
|
172
179
|
nesting level:
|
|
173
180
|
|
|
174
181
|
```ruby
|
|
182
|
+
MyARObject.create!(module_field: MyARObject::MyClass::MyModule1)
|
|
175
183
|
MyARObject.create!(module_field: :my_class_my_module1) # partial nesting
|
|
176
|
-
MyARObject.create!(module_field: :my_module1)
|
|
184
|
+
MyARObject.create!(module_field: :my_module1)
|
|
185
|
+
MyARObject.where(module_field: MyARObject::MyClass::MyModule1)
|
|
177
186
|
MyARObject.where(module_field: :my_class_my_module1) # all segments joined
|
|
178
187
|
|
|
188
|
+
my_ar_object.module_field = MyARObject::MyClass::MyModule1
|
|
179
189
|
my_ar_object.module_field = :my_class_my_module1
|
|
180
190
|
my_ar_object.module_field #=> MyARObject::MyClass::MyModule1
|
|
181
191
|
```
|
|
@@ -188,11 +198,15 @@ and querying:
|
|
|
188
198
|
# Given possible_modules: [MyModule1, MyClass::MyModule1]
|
|
189
199
|
# :MyModule1 resolves to the flat MyModule1 (not MyClass::MyModule1)
|
|
190
200
|
|
|
201
|
+
MyARObject.create!(module_field: MyARObject::MyModule1)
|
|
191
202
|
MyARObject.create!(module_field: :MyModule1) # sets to MyModule1
|
|
203
|
+
MyARObject.where(module_field: MyARObject::MyModule1) # filters by MyModule1
|
|
192
204
|
MyARObject.where(module_field: :MyModule1) # filters by MyModule1
|
|
205
|
+
my_ar_object.module_field = MyARObject::MyModule1
|
|
193
206
|
my_ar_object.module_field = :MyModule1 # assigns MyModule1
|
|
194
207
|
|
|
195
208
|
# Use the underscored nested name for the nested module
|
|
209
|
+
MyARObject.where(module_field: MyARObject::MyClass::MyModule1)
|
|
196
210
|
MyARObject.where(module_field: :my_class_my_module1) # filters by MyClass::MyModule1
|
|
197
211
|
```
|
|
198
212
|
|
|
@@ -200,21 +214,24 @@ You can always refer to modules using their fully qualified names
|
|
|
200
214
|
via symbols or module literals:
|
|
201
215
|
|
|
202
216
|
```ruby
|
|
203
|
-
MyARObject.create!(module_field: :MyModule1)
|
|
204
217
|
MyARObject.create!(module_field: MyARObject::MyModule1)
|
|
218
|
+
MyARObject.create!(module_field: :MyModule1)
|
|
205
219
|
|
|
206
|
-
MyARObject.where(module_field: :MyModule1)
|
|
207
220
|
MyARObject.where(module_field: MyARObject::MyModule1)
|
|
221
|
+
MyARObject.where(module_field: :MyModule1)
|
|
208
222
|
```
|
|
209
223
|
|
|
210
224
|
And if there is the need for disambiguation, you can always use
|
|
211
225
|
fully qualified strings:
|
|
212
226
|
|
|
213
227
|
```ruby
|
|
228
|
+
MyARObject.create!(module_field: MyARObject::MyClass::MyModule1)
|
|
214
229
|
MyARObject.create!(module_field: "MyClass::MyModule1")
|
|
215
230
|
|
|
231
|
+
MyARObject.where(module_field: MyARObject::MyClass::MyModule1)
|
|
216
232
|
MyARObject.where(module_field: "MyClass::MyModule1")
|
|
217
233
|
|
|
234
|
+
my_ar_object.module_field = MyARObject::MyClass::MyModule1
|
|
218
235
|
my_ar_object.module_field = "MyClass::MyModule1"
|
|
219
236
|
|
|
220
237
|
my_ar_object.module_field #=> MyARObject::MyClass::MyModule1
|
|
@@ -231,6 +248,8 @@ module YourClassOrModuleThatWantsToCompare
|
|
|
231
248
|
using ActiveModule::Comparison
|
|
232
249
|
|
|
233
250
|
def method_that_compares
|
|
251
|
+
my_ar_object.module_field == MyARObject::MyModule1
|
|
252
|
+
my_ar_object.module_field =~ MyARObject::MyModule1
|
|
234
253
|
my_ar_object.module_field =~ :my_module1 # underscored name
|
|
235
254
|
my_ar_object.module_field =~ :my_class_my_module1 # nested underscored
|
|
236
255
|
my_ar_object.module_field =~ "MyClass::MyModule1" # fully qualified string
|
|
@@ -241,13 +260,16 @@ end
|
|
|
241
260
|
or like this, if you don't want to use the refinement:
|
|
242
261
|
|
|
243
262
|
```ruby
|
|
263
|
+
ActiveModule::Comparison.compare(
|
|
264
|
+
my_ar_object.module_field, MyARObject::MyClass::MyModule1
|
|
265
|
+
)
|
|
244
266
|
ActiveModule::Comparison.compare(my_ar_object.module_field, :my_module1)
|
|
245
267
|
```
|
|
246
268
|
|
|
247
269
|
but in this last case it would probably make more sense to simply use a module literal:
|
|
248
270
|
|
|
249
271
|
```ruby
|
|
250
|
-
my_ar_object.module_field == MyClass::MyModule1
|
|
272
|
+
my_ar_object.module_field == MyARObject::MyClass::MyModule1
|
|
251
273
|
```
|
|
252
274
|
|
|
253
275
|
|
|
@@ -300,8 +322,10 @@ class MyARObject < ActiveRecord::Base
|
|
|
300
322
|
end
|
|
301
323
|
end
|
|
302
324
|
|
|
303
|
-
MyARObject.create!(strategy:
|
|
304
|
-
MyARObject.create!(strategy: :
|
|
325
|
+
MyARObject.create!(strategy: MyARObject::Strategy1).run_strategy! #=> "strategy1 called"
|
|
326
|
+
MyARObject.create!(strategy: :Strategy1).run_strategy! #=> "strategy1 called"
|
|
327
|
+
MyARObject.create!(strategy: MyARObject::Strategy2).run_strategy! #=> "strategy2 called"
|
|
328
|
+
MyARObject.create!(strategy: :Strategy2).run_strategy! #=> "strategy2 called"
|
|
305
329
|
```
|
|
306
330
|
|
|
307
331
|
You can later easily promote these modules to classes if you need instance variables:
|
|
@@ -333,8 +357,10 @@ class MyARObject < ActiveRecord::Base
|
|
|
333
357
|
end
|
|
334
358
|
end
|
|
335
359
|
|
|
336
|
-
MyARObject.create!(strategy:
|
|
337
|
-
MyARObject.create!(strategy: :
|
|
360
|
+
MyARObject.create!(strategy: MyARObject::Strategy1).run_strategy! #=> "strategy1 called"
|
|
361
|
+
MyARObject.create!(strategy: :Strategy1).run_strategy! #=> "strategy1 called"
|
|
362
|
+
MyARObject.create!(strategy: MyARObject::Strategy2).run_strategy! #=> "strategy2 called"
|
|
363
|
+
MyARObject.create!(strategy: :Strategy2).run_strategy! #=> "strategy2 called"
|
|
338
364
|
```
|
|
339
365
|
|
|
340
366
|
|
|
@@ -368,10 +394,14 @@ class MyARObject < ActiveRecord::Base
|
|
|
368
394
|
possible_modules: Provider.all
|
|
369
395
|
end
|
|
370
396
|
|
|
397
|
+
MyARObject.create!(provider: Provider::Ebay).provider.do_something!
|
|
398
|
+
#=> "do something with the ebay provider config"
|
|
371
399
|
MyARObject.create!(provider: :Ebay).provider.do_something!
|
|
372
400
|
#=> "do something with the ebay provider config"
|
|
373
401
|
MyARObject.create!(provider: Provider::Amazon).provider.do_something!
|
|
374
402
|
#=> "do something with the amazon provider config"
|
|
403
|
+
MyARObject.create!(provider: :Amazon).provider.do_something!
|
|
404
|
+
#=> "do something with the amazon provider config"
|
|
375
405
|
```
|
|
376
406
|
|
|
377
407
|
What is interesting about this is that we can later easily promote
|
|
@@ -477,6 +507,7 @@ class MyARObject < ActiveRecord::Base
|
|
|
477
507
|
end
|
|
478
508
|
end
|
|
479
509
|
|
|
510
|
+
MyARObject.create!(provider_config: ProviderConfig::Ebay).load_page!
|
|
480
511
|
MyARObject.create!(provider_config: :Ebay).load_page!
|
|
481
512
|
```
|
|
482
513
|
|
|
@@ -490,31 +521,33 @@ Nested modules can always be referenced using underscored names at any level of
|
|
|
490
521
|
For example, given a deeply nested module:
|
|
491
522
|
|
|
492
523
|
```ruby
|
|
493
|
-
module
|
|
494
|
-
module
|
|
495
|
-
module
|
|
524
|
+
module Workflow
|
|
525
|
+
module Pipeline
|
|
526
|
+
module InitialContact; end
|
|
496
527
|
end
|
|
497
528
|
end
|
|
498
529
|
|
|
499
530
|
class MyARObject < ActiveRecord::Base
|
|
500
|
-
attribute :
|
|
531
|
+
attribute :pipeline_stage,
|
|
501
532
|
:active_module,
|
|
502
|
-
possible_modules: [
|
|
533
|
+
possible_modules: [Workflow::Pipeline::InitialContact]
|
|
503
534
|
end
|
|
504
535
|
```
|
|
505
536
|
|
|
506
|
-
All of the following resolve to `
|
|
537
|
+
All of the following resolve to `Workflow::Pipeline::InitialContact`:
|
|
507
538
|
|
|
508
539
|
```ruby
|
|
509
|
-
MyARObject.create!(
|
|
510
|
-
MyARObject.create!(
|
|
511
|
-
MyARObject.create!(
|
|
540
|
+
MyARObject.create!(pipeline_stage: Workflow::Pipeline::InitialContact)
|
|
541
|
+
MyARObject.create!(pipeline_stage: :initial_contact) # last segment only
|
|
542
|
+
MyARObject.create!(pipeline_stage: :pipeline_initial_contact) # last two segments
|
|
543
|
+
MyARObject.create!(pipeline_stage: :workflow_pipeline_initial_contact) # all segments
|
|
512
544
|
```
|
|
513
545
|
|
|
514
546
|
This also works for querying:
|
|
515
547
|
|
|
516
548
|
```ruby
|
|
517
|
-
MyARObject.where(
|
|
549
|
+
MyARObject.where(pipeline_stage: Workflow::Pipeline::InitialContact)
|
|
550
|
+
MyARObject.where(pipeline_stage: :pipeline_initial_contact)
|
|
518
551
|
```
|
|
519
552
|
|
|
520
553
|
And for comparison (with `ActiveModule::Comparison`):
|
|
@@ -528,7 +561,13 @@ module MyModuleOrClass
|
|
|
528
561
|
end
|
|
529
562
|
end
|
|
530
563
|
|
|
531
|
-
MyModuleOrClass.match?(
|
|
564
|
+
MyModuleOrClass.match?(
|
|
565
|
+
Workflow::Pipeline::InitialContact,
|
|
566
|
+
Workflow::Pipeline::InitialContact
|
|
567
|
+
) #=> true
|
|
568
|
+
MyModuleOrClass.match?(
|
|
569
|
+
Workflow::Pipeline::InitialContact, :pipeline_initial_contact
|
|
570
|
+
) #=> true
|
|
532
571
|
```
|
|
533
572
|
|
|
534
573
|
#### `active_module_enum` — generating query, bang, and scope methods
|
|
@@ -573,6 +612,7 @@ This generates:
|
|
|
573
612
|
|
|
574
613
|
```ruby
|
|
575
614
|
# Instance query methods (?)
|
|
615
|
+
object = MyARObject.new(pipeline_stage: PipelineStage::InitialContact)
|
|
576
616
|
object = MyARObject.new(pipeline_stage: :initial_contact)
|
|
577
617
|
object.initial_contact? #=> true
|
|
578
618
|
object.lost_deal? #=> false
|
|
@@ -607,6 +647,7 @@ MyARObject.pipeline_stages.values #=> ["PipelineStage::InitialContact", ...]
|
|
|
607
647
|
All forms of underscored symbol names work for assignment and querying:
|
|
608
648
|
|
|
609
649
|
```ruby
|
|
650
|
+
MyARObject.create!(pipeline_stage: PipelineStage::InitialContact)
|
|
610
651
|
MyARObject.create!(pipeline_stage: :initial_contact)
|
|
611
652
|
MyARObject.create!(pipeline_stage: :in_negotiations)
|
|
612
653
|
MyARObject.where(pipeline_stage: :lost_deal)
|
|
@@ -616,24 +657,27 @@ MyARObject.where(pipeline_stage: :paid_out)
|
|
|
616
657
|
For nested modules, methods are generated at **all nesting levels**:
|
|
617
658
|
|
|
618
659
|
```ruby
|
|
619
|
-
module
|
|
620
|
-
module
|
|
621
|
-
module
|
|
660
|
+
module Workflow
|
|
661
|
+
module Pipeline
|
|
662
|
+
module InitialContact; end
|
|
622
663
|
end
|
|
623
664
|
end
|
|
624
665
|
|
|
625
|
-
class
|
|
626
|
-
attribute :
|
|
627
|
-
possible_modules: [
|
|
628
|
-
active_module_enum :
|
|
666
|
+
class PipelineEvent < ActiveRecord::Base
|
|
667
|
+
attribute :pipeline_stage, :active_module,
|
|
668
|
+
possible_modules: [Workflow::Pipeline::InitialContact]
|
|
669
|
+
active_module_enum :pipeline_stage
|
|
629
670
|
end
|
|
630
671
|
|
|
631
|
-
object =
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
object
|
|
672
|
+
object = PipelineEvent.new(
|
|
673
|
+
pipeline_stage: Workflow::Pipeline::InitialContact
|
|
674
|
+
)
|
|
675
|
+
object = PipelineEvent.new(pipeline_stage: :initial_contact)
|
|
676
|
+
object.initial_contact? #=> true
|
|
677
|
+
object.pipeline_initial_contact? #=> true (partial nesting)
|
|
678
|
+
object.workflow_pipeline_initial_contact? #=> true (full nesting)
|
|
635
679
|
|
|
636
|
-
|
|
680
|
+
PipelineEvent.with_pipeline_initial_contact #=> ActiveRecord::Relation
|
|
637
681
|
```
|
|
638
682
|
|
|
639
683
|
The pluralized attribute name method also works with nested modules.
|
|
@@ -647,14 +691,14 @@ module Nested
|
|
|
647
691
|
module StatusB; end
|
|
648
692
|
end
|
|
649
693
|
|
|
650
|
-
class
|
|
651
|
-
attribute :
|
|
694
|
+
class PipelineRecord < ActiveRecord::Base
|
|
695
|
+
attribute :stage, :active_module,
|
|
652
696
|
possible_modules: [StatusA, StatusB,
|
|
653
697
|
Nested::StatusA, Nested::StatusB]
|
|
654
|
-
active_module_enum :
|
|
698
|
+
active_module_enum :stage
|
|
655
699
|
end
|
|
656
700
|
|
|
657
|
-
|
|
701
|
+
PipelineRecord.stages
|
|
658
702
|
#=> { StatusA => "StatusA", StatusB => "StatusB",
|
|
659
703
|
# Nested::StatusA => "Nested::StatusA",
|
|
660
704
|
# Nested::StatusB => "Nested::StatusB" }
|
|
@@ -663,10 +707,14 @@ Fruit.kinds
|
|
|
663
707
|
All underscored forms work for assignment and querying:
|
|
664
708
|
|
|
665
709
|
```ruby
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
710
|
+
PipelineEvent.create!(
|
|
711
|
+
pipeline_stage: Workflow::Pipeline::InitialContact
|
|
712
|
+
)
|
|
713
|
+
PipelineEvent.create!(pipeline_stage: :initial_contact)
|
|
714
|
+
PipelineEvent.create!(pipeline_stage: :pipeline_initial_contact)
|
|
715
|
+
PipelineEvent.create!(pipeline_stage: :workflow_pipeline_initial_contact)
|
|
716
|
+
PipelineEvent.where(pipeline_stage: Workflow::Pipeline::InitialContact)
|
|
717
|
+
PipelineEvent.where(pipeline_stage: :pipeline_initial_contact)
|
|
670
718
|
```
|
|
671
719
|
|
|
672
720
|
##### Options
|
|
@@ -718,6 +766,7 @@ class MyARObject < ActiveRecord::Base
|
|
|
718
766
|
end
|
|
719
767
|
|
|
720
768
|
# Assignment resolves to the flat module
|
|
769
|
+
object = MyARObject.new(status: Status)
|
|
721
770
|
object = MyARObject.new(status: :status)
|
|
722
771
|
object.status #=> Status (flat, not Nested::Status)
|
|
723
772
|
|