inline_transforms 0.1.0 → 0.2.0

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: 22f30721666a47350279cb918ceadc3887f4fa5aaca5202901559ec690778fa6
4
- data.tar.gz: bc800383746ebb80919e00a9103fac32ad4cf8c8863a5207c2ffcd186de58ca4
3
+ metadata.gz: 69650094317116d52d29eaaebdc3237dcf2c014d6447b2ad52631a4eabb11837
4
+ data.tar.gz: 2729df1b7e99479a4101a9c53782b32a4d736e1756008f0da8fa976722799bd2
5
5
  SHA512:
6
- metadata.gz: 423e20167b171a3a929c9dcc063d9bcb7a10cc91a1f0b6cab20817df8ababf10471f96196c09994dad0e7b0999ac324d1466d19d99f680a7ebdb5b5a57e77ea8
7
- data.tar.gz: 65bb28caa0f2d10c3a45a5a45a05574f55f230c77f47783bd5ec819280d62809c1cd2808d314176f62588e02f107d42ccb17caac0121ed16f9d205d59bd9bae1
6
+ metadata.gz: e1432ee3d677412d5aa6bcd9b014f2b7e5c093b7e495b760335dac315d312825890e1e26759a977639a7c69fdce09f6e9309b8b57f3bb2bcb4d06291ac1ee62c
7
+ data.tar.gz: 462d39e0cf06278f6bf757ceec642496b22a7b2d06f9e8bf6d66c0fc0d4993e4c493eaea2cad25258e9c1f72549f7eb25e7925dd04b3af7a0d918a1f47d4c914
data/README.md CHANGED
@@ -8,6 +8,25 @@ Ruby is an incredibly expressive (and easy to read!) language, but lacks a way f
8
8
 
9
9
  In simplest terms:
10
10
  ```ruby
11
+ # "Only If" Logic:
12
+
13
+ final_value = original_value
14
+ final_value = different_value unless original_value == a_good_thing
15
+
16
+ # ... or ...
17
+
18
+ final_value = if original_value == a_good_thing
19
+ original_value
20
+ else
21
+ different_value
22
+ end
23
+
24
+ # ... becomes ...
25
+
26
+ final_value = original_value.only_if a_good_thing,
27
+ else: different_value
28
+ ```
29
+ ```ruby
11
30
  # "Unless" Logic:
12
31
 
13
32
  final_value = original_value
@@ -25,8 +44,8 @@ final_value = if original_value == a_bad_thing
25
44
 
26
45
  final_value = original_value.unless a_bad_thing,
27
46
  then: different_value
28
-
29
-
47
+ ```
48
+ ```ruby
30
49
  # "Transform" Logic:
31
50
 
32
51
  final_value = case original_value
@@ -40,7 +59,6 @@ final_value = case original_value
40
59
  final_value = original_value.transform true => 'success',
41
60
  false => 'failure',
42
61
  String => %(error: "#{original_value}")
43
-
44
62
  ```
45
63
 
46
64
  [Full documentation is available here](https://nestor-custodio.github.io/inline_transforms), but do read below for a crash course on availble featues!
@@ -68,6 +86,20 @@ final_value = original_value.transform true => 'success',
68
86
 
69
87
  ## Usage
70
88
 
89
+ ### Object#only_if
90
+
91
+ `only_if` lets you specify a "good" value and an `else` replacement.
92
+
93
+ - If your `else` replacement is a `Proc`, it is resolved (via `call`) before being returned.
94
+ - This method uses **case comparison** (`===`), so you can check for range inclusion or class.
95
+
96
+ ```ruby
97
+ final = value.only_if good_value, else: fallback_value
98
+ # ... or ...
99
+ final = value.only_if good_value, else: -> { some_method_call with_params }
100
+ ```
101
+
102
+
71
103
  ### Object#unless
72
104
 
73
105
  `unless` lets you specify a "bad" value and a `then` replacement.
@@ -99,6 +131,8 @@ final = value.transform key_1 => value_1,
99
131
 
100
132
  ## Potential Gotchas
101
133
 
134
+ - Because `else:` has special meaning, `transform` cannot check for the original value being the literal `Symbol` `:else`.
135
+
102
136
  - `Proc` instances are only resolved if they _need to be returned and are not the original value_:
103
137
  ```ruby
104
138
  value = -> { 'value proc' }
@@ -0,0 +1,531 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>
7
+ Module: InlineTransforms
8
+
9
+ &mdash; Documentation by YARD 0.9.45
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css">
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css">
16
+
17
+ <script type="text/javascript">
18
+ pathId = "InlineTransforms";
19
+ relpath = '';
20
+ </script>
21
+
22
+
23
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
24
+
25
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
26
+
27
+
28
+ </head>
29
+ <body>
30
+ <div id="main_progress" aria-hidden="true"></div>
31
+
32
+ <div class="nav_wrap">
33
+ <iframe id="nav" src="class_list.html?1"></iframe>
34
+ <div id="resizer"></div>
35
+ </div>
36
+
37
+ <div id="main" tabindex="-1">
38
+ <div id="header">
39
+ <div id="menu">
40
+
41
+ <a href="_index.html">Index (I)</a> &raquo;
42
+
43
+
44
+ <span class="title">InlineTransforms</span>
45
+
46
+ </div>
47
+
48
+ <div id="search">
49
+
50
+ <a class="full_list_link" id="class_list_link"
51
+ href="class_list.html">
52
+
53
+ <svg width="24" height="24">
54
+ <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
55
+ <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
56
+ <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
57
+ </svg>
58
+ </a>
59
+
60
+ </div>
61
+ <div class="clear"></div>
62
+ </div>
63
+
64
+ <div id="content"><h1>Module: InlineTransforms
65
+
66
+
67
+
68
+ </h1>
69
+ <div class="box_info">
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+ <dl>
80
+ <dt>Included in:</dt>
81
+ <dd><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></dd>
82
+ </dl>
83
+
84
+
85
+
86
+ <dl>
87
+ <dt>Defined in:</dt>
88
+ <dd>lib/inline_transforms.rb<span class="defines">,<br>
89
+ lib/inline_transforms/version.rb</span>
90
+
91
+ </dd>
92
+ </dl>
93
+
94
+ </div>
95
+
96
+ <h2>Overview</h2><div class="docstring">
97
+ <div class="discussion">
98
+ <p>Defines <code>only_if</code>, <code>unless</code>, and <code>transform</code>, which we'll then want to make available in <code>Object</code> instances.</p>
99
+
100
+ </div>
101
+ </div>
102
+ <div class="tags">
103
+
104
+
105
+ </div>
106
+
107
+ <h2>
108
+ Constant Summary
109
+ <small><a href="#" class="constants_summary_toggle">collapse</a></small>
110
+ </h2>
111
+
112
+ <dl class="constants">
113
+
114
+ <dt id="VERSION-constant" class="">VERSION =
115
+
116
+ </dt>
117
+ <dd><pre class="code"><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>0.2.0</span><span class='tstring_end'>&#39;</span></span><span class='period'>.</span><span class='id identifier rubyid_freeze'>freeze</span></pre></dd>
118
+
119
+ </dl>
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+ <h2>
130
+ Instance Method Summary
131
+ <small><a href="#" class="summary_toggle">collapse</a></small>
132
+ </h2>
133
+
134
+ <ul class="summary">
135
+
136
+ <li class="public ">
137
+ <span class="summary_signature">
138
+
139
+ <a href="#only_if-instance_method" title="#only_if (instance method)">#<strong>only_if</strong>(good_value, **options) &#x21d2; Object </a>
140
+
141
+
142
+
143
+ </span>
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+
152
+
153
+ <span class="summary_desc"><div class='inline'><p>Returns <code>self</code> <em>only if</em> it matches a &quot;good&quot; value; otherwise returns an alternate (<code>else:</code>) value.</p></div></span>
154
+
155
+ </li>
156
+
157
+
158
+ <li class="public ">
159
+ <span class="summary_signature">
160
+
161
+ <a href="#transform-instance_method" title="#transform (instance method)">#<strong>transform</strong>(**options) &#x21d2; Object </a>
162
+
163
+
164
+
165
+ </span>
166
+
167
+
168
+
169
+
170
+
171
+
172
+
173
+
174
+
175
+ <span class="summary_desc"><div class='inline'><p>Returns <code>self</code> ...</p></div></span>
176
+
177
+ </li>
178
+
179
+
180
+ <li class="public ">
181
+ <span class="summary_signature">
182
+
183
+ <a href="#unless-instance_method" title="#unless (instance method)">#<strong>unless</strong>(bad_value, **options) &#x21d2; Object </a>
184
+
185
+
186
+
187
+ </span>
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+ <span class="summary_desc"><div class='inline'><p>Returns <code>self</code> ...</p></div></span>
198
+
199
+ </li>
200
+
201
+
202
+ </ul>
203
+
204
+
205
+
206
+
207
+
208
+ <div id="instance_method_details" class="method_details_list">
209
+ <h2>Instance Method Details</h2>
210
+
211
+
212
+ <div class="method_details first">
213
+ <h3 class="signature first" id="only_if-instance_method">
214
+
215
+ #<strong>only_if</strong>(good_value, **options) &#x21d2; <tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>
216
+
217
+
218
+
219
+
220
+
221
+ </h3><div class="docstring">
222
+ <div class="discussion">
223
+ <p>Returns <code>self</code> <em>only if</em> it matches a &quot;good&quot; value; otherwise returns an alternate (<code>else:</code>) value.</p>
224
+ <p>(NOTE: this takes an <code>else: nil</code> named param, but is defined as taking <code>(**options)</code> because <code>else</code> is reserved.)</p>
225
+
226
+ </div>
227
+ </div>
228
+ <div class="tags">
229
+ <p class="tag_title">Parameters:</p>
230
+ <ul class="param">
231
+
232
+ <li>
233
+
234
+ <span class='name'>good_value</span>
235
+
236
+
237
+ <span class='type'>(<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>)</span>
238
+
239
+
240
+
241
+ &mdash;
242
+ <div class='inline'><p>The one value we consider &quot;good&quot;, and for which we'd like <code>self</code> returned.</p></div>
243
+
244
+ </li>
245
+
246
+ <li>
247
+
248
+ <span class='name'>else:</span>
249
+
250
+
251
+ <span class='type'>(<tt>Hash</tt>)</span>
252
+
253
+
254
+
255
+ &mdash;
256
+ <div class='inline'><p>a customizable set of options</p></div>
257
+
258
+ </li>
259
+
260
+ </ul>
261
+
262
+
263
+
264
+
265
+
266
+
267
+
268
+ <p class="tag_title">Returns:</p>
269
+ <ul class="return">
270
+
271
+ <li>
272
+
273
+
274
+ <span class='type'></span>
275
+
276
+
277
+
278
+
279
+ <div class='inline'><p>Returns either <code>self</code> or the value given as the <code>else:</code> option.</p></div>
280
+
281
+ </li>
282
+
283
+ </ul>
284
+
285
+ </div><table class="source_code">
286
+ <tr>
287
+ <td>
288
+ <pre class="lines">
289
+
290
+
291
+ 26
292
+ 27
293
+ 28
294
+ 29
295
+ 30
296
+ 31
297
+ 32
298
+ 33</pre>
299
+ </td>
300
+ <td>
301
+ <pre class="code"><span class="info file"># File 'lib/inline_transforms.rb', line 26</span>
302
+
303
+ <span class='kw'>def</span> <span class='id identifier rubyid_only_if'>only_if</span><span class='lparen'>(</span><span class='id identifier rubyid_good_value'>good_value</span><span class='comma'>,</span> <span class='op'>**</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
304
+ <span class='kw'>return</span> <span class='kw'>self</span> <span class='kw'>if</span> <span class='id identifier rubyid_inline_transforms_equality?'>inline_transforms_equality?</span> <span class='id identifier rubyid_good_value'>good_value</span>
305
+
306
+ <span class='id identifier rubyid_alt_value'>alt_value</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:else</span><span class='rbracket'>]</span>
307
+ <span class='kw'>return</span> <span class='id identifier rubyid_alt_value'>alt_value</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span> <span class='kw'>if</span> <span class='id identifier rubyid_alt_value'>alt_value</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span> <span class='const'>Proc</span>
308
+
309
+ <span class='id identifier rubyid_alt_value'>alt_value</span>
310
+ <span class='kw'>end</span></pre>
311
+ </td>
312
+ </tr>
313
+ </table>
314
+ </div>
315
+
316
+ <div class="method_details ">
317
+ <h3 class="signature " id="transform-instance_method">
318
+
319
+ #<strong>transform</strong>(**options) &#x21d2; <tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>
320
+
321
+
322
+
323
+
324
+
325
+ </h3><div class="docstring">
326
+ <div class="discussion">
327
+ <p>Returns <code>self</code> ... unless it matches one of the keys in the given &quot;transformation hash&quot;, in which case the
328
+ corresponding hash value is returned. If no matching hash key is found, this returns a fallback (<code>else:</code>) value.</p>
329
+ <p>(NOTE: this takes an <code>else: nil</code> named param, but is defined as taking <code>(**options)</code> because <code>else</code> is reserved.)</p>
330
+
331
+ </div>
332
+ </div>
333
+ <div class="tags">
334
+ <p class="tag_title">Parameters:</p>
335
+ <ul class="param">
336
+
337
+ <li>
338
+
339
+ <span class='name'>**</span>
340
+
341
+
342
+ <span class='type'>(<tt>Hash</tt>)</span>
343
+
344
+
345
+
346
+ &mdash;
347
+ <div class='inline'><p>a customizable set of options</p></div>
348
+
349
+ </li>
350
+
351
+ <li>
352
+
353
+ <span class='name'>else:</span>
354
+
355
+
356
+ <span class='type'>(<tt>Hash</tt>)</span>
357
+
358
+
359
+
360
+ &mdash;
361
+ <div class='inline'><p>a customizable set of options</p></div>
362
+
363
+ </li>
364
+
365
+ </ul>
366
+
367
+
368
+
369
+
370
+
371
+ <p class="tag_title">Returns:</p>
372
+ <ul class="return">
373
+
374
+ <li>
375
+
376
+
377
+ <span class='type'></span>
378
+
379
+
380
+
381
+
382
+ <div class='inline'><p>Returns either <code>self</code>, one of the transformation hash values, or the <code>else:</code> option.</p></div>
383
+
384
+ </li>
385
+
386
+ </ul>
387
+
388
+ </div><table class="source_code">
389
+ <tr>
390
+ <td>
391
+ <pre class="lines">
392
+
393
+
394
+ 74
395
+ 75
396
+ 76
397
+ 77
398
+ 78
399
+ 79</pre>
400
+ </td>
401
+ <td>
402
+ <pre class="code"><span class="info file"># File 'lib/inline_transforms.rb', line 74</span>
403
+
404
+ <span class='kw'>def</span> <span class='id identifier rubyid_transform'>transform</span><span class='lparen'>(</span><span class='op'>**</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
405
+ <span class='id identifier rubyid_default'>default</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_delete'>delete</span> <span class='symbol'>:else</span>
406
+
407
+ <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_key'>key</span><span class='comma'>,</span> <span class='id identifier rubyid_value'>value</span><span class='op'>|</span> <span class='kw'>return</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_unless'>unless</span> <span class='const'>Proc</span><span class='comma'>,</span> <span class='label'>then:</span> <span class='tlambda'>-&gt;</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span> <span class='rbrace'>}</span> <span class='kw'>if</span> <span class='id identifier rubyid_inline_transforms_equality?'>inline_transforms_equality?</span> <span class='id identifier rubyid_key'>key</span> <span class='rbrace'>}</span>
408
+ <span class='id identifier rubyid_default'>default</span>
409
+ <span class='kw'>end</span></pre>
410
+ </td>
411
+ </tr>
412
+ </table>
413
+ </div>
414
+
415
+ <div class="method_details ">
416
+ <h3 class="signature " id="unless-instance_method">
417
+
418
+ #<strong>unless</strong>(bad_value, **options) &#x21d2; <tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>
419
+
420
+
421
+
422
+
423
+
424
+ </h3><div class="docstring">
425
+ <div class="discussion">
426
+ <p>Returns <code>self</code> ... unless it matches a &quot;bad&quot; value, in which case it returns an alternate (<code>then:</code>) value.</p>
427
+ <p>(NOTE: this takes a <code>then: nil</code> named param, but is defined as taking <code>(**options)</code> because <code>then</code> is reserved.)</p>
428
+
429
+ </div>
430
+ </div>
431
+ <div class="tags">
432
+ <p class="tag_title">Parameters:</p>
433
+ <ul class="param">
434
+
435
+ <li>
436
+
437
+ <span class='name'>bad_value</span>
438
+
439
+
440
+ <span class='type'>(<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>)</span>
441
+
442
+
443
+
444
+ &mdash;
445
+ <div class='inline'><p>The one value we consider &quot;bad&quot;, and for which we'd like the <code>then:</code> option back instead.</p></div>
446
+
447
+ </li>
448
+
449
+ <li>
450
+
451
+ <span class='name'>then:</span>
452
+
453
+
454
+ <span class='type'>(<tt>Hash</tt>)</span>
455
+
456
+
457
+
458
+ &mdash;
459
+ <div class='inline'><p>a customizable set of options</p></div>
460
+
461
+ </li>
462
+
463
+ </ul>
464
+
465
+
466
+
467
+
468
+
469
+
470
+
471
+ <p class="tag_title">Returns:</p>
472
+ <ul class="return">
473
+
474
+ <li>
475
+
476
+
477
+ <span class='type'></span>
478
+
479
+
480
+
481
+
482
+ <div class='inline'><p>Returns either <code>self</code> or the value given as the <code>then:</code> option.</p></div>
483
+
484
+ </li>
485
+
486
+ </ul>
487
+
488
+ </div><table class="source_code">
489
+ <tr>
490
+ <td>
491
+ <pre class="lines">
492
+
493
+
494
+ 50
495
+ 51
496
+ 52
497
+ 53
498
+ 54
499
+ 55
500
+ 56
501
+ 57</pre>
502
+ </td>
503
+ <td>
504
+ <pre class="code"><span class="info file"># File 'lib/inline_transforms.rb', line 50</span>
505
+
506
+ <span class='kw'>def</span> <span class='kw'>unless</span><span class='lparen'>(</span><span class='id identifier rubyid_bad_value'>bad_value</span><span class='comma'>,</span> <span class='op'>**</span><span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
507
+ <span class='kw'>return</span> <span class='kw'>self</span> <span class='kw'>unless</span> <span class='id identifier rubyid_inline_transforms_equality?'>inline_transforms_equality?</span> <span class='id identifier rubyid_bad_value'>bad_value</span>
508
+
509
+ <span class='id identifier rubyid_alt_value'>alt_value</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:then</span><span class='rbracket'>]</span>
510
+ <span class='kw'>return</span> <span class='id identifier rubyid_alt_value'>alt_value</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span> <span class='kw'>if</span> <span class='id identifier rubyid_alt_value'>alt_value</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span> <span class='const'>Proc</span>
511
+
512
+ <span class='id identifier rubyid_alt_value'>alt_value</span>
513
+ <span class='kw'>end</span></pre>
514
+ </td>
515
+ </tr>
516
+ </table>
517
+ </div>
518
+
519
+ </div>
520
+
521
+ </div>
522
+
523
+ <div id="footer">
524
+ Generated on Tue Aug 25 16:11:47 2026 by
525
+ <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
526
+ 0.9.45 (ruby-3.4.8).
527
+ </div>
528
+
529
+ </div>
530
+ </body>
531
+ </html>