anchormodel 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/.ruby-version +1 -1
  3. data/CHANGELOG.md +12 -0
  4. data/Gemfile +3 -0
  5. data/Gemfile.lock +206 -0
  6. data/README.md +10 -5
  7. data/Rakefile +23 -3
  8. data/anchormodel.gemspec +27 -11
  9. data/doc/Anchormodel/ActiveModelTypeValue.html +10 -6
  10. data/doc/Anchormodel/Attribute.html +21 -14
  11. data/doc/Anchormodel/ModelMixin.html +14 -9
  12. data/doc/Anchormodel/Version.html +3 -3
  13. data/doc/Anchormodel.html +141 -53
  14. data/doc/_index.html +2 -2
  15. data/doc/file.README.html +37 -48
  16. data/doc/index.html +37 -48
  17. data/doc/method_list.html +8 -0
  18. data/doc/top-level-namespace.html +2 -2
  19. data/lib/anchormodel/attribute.rb +2 -2
  20. data/lib/anchormodel/model_mixin.rb +1 -1
  21. data/lib/anchormodel/version.rb +2 -2
  22. data/lib/anchormodel.rb +14 -1
  23. data/logo.svg +98 -0
  24. data/test/active_record_model/user_test.rb +51 -0
  25. data/test/dummy/.gitignore +29 -0
  26. data/test/dummy/Rakefile +6 -0
  27. data/test/dummy/app/anchormodels/locale.rb +6 -0
  28. data/test/dummy/app/anchormodels/role.rb +14 -0
  29. data/test/dummy/app/helpers/application_helper.rb +2 -0
  30. data/test/dummy/app/models/application_record.rb +5 -0
  31. data/test/dummy/app/models/concerns/.keep +0 -0
  32. data/test/dummy/app/models/user.rb +4 -0
  33. data/test/dummy/bin/rails +4 -0
  34. data/test/dummy/bin/rake +4 -0
  35. data/test/dummy/bin/setup +33 -0
  36. data/test/dummy/config/application.rb +37 -0
  37. data/test/dummy/config/boot.rb +3 -0
  38. data/test/dummy/config/credentials.yml.enc +1 -0
  39. data/test/dummy/config/database.yml +8 -0
  40. data/test/dummy/config/environment.rb +5 -0
  41. data/test/dummy/config/environments/test.rb +50 -0
  42. data/test/dummy/config/initializers/content_security_policy.rb +25 -0
  43. data/test/dummy/config/initializers/filter_parameter_logging.rb +8 -0
  44. data/test/dummy/config/initializers/inflections.rb +16 -0
  45. data/test/dummy/config/initializers/permissions_policy.rb +11 -0
  46. data/test/dummy/config/locales/en.yml +33 -0
  47. data/test/dummy/config/puma.rb +43 -0
  48. data/test/dummy/config/routes.rb +6 -0
  49. data/test/dummy/config.ru +6 -0
  50. data/test/dummy/db/migrate/20230107173151_create_users.rb +10 -0
  51. data/test/dummy/db/schema.rb +21 -0
  52. data/test/dummy/db/seeds.rb +7 -0
  53. data/test/dummy/lib/tasks/.keep +0 -0
  54. data/test/dummy/log/.keep +0 -0
  55. data/test/dummy/tmp/.keep +0 -0
  56. data/test/dummy/tmp/pids/.keep +0 -0
  57. data/test/test_helper.rb +21 -0
  58. metadata +150 -13
data/doc/Anchormodel.html CHANGED
@@ -103,8 +103,8 @@
103
103
 
104
104
  <h2>Overview</h2><div class="docstring">
105
105
  <div class="discussion">
106
- <p>Inherit from this class and place your Anchormodel under <code>app/anchormodels/your_anchor_model.rb</code>. Use it like <code>Anchormodels::YourAnchorModel</code>.
107
- Refer to the README for usage.</p>
106
+
107
+ <p>Inherit from this class and place your Anchormodel under <code>app/anchormodels/your_anchor_model.rb</code>. Use it like <code>YourAnchorModel</code>. Refer to the README for usage.</p>
108
108
 
109
109
 
110
110
  </div>
@@ -215,7 +215,8 @@ Refer to the README for usage.</p>
215
215
 
216
216
 
217
217
 
218
- <span class="summary_desc"><div class='inline'><p>Returns all possible values this Anchormodel can take.</p>
218
+ <span class="summary_desc"><div class='inline'>
219
+ <p>Returns all possible values this Anchormodel can take.</p>
219
220
  </div></span>
220
221
 
221
222
  </li>
@@ -238,7 +239,32 @@ Refer to the README for usage.</p>
238
239
 
239
240
 
240
241
 
241
- <span class="summary_desc"><div class='inline'><p>Retrieves a particular value given the key.</p>
242
+ <span class="summary_desc"><div class='inline'>
243
+ <p>Retrieves a particular value given the key.</p>
244
+ </div></span>
245
+
246
+ </li>
247
+
248
+
249
+ <li class="public ">
250
+ <span class="summary_signature">
251
+
252
+ <a href="#setup!-class_method" title="setup! (class method)">.<strong>setup!</strong> &#x21d2; Object </a>
253
+
254
+
255
+
256
+ </span>
257
+
258
+
259
+
260
+
261
+
262
+
263
+
264
+
265
+
266
+ <span class="summary_desc"><div class='inline'>
267
+ <p>When a descendant of Anchormodel is first used, it must overwrite the class_attributes to prevent cross-class pollution.</p>
242
268
  </div></span>
243
269
 
244
270
  </li>
@@ -294,7 +320,8 @@ Refer to the README for usage.</p>
294
320
 
295
321
 
296
322
 
297
- <span class="summary_desc"><div class='inline'><p>Call this initializer directly in your Anchormodel class.</p>
323
+ <span class="summary_desc"><div class='inline'>
324
+ <p>Call this initializer directly in your Anchormodel class.</p>
298
325
  </div></span>
299
326
 
300
327
  </li>
@@ -339,7 +366,8 @@ Refer to the README for usage.</p>
339
366
 
340
367
 
341
368
 
342
- <span class="summary_desc"><div class='inline'><p>Returns a Rails label that is compatible with the <a href="https://github.com/grosser/gettext_i18n_rails/">Rails FastGettext</a> gem.</p>
369
+ <span class="summary_desc"><div class='inline'>
370
+ <p>Returns a Rails label that is compatible with the <a href="https://github.com/grosser/gettext_i18n_rails/">Rails FastGettext</a> gem.</p>
343
371
  </div></span>
344
372
 
345
373
  </li>
@@ -384,7 +412,8 @@ Refer to the README for usage.</p>
384
412
 
385
413
  </h3><div class="docstring">
386
414
  <div class="discussion">
387
- <p>Call this initializer directly in your Anchormodel class. To set <code>@foo=:bar</code> for anchor <code>:ter</code>, use <code>new(:ter, foo: :bar)</code></p>
415
+
416
+ <p>Call this initializer directly in your Anchormodel class. To set <code>@foo=:bar</code> for anchor <code>:ter</code>, use <code>new(:ter, foo: :bar)</code></p>
388
417
 
389
418
 
390
419
  </div>
@@ -403,7 +432,8 @@ Refer to the README for usage.</p>
403
432
 
404
433
 
405
434
  &mdash;
406
- <div class='inline'><p>The key under which the entry should be stored.</p>
435
+ <div class='inline'>
436
+ <p>The key under which the entry should be stored.</p>
407
437
  </div>
408
438
 
409
439
  </li>
@@ -418,7 +448,8 @@ Refer to the README for usage.</p>
418
448
 
419
449
 
420
450
  &mdash;
421
- <div class='inline'><p>All named arguments to Anchormodel are made available as instance attributes.</p>
451
+ <div class='inline'>
452
+ <p>All named arguments to Anchormodel are made available as instance attributes.</p>
422
453
  </div>
423
454
 
424
455
  </li>
@@ -432,27 +463,31 @@ Refer to the README for usage.</p>
432
463
  <pre class="lines">
433
464
 
434
465
 
435
- 27
436
- 28
437
- 29
438
- 30
439
- 31
440
- 32
441
- 33
442
- 34
443
- 35
444
- 36
445
- 37
446
466
  38
447
467
  39
448
468
  40
449
469
  41
450
- 42</pre>
470
+ 42
471
+ 43
472
+ 44
473
+ 45
474
+ 46
475
+ 47
476
+ 48
477
+ 49
478
+ 50
479
+ 51
480
+ 52
481
+ 53
482
+ 54
483
+ 55</pre>
451
484
  </td>
452
485
  <td>
453
- <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 27</span>
486
+ <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 38</span>
454
487
 
455
488
  <span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='op'>**</span><span class='id identifier rubyid_attributes'>attributes</span><span class='rparen'>)</span>
489
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_setup!'>setup!</span> <span class='kw'>unless</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_setup_completed'>setup_completed</span>
490
+
456
491
  <span class='ivar'>@key</span> <span class='op'>=</span> <span class='id identifier rubyid_key'>key</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>
457
492
  <span class='ivar'>@index</span> <span class='op'>=</span> <span class='id identifier rubyid_entries_list'>entries_list</span><span class='period'>.</span><span class='id identifier rubyid_count'>count</span>
458
493
 
@@ -578,7 +613,8 @@ Refer to the README for usage.</p>
578
613
 
579
614
  </h3><div class="docstring">
580
615
  <div class="discussion">
581
- <p>Returns all possible values this Anchormodel can take.</p>
616
+
617
+ <p>Returns all possible values this Anchormodel can take.</p>
582
618
 
583
619
 
584
620
  </div>
@@ -592,12 +628,12 @@ Refer to the README for usage.</p>
592
628
  <pre class="lines">
593
629
 
594
630
 
595
- 13
596
- 14
597
- 15</pre>
631
+ 24
632
+ 25
633
+ 26</pre>
598
634
  </td>
599
635
  <td>
600
- <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 13</span>
636
+ <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 24</span>
601
637
 
602
638
  <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_all'>all</span>
603
639
  <span class='id identifier rubyid_entries_list'>entries_list</span>
@@ -618,7 +654,8 @@ Refer to the README for usage.</p>
618
654
 
619
655
  </h3><div class="docstring">
620
656
  <div class="discussion">
621
- <p>Retrieves a particular value given the key. Fails if not found.</p>
657
+
658
+ <p>Retrieves a particular value given the key. Fails if not found.</p>
622
659
 
623
660
 
624
661
  </div>
@@ -637,7 +674,8 @@ Refer to the README for usage.</p>
637
674
 
638
675
 
639
676
  &mdash;
640
- <div class='inline'><p>The key of the value that should be retrieved.</p>
677
+ <div class='inline'>
678
+ <p>The key of the value that should be retrieved.</p>
641
679
  </div>
642
680
 
643
681
  </li>
@@ -651,13 +689,13 @@ Refer to the README for usage.</p>
651
689
  <pre class="lines">
652
690
 
653
691
 
654
- 19
655
- 20
656
- 21
657
- 22</pre>
692
+ 30
693
+ 31
694
+ 32
695
+ 33</pre>
658
696
  </td>
659
697
  <td>
660
- <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 19</span>
698
+ <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 30</span>
661
699
 
662
700
  <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_find'>find</span><span class='lparen'>(</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
663
701
  <span class='kw'>return</span> <span class='kw'>nil</span> <span class='kw'>if</span> <span class='id identifier rubyid_key'>key</span><span class='period'>.</span><span class='id identifier rubyid_nil?'>nil?</span>
@@ -666,6 +704,55 @@ Refer to the README for usage.</p>
666
704
  </td>
667
705
  </tr>
668
706
  </table>
707
+ </div>
708
+
709
+ <div class="method_details ">
710
+ <h3 class="signature " id="setup!-class_method">
711
+
712
+ .<strong>setup!</strong> &#x21d2; <tt>Object</tt>
713
+
714
+
715
+
716
+
717
+
718
+ </h3><div class="docstring">
719
+ <div class="discussion">
720
+
721
+ <p>When a descendant of Anchormodel is first used, it must overwrite the class_attributes to prevent cross-class pollution.</p>
722
+
723
+
724
+ </div>
725
+ </div>
726
+ <div class="tags">
727
+
728
+
729
+ </div><table class="source_code">
730
+ <tr>
731
+ <td>
732
+ <pre class="lines">
733
+
734
+
735
+ 15
736
+ 16
737
+ 17
738
+ 18
739
+ 19
740
+ 20
741
+ 21</pre>
742
+ </td>
743
+ <td>
744
+ <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 15</span>
745
+
746
+ <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_setup!'>setup!</span>
747
+ <span class='id identifier rubyid_fail'>fail</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>`setup!` was called twice for Anchormodel subclass </span><span class='embexpr_beg'>#{</span><span class='kw'>self</span><span class='embexpr_end'>}</span><span class='tstring_content'>.</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_setup_completed'>setup_completed</span>
748
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_entries_list'>entries_list</span> <span class='op'>=</span> <span class='id identifier rubyid_entries_list'>entries_list</span><span class='period'>.</span><span class='id identifier rubyid_dup'>dup</span>
749
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_entries_hash'>entries_hash</span> <span class='op'>=</span> <span class='id identifier rubyid_entries_hash'>entries_hash</span><span class='period'>.</span><span class='id identifier rubyid_dup'>dup</span>
750
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_valid_keys'>valid_keys</span> <span class='op'>=</span> <span class='id identifier rubyid_valid_keys'>valid_keys</span><span class='period'>.</span><span class='id identifier rubyid_dup'>dup</span>
751
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_setup_completed'>setup_completed</span> <span class='op'>=</span> <span class='kw'>true</span>
752
+ <span class='kw'>end</span></pre>
753
+ </td>
754
+ </tr>
755
+ </table>
669
756
  </div>
670
757
 
671
758
  </div>
@@ -698,12 +785,12 @@ Refer to the README for usage.</p>
698
785
  <pre class="lines">
699
786
 
700
787
 
701
- 44
702
- 45
703
- 46</pre>
788
+ 57
789
+ 58
790
+ 59</pre>
704
791
  </td>
705
792
  <td>
706
- <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 44</span>
793
+ <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 57</span>
707
794
 
708
795
  <span class='kw'>def</span> <span class='op'>==</span><span class='lparen'>(</span><span class='id identifier rubyid_other'>other</span><span class='rparen'>)</span>
709
796
  <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span> <span class='op'>==</span> <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span> <span class='op'>&amp;&amp;</span> <span class='id identifier rubyid_key'>key</span> <span class='op'>==</span> <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span>
@@ -737,12 +824,12 @@ Refer to the README for usage.</p>
737
824
  <pre class="lines">
738
825
 
739
826
 
740
- 53
741
- 54
742
- 55</pre>
827
+ 66
828
+ 67
829
+ 68</pre>
743
830
  </td>
744
831
  <td>
745
- <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 53</span>
832
+ <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 66</span>
746
833
 
747
834
  <span class='kw'>def</span> <span class='id identifier rubyid_inspect'>inspect</span>
748
835
  <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>#&lt;</span><span class='embexpr_beg'>#{</span><span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_name'>name</span><span class='embexpr_end'>}</span><span class='tstring_content'>&lt;</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_key'>key</span><span class='embexpr_end'>}</span><span class='tstring_content'>&gt;:</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_hash'>hash</span><span class='embexpr_end'>}</span><span class='tstring_content'>&gt;</span><span class='tstring_end'>&quot;</span></span>
@@ -763,7 +850,8 @@ Refer to the README for usage.</p>
763
850
 
764
851
  </h3><div class="docstring">
765
852
  <div class="discussion">
766
- <p>Returns a Rails label that is compatible with the <a href="https://github.com/grosser/gettext_i18n_rails/">Rails FastGettext</a> gem.</p>
853
+
854
+ <p>Returns a Rails label that is compatible with the <a href="https://github.com/grosser/gettext_i18n_rails/">Rails FastGettext</a> gem.</p>
767
855
 
768
856
 
769
857
  </div>
@@ -777,12 +865,12 @@ Refer to the README for usage.</p>
777
865
  <pre class="lines">
778
866
 
779
867
 
780
- 49
781
- 50
782
- 51</pre>
868
+ 62
869
+ 63
870
+ 64</pre>
783
871
  </td>
784
872
  <td>
785
- <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 49</span>
873
+ <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 62</span>
786
874
 
787
875
  <span class='kw'>def</span> <span class='id identifier rubyid_label'>label</span>
788
876
  <span class='const'>I18n</span><span class='period'>.</span><span class='id identifier rubyid_t'>t</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='embexpr_beg'>#{</span><span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='period'>.</span><span class='id identifier rubyid_name'>name</span><span class='period'>.</span><span class='id identifier rubyid_demodulize'>demodulize</span><span class='embexpr_end'>}</span><span class='tstring_content'>|</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_key'>key</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span><span class='period'>.</span><span class='id identifier rubyid_humanize'>humanize</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span>
@@ -816,12 +904,12 @@ Refer to the README for usage.</p>
816
904
  <pre class="lines">
817
905
 
818
906
 
819
- 57
820
- 58
821
- 59</pre>
907
+ 70
908
+ 71
909
+ 72</pre>
822
910
  </td>
823
911
  <td>
824
- <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 57</span>
912
+ <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 70</span>
825
913
 
826
914
  <span class='kw'>def</span> <span class='id identifier rubyid_to_s'>to_s</span>
827
915
  <span class='id identifier rubyid_inspect'>inspect</span>
@@ -836,9 +924,9 @@ Refer to the README for usage.</p>
836
924
  </div>
837
925
 
838
926
  <div id="footer">
839
- Generated on Sat Dec 17 12:56:00 2022 by
927
+ Generated on Wed Jan 18 11:24:43 2023 by
840
928
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
841
- 0.9.28 (ruby-3.0.4).
929
+ 0.9.28 (ruby-3.1.3).
842
930
  </div>
843
931
 
844
932
  </div>
data/doc/_index.html CHANGED
@@ -141,9 +141,9 @@
141
141
  </div>
142
142
 
143
143
  <div id="footer">
144
- Generated on Sat Dec 17 12:56:00 2022 by
144
+ Generated on Wed Jan 18 11:24:43 2023 by
145
145
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
146
- 0.9.28 (ruby-3.0.4).
146
+ 0.9.28 (ruby-3.1.3).
147
147
  </div>
148
148
 
149
149
  </div>
data/doc/file.README.html CHANGED
@@ -57,63 +57,52 @@
57
57
  <div class="clear"></div>
58
58
  </div>
59
59
 
60
- <div id="content"><div id='filecontents'><h1 id="introducing-anchormodel">Introducing Anchormodel</h1>
60
+ <div id="content"><div id='filecontents'>
61
+ <p>&lt;img src=“logo.svg” height=250 alt=“Anchormodel logo”/&gt;</p>
61
62
 
62
- <p>This gem provides a simple but powerful alternative to <a href="https://api.rubyonrails.org/v7.0/classes/ActiveRecord/Enum.html">Rails
63
- Enums</a>. In
64
- contrast to regular Enums, Anchormodels can hold application logic, making them
65
- ideal for tying code to database objects.</p>
63
+ <h1 id="label-Introducing+Anchormodel">Introducing Anchormodel</h1>
66
64
 
67
- <h1 id="use-case">Use case</h1>
65
+ <p>This gem provides a simple but powerful alternative to <a href="https://api.rubyonrails.org/v7.0/classes/ActiveRecord/Enum.html">Rails Enums</a>. In contrast to regular Enums, Anchormodels can hold application logic, making them ideal for tying code to database objects.</p>
68
66
 
69
- <p>Typically, a Rails application consists of three kinds of state:</p>
67
+ <h1 id="label-Use+case">Use case</h1>
70
68
 
71
- <ul>
72
- <li>The code, which we can consider static within a given version. Code can
73
- reference to other code, e.g. <code>node.parent = other_node</code>.</li>
74
- <li>The database contents, which can fluctuate within the bounds of the DB schema.
75
- Data can reference to other data, ideally via foreign keys.</li>
76
- <li>A mix of the two, where code needs to be specifically tailored for some kind
77
- of data. A prominent example of such a mix would for instance be user roles:
78
- roles must be hardcoded in the code because security logic is tied to them.
79
- However, as users are assigned to roles in the database, roles also need to be
80
- persisted in the database. This is where Anchormodel comes into play.</li>
81
- </ul>
69
+ <p>Typically, a Rails application consists of three kinds of state:</p>
70
+ <ul><li>
71
+ <p>The code, which we can consider static within a given version. Code can reference to other code, e.g. <code>node.parent = other_node</code>.</p>
72
+ </li><li>
73
+ <p>The database contents, which can fluctuate within the bounds of the DB schema. Data can reference to other data, ideally via foreign keys.</p>
74
+ </li><li>
75
+ <p>A mix of the two, where code needs to be specifically tailored for some kind of data. A prominent example of such a mix would for instance be user roles: roles must be hardcoded in the code because security logic is tied to them. However, as users are assigned to roles in the database, roles also need to be persisted in the database. This is where Anchormodel comes into play.</p>
76
+ </li></ul>
82
77
 
83
- <h2 id="alternatives-coviering-the-same-use-case">Alternatives coviering the same use case</h2>
78
+ <h2 id="label-Alternatives+coviering+the+same+use+case">Alternatives coviering the same use case</h2>
84
79
 
85
- <h3 id="enum">Enum</h3>
80
+ <h3 id="label-Enum">Enum</h3>
86
81
 
87
- <p>In Rails and other frameworks, the third point in the listing above is typically
88
- achieved via Enums. Enums map either Integers or Strings to constants and
89
- therefore provide a link between the DB and the application code.</p>
82
+ <p>In Rails and other frameworks, the third point in the listing above is typically achieved via Enums. Enums map either Integers or Strings to constants and therefore provide a link between the DB and the application code.</p>
90
83
 
91
- <p>However, at least in Rails, Enums provide very limited customization options.
92
- They are basic values that can be used in if-statements. Anchormodels however
93
- are regular classes and can easily be extended.</p>
84
+ <p>However, at least in Rails, Enums provide very limited customization options. They are basic values that can be used in if-statements. Anchormodels however are regular classes and can easily be extended.</p>
94
85
 
95
- <h3 id="activeenum">ActiveEnum</h3>
86
+ <h3 id="label-ActiveEnum">ActiveEnum</h3>
96
87
 
97
- <p>The gem (ActiveEnum)[<a href="https://github.com/adzap/active_enum">https://github.com/adzap/active_enum</a>] allows to create
98
- Enum-like classes that can be extended. However it only supports Integer keys. I
99
- find this unsatisfactory, as debugging with tools like <code>psql</code> or <code>mysql</code> is made
100
- unnecessarily hard when you only see numbers. Keys for enums should be
101
- meaningful, making you immediately understand what they stand for.</p>
88
+ <p>The gem (<a href="https://github.com/adzap/active_enum">ActiveEnum)</a> allows to create Enum-like classes that can be extended. However it only supports Integer keys. I find this unsatisfactory, as debugging with tools like <code>psql</code> or <code>mysql</code> is made unnecessarily hard when you only see numbers. Keys for enums should be meaningful, making you immediately understand what they stand for.</p>
102
89
 
103
- <p>This is why Anchormodel is strictly relying on String keys corresponding to the
104
- entries of an Anchormodel.</p>
90
+ <p>This is why Anchormodel is strictly relying on String keys corresponding to the entries of an Anchormodel.</p>
105
91
 
106
- <h1 id="example">Example</h1>
92
+ <h1 id="label-Example">Example</h1>
107
93
 
108
94
  <p><code>app/anchormodels/role.rb</code>:</p>
109
95
 
110
- <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Anchormodels</span><span class='op'>::</span><span class='const'>Role</span> <span class='op'>&lt;</span> <span class='const'><span class='object_link'><a href="Anchormodel.html" title="Anchormodel (class)">Anchormodel</a></span></span>
96
+ <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>Role</span> <span class='op'>&lt;</span> <span class='const'><span class='object_link'><a href="Anchormodel.html" title="Anchormodel (class)">Anchormodel</a></span></span>
97
+ <span class='comment'># Make &lt;, &gt; etc. based on &lt;=&gt; operator whic hwe will define below
98
+ </span> <span class='id identifier rubyid_include'>include</span> <span class='const'>Comparable</span>
99
+
111
100
  <span class='comment'># Expose the attribute privilege_level
112
101
  </span> <span class='id identifier rubyid_attr_reader'>attr_reader</span> <span class='symbol'>:privilege_level</span>
113
102
 
114
- <span class='comment'># Overload &lt;=&gt; to make user roles comparable based on the privilege level
103
+ <span class='comment'># Define &lt;=&gt; to make user roles comparable based on the privilege level
115
104
  </span> <span class='kw'>def</span> <span class='op'>&lt;=&gt;</span><span class='lparen'>(</span><span class='id identifier rubyid_other'>other</span><span class='rparen'>)</span>
116
- <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_privilege_level'>privilege_level</span> <span class='op'>&lt;=&gt;</span> <span class='ivar'>@privilege_level</span>
105
+ <span class='ivar'>@privilege_level</span> <span class='op'>&lt;=&gt;</span> <span class='id identifier rubyid_other'>other</span><span class='period'>.</span><span class='id identifier rubyid_privilege_level'>privilege_level</span>
117
106
  <span class='kw'>end</span>
118
107
 
119
108
  <span class='comment'># Declare all available roles
@@ -134,10 +123,10 @@ entries of an Anchormodel.</p>
134
123
  <p>You may now use the following methods:</p>
135
124
 
136
125
  <pre class="code ruby"><code class="ruby"><span class='comment'># Retrieve all user roles:
137
- </span><span class='const'>Anchormodels</span><span class='op'>::</span><span class='const'>Role</span><span class='period'>.</span><span class='id identifier rubyid_all'>all</span>
126
+ </span><span class='const'>Role</span><span class='period'>.</span><span class='id identifier rubyid_all'>all</span>
138
127
 
139
128
  <span class='comment'># Retrieve a specific role from the String and find its privilege level
140
- </span><span class='const'>Anchormodels</span><span class='op'>::</span><span class='const'>Role</span><span class='period'>.</span><span class='id identifier rubyid_find'>find</span><span class='lparen'>(</span><span class='symbol'>:guest</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_privilege_level'>privilege_level</span>
129
+ </span><span class='const'>Role</span><span class='period'>.</span><span class='id identifier rubyid_find'>find</span><span class='lparen'>(</span><span class='symbol'>:guest</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_privilege_level'>privilege_level</span>
141
130
 
142
131
  <span class='comment'># Implement a Rails helper that makes sure users can only edit other users that have a lower privilege level than themselves
143
132
  </span><span class='kw'>def</span> <span class='id identifier rubyid_user_can_edit?'>user_can_edit?</span><span class='lparen'>(</span><span class='id identifier rubyid_this_user'>this_user</span><span class='comma'>,</span> <span class='id identifier rubyid_other_user'>other_user</span><span class='rparen'>)</span>
@@ -148,18 +137,18 @@ entries of an Anchormodel.</p>
148
137
  </span><span class='id identifier rubyid_puts'>puts</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>User </span><span class='embexpr_beg'>#{</span><span class='ivar'>@user</span><span class='period'>.</span><span class='id identifier rubyid_name'>name</span><span class='embexpr_end'>}</span><span class='tstring_content'> has role </span><span class='embexpr_beg'>#{</span><span class='ivar'>@user</span><span class='period'>.</span><span class='id identifier rubyid_role'>role</span><span class='period'>.</span><span class='id identifier rubyid_label'>label</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span>
149
138
  </code></pre>
150
139
 
151
- <h1 id="installation">Installation</h1>
152
-
153
- <ol>
154
- <li>Add gem to Gemfile: <code>gem &#39;anchormodel&#39;</code></li>
155
- <li>In <code>application_record.rb</code>, add in the class body: <code>include Anchormodel::ModelMixin</code></li>
156
- </ol>
140
+ <h1 id="label-Installation">Installation</h1>
141
+ <ol><li>
142
+ <p>Add gem to Gemfile: <code>gem &#39;anchormodel&#39;</code></p>
143
+ </li><li>
144
+ <p>In <code>application_record.rb</code>, add in the class body: <code>include Anchormodel::ModelMixin</code></p>
145
+ </li></ol>
157
146
  </div></div>
158
147
 
159
148
  <div id="footer">
160
- Generated on Sat Dec 17 12:56:00 2022 by
149
+ Generated on Wed Jan 18 11:24:43 2023 by
161
150
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
162
- 0.9.28 (ruby-3.0.4).
151
+ 0.9.28 (ruby-3.1.3).
163
152
  </div>
164
153
 
165
154
  </div>