inline_transforms 0.1.0 → 0.3.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: ff2ee8053b8af9abd8a87c51f631c41e50f487816a86061107cbc09731df311f
4
+ data.tar.gz: 3583bf28d21bf28654f8dd37a404ca1118a279c90a819be5e72f03c00c6190db
5
5
  SHA512:
6
- metadata.gz: 423e20167b171a3a929c9dcc063d9bcb7a10cc91a1f0b6cab20817df8ababf10471f96196c09994dad0e7b0999ac324d1466d19d99f680a7ebdb5b5a57e77ea8
7
- data.tar.gz: 65bb28caa0f2d10c3a45a5a45a05574f55f230c77f47783bd5ec819280d62809c1cd2808d314176f62588e02f107d42ccb17caac0121ed16f9d205d59bd9bae1
6
+ metadata.gz: 4d4e62ff7f0b687407d47b5887715805e3c86ec645027c53c75ae70ecca2102ff49e508aaac815a5fc4f1bdb33aca1f2967c006e363a8ac57bea71f04e3919dd
7
+ data.tar.gz: d4eca27ab651c2f31dd93f60a261f1087407eabe9258e0981a9bf53bced6a6da646222afa78e3f9d907697905a792ce97e35b574c25647791b6ebde5d91a9672
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,11 +86,31 @@ 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
+ - If it has arity 0, it is `call`ed with no params.
95
+ - If it has arity 1, it is `call`ed with the original value.
96
+ - If it has arity -1 (a `lambda` with optional param), it is `call`ed with the original value.
97
+ - This method uses **case comparison** (`===`), so you can check for range inclusion or class.
98
+
99
+ ```ruby
100
+ final = value.only_if good_value, else: fallback_value
101
+ # ... or ...
102
+ final = value.only_if good_value, else: -> { some_method_call with_params }
103
+ ```
104
+
105
+
71
106
  ### Object#unless
72
107
 
73
108
  `unless` lets you specify a "bad" value and a `then` replacement.
74
109
 
75
110
  - If your `then` replacement is a `Proc`, it is resolved (via `call`) before being returned.
111
+ - If it has arity 0, it is `call`ed with no params.
112
+ - If it has arity 1, it is `call`ed with the original value.
113
+ - If it has arity -1 (a `lambda` with optional param), it is `call`ed with the original value.
76
114
  - This method uses **case comparison** (`===`), so you can check for range inclusion or class.
77
115
 
78
116
  ```ruby
@@ -87,6 +125,9 @@ final = value.unless bad_value, then: -> { some_method_call with_params }
87
125
  `transform` lets you specify a transformation hash and will return the value for the first matching key, or (if no matching key is found) the `:else` value.
88
126
 
89
127
  - Any `Proc` values in the transform hash are resolved (via `call`) before being returned.
128
+ - Values with arity 0 are `call`ed with no params.
129
+ - Values with arity 1 are `call`ed with the original value.
130
+ - Values with arity -1 (`lambda`s with an optional param) are `call`ed with the original value.
90
131
  - This method uses **case comparison** (`===`), so range and class keys work as you expect.
91
132
 
92
133
  ```ruby
@@ -99,6 +140,8 @@ final = value.transform key_1 => value_1,
99
140
 
100
141
  ## Potential Gotchas
101
142
 
143
+ - Because `else:` has special meaning, `transform` cannot check for the original value being the literal `Symbol` `:else`.
144
+
102
145
  - `Proc` instances are only resolved if they _need to be returned and are not the original value_:
103
146
  ```ruby
104
147
  value = -> { 'value proc' }
@@ -0,0 +1,519 @@
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.3.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</pre>
296
+ </td>
297
+ <td>
298
+ <pre class="code"><span class="info file"># File 'lib/inline_transforms.rb', line 26</span>
299
+
300
+ <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>
301
+ <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>
302
+
303
+ <span class='id identifier rubyid_inline_transforms_response_processor'>inline_transforms_response_processor</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:else</span><span class='rbracket'>]</span>
304
+ <span class='kw'>end</span></pre>
305
+ </td>
306
+ </tr>
307
+ </table>
308
+ </div>
309
+
310
+ <div class="method_details ">
311
+ <h3 class="signature " id="transform-instance_method">
312
+
313
+ #<strong>transform</strong>(**options) &#x21d2; <tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>
314
+
315
+
316
+
317
+
318
+
319
+ </h3><div class="docstring">
320
+ <div class="discussion">
321
+ <p>Returns <code>self</code> ... unless it matches one of the keys in the given &quot;transformation hash&quot;, in which case the
322
+ corresponding hash value is returned. If no matching hash key is found, this returns a fallback (<code>else:</code>) value.</p>
323
+ <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>
324
+
325
+ </div>
326
+ </div>
327
+ <div class="tags">
328
+ <p class="tag_title">Parameters:</p>
329
+ <ul class="param">
330
+
331
+ <li>
332
+
333
+ <span class='name'>**</span>
334
+
335
+
336
+ <span class='type'>(<tt>Hash</tt>)</span>
337
+
338
+
339
+
340
+ &mdash;
341
+ <div class='inline'><p>a customizable set of options</p></div>
342
+
343
+ </li>
344
+
345
+ <li>
346
+
347
+ <span class='name'>else:</span>
348
+
349
+
350
+ <span class='type'>(<tt>Hash</tt>)</span>
351
+
352
+
353
+
354
+ &mdash;
355
+ <div class='inline'><p>a customizable set of options</p></div>
356
+
357
+ </li>
358
+
359
+ </ul>
360
+
361
+
362
+
363
+
364
+
365
+ <p class="tag_title">Returns:</p>
366
+ <ul class="return">
367
+
368
+ <li>
369
+
370
+
371
+ <span class='type'></span>
372
+
373
+
374
+
375
+
376
+ <div class='inline'><p>Returns either <code>self</code>, one of the transformation hash values, or the <code>else:</code> option.</p></div>
377
+
378
+ </li>
379
+
380
+ </ul>
381
+
382
+ </div><table class="source_code">
383
+ <tr>
384
+ <td>
385
+ <pre class="lines">
386
+
387
+
388
+ 68
389
+ 69
390
+ 70
391
+ 71
392
+ 72
393
+ 73</pre>
394
+ </td>
395
+ <td>
396
+ <pre class="code"><span class="info file"># File 'lib/inline_transforms.rb', line 68</span>
397
+
398
+ <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>
399
+ <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>
400
+
401
+ <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_inline_transforms_response_processor'>inline_transforms_response_processor</span> <span class='id identifier rubyid_value'>value</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>
402
+ <span class='id identifier rubyid_inline_transforms_response_processor'>inline_transforms_response_processor</span> <span class='id identifier rubyid_default'>default</span>
403
+ <span class='kw'>end</span></pre>
404
+ </td>
405
+ </tr>
406
+ </table>
407
+ </div>
408
+
409
+ <div class="method_details ">
410
+ <h3 class="signature " id="unless-instance_method">
411
+
412
+ #<strong>unless</strong>(bad_value, **options) &#x21d2; <tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>
413
+
414
+
415
+
416
+
417
+
418
+ </h3><div class="docstring">
419
+ <div class="discussion">
420
+ <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>
421
+ <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>
422
+
423
+ </div>
424
+ </div>
425
+ <div class="tags">
426
+ <p class="tag_title">Parameters:</p>
427
+ <ul class="param">
428
+
429
+ <li>
430
+
431
+ <span class='name'>bad_value</span>
432
+
433
+
434
+ <span class='type'>(<tt><span class='object_link'><a href="Object.html" title="Object (class)">Object</a></span></tt>)</span>
435
+
436
+
437
+
438
+ &mdash;
439
+ <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>
440
+
441
+ </li>
442
+
443
+ <li>
444
+
445
+ <span class='name'>then:</span>
446
+
447
+
448
+ <span class='type'>(<tt>Hash</tt>)</span>
449
+
450
+
451
+
452
+ &mdash;
453
+ <div class='inline'><p>a customizable set of options</p></div>
454
+
455
+ </li>
456
+
457
+ </ul>
458
+
459
+
460
+
461
+
462
+
463
+
464
+
465
+ <p class="tag_title">Returns:</p>
466
+ <ul class="return">
467
+
468
+ <li>
469
+
470
+
471
+ <span class='type'></span>
472
+
473
+
474
+
475
+
476
+ <div class='inline'><p>Returns either <code>self</code> or the value given as the <code>then:</code> option.</p></div>
477
+
478
+ </li>
479
+
480
+ </ul>
481
+
482
+ </div><table class="source_code">
483
+ <tr>
484
+ <td>
485
+ <pre class="lines">
486
+
487
+
488
+ 47
489
+ 48
490
+ 49
491
+ 50
492
+ 51</pre>
493
+ </td>
494
+ <td>
495
+ <pre class="code"><span class="info file"># File 'lib/inline_transforms.rb', line 47</span>
496
+
497
+ <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>
498
+ <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>
499
+
500
+ <span class='id identifier rubyid_inline_transforms_response_processor'>inline_transforms_response_processor</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:then</span><span class='rbracket'>]</span>
501
+ <span class='kw'>end</span></pre>
502
+ </td>
503
+ </tr>
504
+ </table>
505
+ </div>
506
+
507
+ </div>
508
+
509
+ </div>
510
+
511
+ <div id="footer">
512
+ Generated on Wed Aug 26 23:26:19 2026 by
513
+ <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
514
+ 0.9.45 (ruby-3.4.8).
515
+ </div>
516
+
517
+ </div>
518
+ </body>
519
+ </html>