anchormodel 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -117,7 +117,7 @@
117
117
  <li class="public ">
118
118
  <span class="summary_signature">
119
119
 
120
- <a href="#belongs_to_anchormodel-class_method" title="belongs_to_anchormodel (class method)">.<strong>belongs_to_anchormodel</strong>(attribute_name, anchor_class_name = nil, optional: false) &#x21d2; Object </a>
120
+ <a href="#belongs_to_anchormodel-class_method" title="belongs_to_anchormodel (class method)">.<strong>belongs_to_anchormodel</strong>(attribute_name, anchormodel_class = nil, optional: false, model_readers: true, model_writers: true, model_scopes: true, model_methods: nil) &#x21d2; Object </a>
121
121
 
122
122
 
123
123
 
@@ -151,7 +151,7 @@
151
151
  <div class="method_details first">
152
152
  <h3 class="signature first" id="belongs_to_anchormodel-class_method">
153
153
 
154
- .<strong>belongs_to_anchormodel</strong>(attribute_name, anchor_class_name = nil, optional: false) &#x21d2; <tt>Object</tt>
154
+ .<strong>belongs_to_anchormodel</strong>(attribute_name, anchormodel_class = nil, optional: false, model_readers: true, model_writers: true, model_scopes: true, model_methods: nil) &#x21d2; <tt>Object</tt>
155
155
 
156
156
 
157
157
 
@@ -187,10 +187,10 @@
187
187
 
188
188
  <li>
189
189
 
190
- <span class='name'>anchor_class_name</span>
190
+ <span class='name'>anchormodel_class</span>
191
191
 
192
192
 
193
- <span class='type'>(<tt>String</tt>)</span>
193
+ <span class='type'>(<tt>Class</tt>)</span>
194
194
 
195
195
 
196
196
  <em class="default">(defaults to: <tt>nil</tt>)</em>
@@ -198,7 +198,7 @@
198
198
 
199
199
  &mdash;
200
200
  <div class='inline'>
201
- <p>Name of the Anchormodel class (omit if attribute <code>:foo_bar</code> holds a <code>FooBar</code>)</p>
201
+ <p>Class of the Anchormodel (omit if attribute <code>:foo_bar</code> holds a <code>FooBar</code>)</p>
202
202
  </div>
203
203
 
204
204
  </li>
@@ -217,6 +217,78 @@
217
217
  &mdash;
218
218
  <div class='inline'>
219
219
  <p>If true, a presence validation is added to the model.</p>
220
+ </div>
221
+
222
+ </li>
223
+
224
+ <li>
225
+
226
+ <span class='name'>model_readers</span>
227
+
228
+
229
+ <span class='type'>(<tt>Boolean</tt>)</span>
230
+
231
+
232
+ <em class="default">(defaults to: <tt>true</tt>)</em>
233
+
234
+
235
+ &mdash;
236
+ <div class='inline'>
237
+ <p>If true, the model is given an ActiveRecord::Enum style method <code>my_model.my_key?</code> reader for each key in the anchormodel</p>
238
+ </div>
239
+
240
+ </li>
241
+
242
+ <li>
243
+
244
+ <span class='name'>model_writers</span>
245
+
246
+
247
+ <span class='type'>(<tt>Boolean</tt>)</span>
248
+
249
+
250
+ <em class="default">(defaults to: <tt>true</tt>)</em>
251
+
252
+
253
+ &mdash;
254
+ <div class='inline'>
255
+ <p>If true, the model is given an ActiveRecord::Enum style method <code>my_model.my_key!</code> writer for each key in the anchormodel</p>
256
+ </div>
257
+
258
+ </li>
259
+
260
+ <li>
261
+
262
+ <span class='name'>model_scopes</span>
263
+
264
+
265
+ <span class='type'>(<tt>Boolean</tt>)</span>
266
+
267
+
268
+ <em class="default">(defaults to: <tt>true</tt>)</em>
269
+
270
+
271
+ &mdash;
272
+ <div class='inline'>
273
+ <p>If true, the model is given an ActiveRecord::Enum style scope <code>MyModel.mykey</code> for each key in the anchormodel</p>
274
+ </div>
275
+
276
+ </li>
277
+
278
+ <li>
279
+
280
+ <span class='name'>model_methods</span>
281
+
282
+
283
+ <span class='type'>(<tt>Boolean</tt>, <tt>NilClass</tt>)</span>
284
+
285
+
286
+ <em class="default">(defaults to: <tt>nil</tt>)</em>
287
+
288
+
289
+ &mdash;
290
+ <div class='inline'>
291
+ <p>If non-nil, this mass-assigns and overrides <code>model_readers</code>, <code>model_writers</code> and <code>model_scopes</code></p>
220
292
  </div>
221
293
 
222
294
  </li>
@@ -230,10 +302,6 @@
230
302
  <pre class="lines">
231
303
 
232
304
 
233
- 16
234
- 17
235
- 18
236
- 19
237
305
  20
238
306
  21
239
307
  22
@@ -258,14 +326,73 @@
258
326
  41
259
327
  42
260
328
  43
261
- 44</pre>
329
+ 44
330
+ 45
331
+ 46
332
+ 47
333
+ 48
334
+ 49
335
+ 50
336
+ 51
337
+ 52
338
+ 53
339
+ 54
340
+ 55
341
+ 56
342
+ 57
343
+ 58
344
+ 59
345
+ 60
346
+ 61
347
+ 62
348
+ 63
349
+ 64
350
+ 65
351
+ 66
352
+ 67
353
+ 68
354
+ 69
355
+ 70
356
+ 71
357
+ 72
358
+ 73
359
+ 74
360
+ 75
361
+ 76
362
+ 77
363
+ 78
364
+ 79
365
+ 80
366
+ 81
367
+ 82
368
+ 83
369
+ 84
370
+ 85
371
+ 86
372
+ 87
373
+ 88
374
+ 89
375
+ 90
376
+ 91
377
+ 92
378
+ 93
379
+ 94</pre>
262
380
  </td>
263
381
  <td>
264
- <pre class="code"><span class="info file"># File 'lib/anchormodel/model_mixin.rb', line 16</span>
382
+ <pre class="code"><span class="info file"># File 'lib/anchormodel/model_mixin.rb', line 20</span>
265
383
 
266
- <span class='kw'>def</span> <span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span><span class='lparen'>(</span><span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='comma'>,</span> <span class='id identifier rubyid_anchor_class_name'>anchor_class_name</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='comma'>,</span> <span class='label'>optional:</span> <span class='kw'>false</span><span class='rparen'>)</span>
384
+ <span class='kw'>def</span> <span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span><span class='lparen'>(</span><span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='comma'>,</span> <span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='comma'>,</span> <span class='label'>optional:</span> <span class='kw'>false</span><span class='comma'>,</span> <span class='label'>model_readers:</span> <span class='kw'>true</span><span class='comma'>,</span>
385
+ <span class='label'>model_writers:</span> <span class='kw'>true</span><span class='comma'>,</span> <span class='label'>model_scopes:</span> <span class='kw'>true</span><span class='comma'>,</span> <span class='label'>model_methods:</span> <span class='kw'>nil</span><span class='rparen'>)</span>
386
+ <span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span> <span class='op'>||=</span> <span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span><span class='period'>.</span><span class='id identifier rubyid_classify'>classify</span><span class='period'>.</span><span class='id identifier rubyid_constantize'>constantize</span>
267
387
  <span class='id identifier rubyid_attribute_name'>attribute_name</span> <span class='op'>=</span> <span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span>
268
- <span class='id identifier rubyid_attribute'>attribute</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="../Anchormodel.html" title="Anchormodel (class)">Anchormodel</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Attribute.html" title="Anchormodel::Attribute (class)">Attribute</a></span></span><span class='period'>.</span><span class='id identifier rubyid_new'><span class='object_link'><a href="Attribute.html#initialize-instance_method" title="Anchormodel::Attribute#initialize (method)">new</a></span></span><span class='lparen'>(</span><span class='kw'>self</span><span class='comma'>,</span> <span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='comma'>,</span> <span class='id identifier rubyid_anchor_class_name'>anchor_class_name</span><span class='comma'>,</span> <span class='id identifier rubyid_optional'>optional</span><span class='rparen'>)</span>
388
+ <span class='id identifier rubyid_attribute'>attribute</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="../Anchormodel.html" title="Anchormodel (class)">Anchormodel</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="Attribute.html" title="Anchormodel::Attribute (class)">Attribute</a></span></span><span class='period'>.</span><span class='id identifier rubyid_new'><span class='object_link'><a href="Attribute.html#initialize-instance_method" title="Anchormodel::Attribute#initialize (method)">new</a></span></span><span class='lparen'>(</span><span class='kw'>self</span><span class='comma'>,</span> <span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='comma'>,</span> <span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span><span class='comma'>,</span> <span class='id identifier rubyid_optional'>optional</span><span class='rparen'>)</span>
389
+
390
+ <span class='comment'># Mass configurations if model_methods was specfied
391
+ </span> <span class='kw'>unless</span> <span class='id identifier rubyid_model_methods'>model_methods</span><span class='period'>.</span><span class='id identifier rubyid_nil?'>nil?</span>
392
+ <span class='id identifier rubyid_model_readers'>model_readers</span> <span class='op'>=</span> <span class='id identifier rubyid_model_methods'>model_methods</span>
393
+ <span class='id identifier rubyid_model_writers'>model_writers</span> <span class='op'>=</span> <span class='id identifier rubyid_model_methods'>model_methods</span>
394
+ <span class='id identifier rubyid_model_scopes'>model_scopes</span> <span class='op'>=</span> <span class='id identifier rubyid_model_methods'>model_methods</span>
395
+ <span class='kw'>end</span>
269
396
 
270
397
  <span class='comment'># Register attribute
271
398
  </span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_anchormodel_attributes'>anchormodel_attributes</span> <span class='op'>=</span> <span class='id identifier rubyid_anchormodel_attributes'>anchormodel_attributes</span><span class='period'>.</span><span class='id identifier rubyid_merge'>merge</span><span class='lparen'>(</span><span class='lbrace'>{</span> <span class='id identifier rubyid_attribute_name'>attribute_name</span> <span class='op'>=&gt;</span> <span class='id identifier rubyid_attribute'>attribute</span> <span class='rbrace'>}</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_freeze'>freeze</span>
@@ -291,6 +418,43 @@
291
418
 
292
419
  <span class='comment'># Supply serializer and deserializer
293
420
  </span> <span class='id identifier rubyid_attribute'>attribute</span> <span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='comma'>,</span> <span class='id identifier rubyid_active_model_type_value'>active_model_type_value</span>
421
+
422
+ <span class='comment'># Create ActiveRecord::Enum style reader directly in the model if asked to do so
423
+ </span> <span class='comment'># For a model User with anchormodel Role with keys :admin and :guest, this creates user.admin? and user.guest? (returning true iff role is admin/guest)
424
+ </span> <span class='kw'>if</span> <span class='id identifier rubyid_model_readers'>model_readers</span>
425
+ <span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span><span class='period'>.</span><span class='id identifier rubyid_all'>all</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_entry'>entry</span><span class='op'>|</span>
426
+ <span class='kw'>if</span> <span class='id identifier rubyid_respond_to?'>respond_to?</span><span class='lparen'>(</span><span class='symbol'>:&quot;#{</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='embexpr_end'>}</span><span class='tstring_content'>?</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span>
427
+ <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'>Anchormodel reader </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='embexpr_end'>}</span><span class='tstring_content'>? already defined for </span><span class='embexpr_beg'>#{</span><span class='kw'>self</span><span class='embexpr_end'>}</span><span class='tstring_content'>, add `model_readers: false` to `belongs_to_anchormodel :</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='embexpr_end'>}</span><span class='tstring_content'>`.</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span>
428
+ <span class='kw'>end</span>
429
+ <span class='id identifier rubyid_define_method'>define_method</span><span class='lparen'>(</span><span class='symbol'>:&quot;#{</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</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'>do</span>
430
+ <span class='id identifier rubyid_public_send'>public_send</span><span class='lparen'>(</span><span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span><span class='rparen'>)</span> <span class='op'>==</span> <span class='id identifier rubyid_entry'>entry</span>
431
+ <span class='kw'>end</span>
432
+ <span class='kw'>end</span>
433
+ <span class='kw'>end</span>
434
+
435
+ <span class='comment'># Create ActiveRecord::Enum style writer directly in the model if asked to do so
436
+ </span> <span class='comment'># For a model User with anchormodel Role with keys :admin and :guest, this creates user.admin! and user.guest! (setting the role to admin/guest)
437
+ </span> <span class='kw'>if</span> <span class='id identifier rubyid_model_writers'>model_writers</span>
438
+ <span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span><span class='period'>.</span><span class='id identifier rubyid_all'>all</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_entry'>entry</span><span class='op'>|</span>
439
+ <span class='kw'>if</span> <span class='id identifier rubyid_respond_to?'>respond_to?</span><span class='lparen'>(</span><span class='symbol'>:&quot;#{</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='embexpr_end'>}</span><span class='tstring_content'>!</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span>
440
+ <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'>Anchormodel writer </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='embexpr_end'>}</span><span class='tstring_content'>! already defined for </span><span class='embexpr_beg'>#{</span><span class='kw'>self</span><span class='embexpr_end'>}</span><span class='tstring_content'>, add `model_writers: false` to `belongs_to_anchormodel :</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='embexpr_end'>}</span><span class='tstring_content'>`.</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span>
441
+ <span class='kw'>end</span>
442
+ <span class='id identifier rubyid_define_method'>define_method</span><span class='lparen'>(</span><span class='symbol'>:&quot;#{</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</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'>do</span>
443
+ <span class='id identifier rubyid_public_send'>public_send</span><span class='lparen'>(</span><span class='symbol'>:&quot;#{</span><span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='embexpr_end'>}</span><span class='tstring_content'>=</span><span class='tstring_end'>&quot;</span></span><span class='comma'>,</span> <span class='id identifier rubyid_entry'>entry</span><span class='rparen'>)</span>
444
+ <span class='kw'>end</span>
445
+ <span class='kw'>end</span>
446
+ <span class='kw'>end</span>
447
+
448
+ <span class='comment'># Create ActiveRecord::Enum style scope directly in the model class if asked to do so
449
+ </span> <span class='comment'># For a model User with anchormodel Role with keys :admin and :guest, this creates user.admin! and user.guest! (setting the role to admin/guest)
450
+ </span> <span class='kw'>if</span> <span class='id identifier rubyid_model_scopes'>model_scopes</span>
451
+ <span class='id identifier rubyid_anchormodel_class'>anchormodel_class</span><span class='period'>.</span><span class='id identifier rubyid_all'>all</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_entry'>entry</span><span class='op'>|</span>
452
+ <span class='kw'>if</span> <span class='id identifier rubyid_respond_to?'>respond_to?</span><span class='lparen'>(</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span>
453
+ <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'>Anchormodel scope </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='embexpr_end'>}</span><span class='tstring_content'> already defined for </span><span class='embexpr_beg'>#{</span><span class='kw'>self</span><span class='embexpr_end'>}</span><span class='tstring_content'>, add `model_scopes: false` to `belongs_to_anchormodel :</span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_attribute_name'>attribute_name</span><span class='embexpr_end'>}</span><span class='tstring_content'>`.</span><span class='tstring_end'>&quot;</span></span><span class='rparen'>)</span>
454
+ <span class='kw'>end</span>
455
+ <span class='id identifier rubyid_scope'>scope</span><span class='lparen'>(</span><span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='tlambda'>-&gt;</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_where'>where</span><span class='lparen'>(</span><span class='id identifier rubyid_attribute_name'>attribute_name</span> <span class='op'>=&gt;</span> <span class='id identifier rubyid_entry'>entry</span><span class='period'>.</span><span class='id identifier rubyid_key'>key</span><span class='rparen'>)</span> <span class='rbrace'>}</span><span class='rparen'>)</span>
456
+ <span class='kw'>end</span>
457
+ <span class='kw'>end</span>
294
458
  <span class='kw'>end</span></pre>
295
459
  </td>
296
460
  </tr>
@@ -302,7 +466,7 @@
302
466
  </div>
303
467
 
304
468
  <div id="footer">
305
- Generated on Wed Jan 18 11:24:43 2023 by
469
+ Generated on Wed Jan 25 12:36:40 2023 by
306
470
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
307
471
  0.9.28 (ruby-3.1.3).
308
472
  </div>
@@ -127,7 +127,7 @@
127
127
 
128
128
  </div>
129
129
  </dt>
130
- <dd><pre class="code"><span class='int'>0</span></pre></dd>
130
+ <dd><pre class="code"><span class='int'>1</span></pre></dd>
131
131
 
132
132
  <dt id="PATCH-constant" class="">PATCH =
133
133
  <div class="docstring">
@@ -141,7 +141,7 @@
141
141
 
142
142
  </div>
143
143
  </dt>
144
- <dd><pre class="code"><span class='int'>3</span></pre></dd>
144
+ <dd><pre class="code"><span class='int'>2</span></pre></dd>
145
145
 
146
146
  <dt id="EDGE-constant" class="">EDGE =
147
147
  <div class="docstring">
@@ -155,7 +155,7 @@
155
155
 
156
156
  </div>
157
157
  </dt>
158
- <dd><pre class="code"><span class='kw'>true</span></pre></dd>
158
+ <dd><pre class="code"><span class='kw'>false</span></pre></dd>
159
159
 
160
160
  <dt id="LABEL-constant" class="">LABEL =
161
161
  <div class="docstring">
@@ -185,7 +185,7 @@
185
185
  </div>
186
186
 
187
187
  <div id="footer">
188
- Generated on Wed Jan 18 11:24:43 2023 by
188
+ Generated on Wed Jan 25 12:36:39 2023 by
189
189
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
190
190
  0.9.28 (ruby-3.1.3).
191
191
  </div>
data/doc/Anchormodel.html CHANGED
@@ -480,7 +480,12 @@
480
480
  52
481
481
  53
482
482
  54
483
- 55</pre>
483
+ 55
484
+ 56
485
+ 57
486
+ 58
487
+ 59
488
+ 60</pre>
484
489
  </td>
485
490
  <td>
486
491
  <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 38</span>
@@ -502,6 +507,11 @@
502
507
 
503
508
  <span class='comment'># Register valid keys
504
509
  </span> <span class='id identifier rubyid_valid_keys'>valid_keys</span> <span class='op'>&lt;&lt;</span> <span class='id identifier rubyid_key'>key</span>
510
+
511
+ <span class='comment'># Define boolean reader
512
+ </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_define_method'>define_method</span><span class='lparen'>(</span><span class='symbol'>:&quot;#{</span><span class='id identifier rubyid_key'>key</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'>do</span>
513
+ <span class='ivar'>@key</span> <span class='op'>==</span> <span class='id identifier rubyid_key'>key</span>
514
+ <span class='kw'>end</span>
505
515
  <span class='kw'>end</span></pre>
506
516
  </td>
507
517
  </tr>
@@ -785,12 +795,12 @@
785
795
  <pre class="lines">
786
796
 
787
797
 
788
- 57
789
- 58
790
- 59</pre>
798
+ 62
799
+ 63
800
+ 64</pre>
791
801
  </td>
792
802
  <td>
793
- <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 57</span>
803
+ <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 62</span>
794
804
 
795
805
  <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>
796
806
  <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>
@@ -824,12 +834,12 @@
824
834
  <pre class="lines">
825
835
 
826
836
 
827
- 66
828
- 67
829
- 68</pre>
837
+ 71
838
+ 72
839
+ 73</pre>
830
840
  </td>
831
841
  <td>
832
- <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 66</span>
842
+ <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 71</span>
833
843
 
834
844
  <span class='kw'>def</span> <span class='id identifier rubyid_inspect'>inspect</span>
835
845
  <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>
@@ -865,12 +875,12 @@
865
875
  <pre class="lines">
866
876
 
867
877
 
868
- 62
869
- 63
870
- 64</pre>
878
+ 67
879
+ 68
880
+ 69</pre>
871
881
  </td>
872
882
  <td>
873
- <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 62</span>
883
+ <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 67</span>
874
884
 
875
885
  <span class='kw'>def</span> <span class='id identifier rubyid_label'>label</span>
876
886
  <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>
@@ -904,12 +914,12 @@
904
914
  <pre class="lines">
905
915
 
906
916
 
907
- 70
908
- 71
909
- 72</pre>
917
+ 75
918
+ 76
919
+ 77</pre>
910
920
  </td>
911
921
  <td>
912
- <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 70</span>
922
+ <pre class="code"><span class="info file"># File 'lib/anchormodel.rb', line 75</span>
913
923
 
914
924
  <span class='kw'>def</span> <span class='id identifier rubyid_to_s'>to_s</span>
915
925
  <span class='id identifier rubyid_inspect'>inspect</span>
@@ -924,7 +934,7 @@
924
934
  </div>
925
935
 
926
936
  <div id="footer">
927
- Generated on Wed Jan 18 11:24:43 2023 by
937
+ Generated on Wed Jan 25 12:36:40 2023 by
928
938
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
929
939
  0.9.28 (ruby-3.1.3).
930
940
  </div>
data/doc/_index.html CHANGED
@@ -141,7 +141,7 @@
141
141
  </div>
142
142
 
143
143
  <div id="footer">
144
- Generated on Wed Jan 18 11:24:43 2023 by
144
+ Generated on Wed Jan 25 12:36:39 2023 by
145
145
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
146
146
  0.9.28 (ruby-3.1.3).
147
147
  </div>
data/doc/file.README.html CHANGED
@@ -89,7 +89,14 @@
89
89
 
90
90
  <p>This is why Anchormodel is strictly relying on String keys corresponding to the entries of an Anchormodel.</p>
91
91
 
92
- <h1 id="label-Example">Example</h1>
92
+ <h1 id="label-Installation">Installation</h1>
93
+ <ol><li>
94
+ <p>Add gem to Gemfile: <code>gem &#39;anchormodel&#39;</code></p>
95
+ </li><li>
96
+ <p>In <code>application_record.rb</code>, add in the class body: <code>include Anchormodel::ModelMixin</code></p>
97
+ </li></ol>
98
+
99
+ <h1 id="label-Basic+example">Basic example</h1>
93
100
 
94
101
  <p><code>app/anchormodels/role.rb</code>:</p>
95
102
 
@@ -116,7 +123,11 @@
116
123
 
117
124
  <pre class="code ruby"><code class="ruby"><span class='comment'># The DB table `users` must have a String column `users.role`
118
125
  </span><span class='kw'>class</span> <span class='const'>User</span> <span class='op'>&lt;</span> <span class='const'>ApplicationRecord</span>
119
- <span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span> <span class='symbol'>:role</span>
126
+ <span class='comment'># If `users.role` has an `NOT NULL` constraint, use:
127
+ </span> <span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span> <span class='symbol'>:role</span>
128
+
129
+ <span class='comment'># If `users.role` can be `NULL`, use the following instead:
130
+ </span> <span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span> <span class='symbol'>:role</span><span class='comma'>,</span> <span class='label'>optional:</span> <span class='kw'>true</span>
120
131
  <span class='kw'>end</span>
121
132
  </code></pre>
122
133
 
@@ -135,18 +146,89 @@
135
146
 
136
147
  <span class='comment'># Pretty print a user&#39;s role, e.g. using the Rails FastGettext gem:
137
148
  </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>
138
- </code></pre>
139
149
 
140
- <h1 id="label-Installation">Installation</h1>
141
- <ol><li>
142
- <p>Add gem to Gemfile: <code>gem &#39;anchormodel&#39;</code></p>
150
+ <span class='comment'># Check whether @user has role admin
151
+ </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_admin?'>admin?</span> <span class='comment'># true if and only if the role is admin (false otherwise)
152
+ </span></code></pre>
153
+
154
+ <h1 id="label-Rails+Enum+style+model+methods">Rails Enum style model methods</h1>
155
+
156
+ <p>By default, Anchormodel adds three kinds of methods for each key to the model:</p>
157
+ <ul><li>
158
+ <p>a reader (getter)</p>
143
159
  </li><li>
144
- <p>In <code>application_record.rb</code>, add in the class body: <code>include Anchormodel::ModelMixin</code></p>
145
- </li></ol>
160
+ <p>a writer (setter)</p>
161
+ </li><li>
162
+ <p>a Rails scope</p>
163
+ </li></ul>
164
+
165
+ <p>For instance:</p>
166
+
167
+ <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>User</span> <span class='op'>&lt;</span> <span class='const'>ApplicationRecord</span>
168
+ <span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span> <span class='symbol'>:role</span> <span class='comment'># where Role has keys :guest, :manager and :admin
169
+ </span> <span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span> <span class='symbol'>:shape</span> <span class='comment'># where Shape has keys :circle and :rectangle
170
+ </span><span class='kw'>end</span>
171
+
172
+ <span class='comment'># User now implements the following methods, given that @user is retrieved as follows:
173
+ </span><span class='ivar'>@user</span> <span class='op'>=</span> <span class='const'>User</span><span class='period'>.</span><span class='id identifier rubyid_first'>first</span> <span class='comment'># for example
174
+ </span>
175
+ <span class='comment'># Readers
176
+ </span><span class='ivar'>@user</span><span class='period'>.</span><span class='id identifier rubyid_guest?'>guest?</span> <span class='comment'># same as @user.role.guest?
177
+ </span><span class='ivar'>@user</span><span class='period'>.</span><span class='id identifier rubyid_manager?'>manager?</span>
178
+ <span class='ivar'>@user</span><span class='period'>.</span><span class='id identifier rubyid_admin?'>admin?</span>
179
+ <span class='ivar'>@user</span><span class='period'>.</span><span class='id identifier rubyid_rectangle?'>rectangle?</span> <span class='comment'># same as @user.shape.rectangle?
180
+ </span><span class='ivar'>@user</span><span class='period'>.</span><span class='id identifier rubyid_circle?'>circle?</span>
181
+ <span class='comment'># Writers
182
+ </span><span class='ivar'>@user</span><span class='period'>.</span><span class='id identifier rubyid_guest!'>guest!</span> <span class='comment'># same as @user.role = Role.find(:guest)
183
+ </span><span class='ivar'>@user</span><span class='period'>.</span><span class='id identifier rubyid_manager!'>manager!</span>
184
+ <span class='ivar'>@user</span><span class='period'>.</span><span class='id identifier rubyid_admin!'>admin!</span>
185
+ <span class='ivar'>@user</span><span class='period'>.</span><span class='id identifier rubyid_rectangle!'>rectangle!</span> <span class='comment'># same as @user.shape = Shape.find(:rectangle)
186
+ </span><span class='ivar'>@user</span><span class='period'>.</span><span class='id identifier rubyid_circle!'>circle!</span>
187
+ <span class='comment'># Scopes
188
+ </span><span class='const'>User</span><span class='period'>.</span><span class='id identifier rubyid_guest'>guest</span> <span class='comment'># same as User.where(role: &#39;guest&#39;)
189
+ </span><span class='const'>User</span><span class='period'>.</span><span class='id identifier rubyid_manager'>manager</span>
190
+ <span class='const'>User</span><span class='period'>.</span><span class='id identifier rubyid_admin'>admin</span>
191
+ <span class='const'>User</span><span class='period'>.</span><span class='id identifier rubyid_rectangle'>rectangle</span> <span class='comment'># same as User.where(shape: &#39;rectangle&#39;)
192
+ </span><span class='const'>User</span><span class='period'>.</span><span class='id identifier rubyid_circle'>circle</span>
193
+ </code></pre>
194
+
195
+ <p>This behavior is similar as the one from Rails Enums. If you want to disable it, use:</p>
196
+
197
+ <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>User</span> <span class='op'>&lt;</span> <span class='const'>ApplicationRecord</span>
198
+ <span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span> <span class='symbol'>:role</span><span class='comma'>,</span> <span class='label'>model_readers:</span> <span class='kw'>false</span><span class='comma'>,</span> <span class='label'>model_writers:</span> <span class='kw'>false</span><span class='comma'>,</span> <span class='label'>model_scopes:</span> <span class='kw'>false</span>
199
+ <span class='comment'># or, equivalent, to disable all at once:
200
+ </span> <span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span> <span class='symbol'>:role</span><span class='comma'>,</span> <span class='label'>model_methods:</span> <span class='kw'>false</span>
201
+ <span class='kw'>end</span>
202
+ </code></pre>
203
+
204
+ <h1 id="label-Calling+a+column+differently+than+the+Anchormodel">Calling a column differently than the Anchormodel</h1>
205
+
206
+ <p>If your column name (and the model’s attribute) is called differently than the Anchormodel, you may give the Anchormodel’s class as the second argument. For example:</p>
207
+
208
+ <pre class="code ruby"><code class="ruby"><span class='comment'># app/anchormodels/color.rb
209
+ </span><span class='kw'>class</span> <span class='const'>Color</span> <span class='op'>&lt;</span> <span class='const'><span class='object_link'><a href="Anchormodel.html" title="Anchormodel (class)">Anchormodel</a></span></span>
210
+ <span class='id identifier rubyid_new'>new</span> <span class='symbol'>:green</span>
211
+ <span class='id identifier rubyid_new'>new</span> <span class='symbol'>:red</span>
212
+ <span class='kw'>end</span>
213
+
214
+ <span class='comment'># app/models/user.rb
215
+ </span><span class='kw'>class</span> <span class='const'>User</span> <span class='op'>&lt;</span> <span class='const'>ApplicationRecord</span>
216
+ <span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span> <span class='symbol'>:favorite_color</span><span class='comma'>,</span> <span class='const'>Color</span>
217
+ <span class='kw'>end</span>
218
+ </code></pre>
219
+
220
+ <h2 id="label-Having+multiple+attributes+to+the+same+Anchormodel">Having multiple attributes to the same Anchormodel</h2>
221
+
222
+ <p>If you want to have multiple attributes in the same model pointing to the same Anchormodel, you need to disable <code>model_methods</code> for at least one of them (otherwise the model methods will clash in your model class):</p>
223
+
224
+ <pre class="code ruby"><code class="ruby"><span class='comment'># app/models/user.rb
225
+ </span> <span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span> <span class='symbol'>:role</span>
226
+ <span class='id identifier rubyid_belongs_to_anchormodel'>belongs_to_anchormodel</span> <span class='symbol'>:secondary_role</span><span class='comma'>,</span> <span class='const'>Role</span><span class='comma'>,</span> <span class='label'>model_methods:</span> <span class='kw'>false</span>
227
+ </code></pre>
146
228
  </div></div>
147
229
 
148
230
  <div id="footer">
149
- Generated on Wed Jan 18 11:24:43 2023 by
231
+ Generated on Wed Jan 25 12:36:39 2023 by
150
232
  <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
151
233
  0.9.28 (ruby-3.1.3).
152
234
  </div>