marameters 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/README.adoc +18 -6
- data/lib/marameters/categorizer.rb +8 -2
- data/lib/marameters/probe.rb +0 -4
- data/marameters.gemspec +1 -1
- data.tar.gz.sig +0 -0
- metadata +2 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b53c9a80f1845fabb688e43162c18519192414425ef6a5b2a4c16e2575d29f99
|
4
|
+
data.tar.gz: e49e58811bb7e32ec2c8bee2691b6d230be4dc0f18503b86f5cebdfa812033c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4763d0f9492fce3673c1803884ae5cc6d49e64957f35649c7b10045a9146fbabf513a64d21346fd3642df9e481ba4f12cb59fcc361bae5fae0eb0cb3b7cc7c4
|
7
|
+
data.tar.gz: 4c6ffd0555c7d563759d4c25eb880f44ae8d4158d0399520bcb1301d64709c67a88b7aeedea69b6485c9598430b94d2a7d58529001435e6721fe704de742a023
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/README.adoc
CHANGED
@@ -13,7 +13,7 @@ Marameters is a portmanteau (i.e. `[m]ethod + p[arameters] = marameters`) which
|
|
13
13
|
* *Arguments*: Represents the _actual_ values passed to the method when messaged. Example: `demo 1, two: 2`.
|
14
14
|
|
15
15
|
This gem will help you debug methods or aid your workflow when
|
16
|
-
metaprogramming -- as used in the link:https://www.alchemists.io/projects/
|
16
|
+
metaprogramming -- as used in the link:https://www.alchemists.io/projects/infusible[Infusible] gem -- when architecting more sophisticated applications.
|
17
17
|
|
18
18
|
toc::[]
|
19
19
|
|
@@ -153,7 +153,7 @@ probe.to_a # []
|
|
153
153
|
|
154
154
|
=== Categorizer
|
155
155
|
|
156
|
-
The categorizer allows you to dynamically build positional, keyword, and block arguments for message passing. This is most valuable when you know the object
|
156
|
+
The categorizer allows you to dynamically build positional, keyword, and block arguments for message passing. This is most valuable when you know the object and method while needing to align the arguments in the right order. Here's a demonstration where {amazing_print_link} (i.e. `ap`) is used to format the output:
|
157
157
|
|
158
158
|
[source,ruby]
|
159
159
|
----
|
@@ -207,7 +207,7 @@ Inspector.call [1, nil, nil, {four: 4}]
|
|
207
207
|
|
208
208
|
When we step through the above implementation and output, we see the following unfold:
|
209
209
|
|
210
|
-
. The `Demo` module allows us define a maximum set of parameters and then print the arguments received for inspection purposes.
|
210
|
+
. The `Demo` module allows us to define a maximum set of parameters and then print the arguments received for inspection purposes.
|
211
211
|
. The `Inspector` module provides a wrapper around the `Categorizer` so we can conveniently pass in different arguments for experimentation purposes.
|
212
212
|
. We pass in our arguments to `Inspector.call` where `nil` is used for optional arguments and hashes for keyword arguments.
|
213
213
|
. Once inside `Inspector.call`, the `Categorizer` is initialized with the `Demo.test` method parameters.
|
@@ -248,7 +248,7 @@ Inspector.call [1, 2, [98, 99], {four: 4}, {five: 5}, {twenty: 20, thirty: 30},
|
|
248
248
|
# 7. #<Proc:0x000000010a88cec0 (irb):1>
|
249
249
|
----
|
250
250
|
|
251
|
-
Once again, it is important to keep in mind that the argument positions _must_ align with the parameter positions since the parameters are an array of elements too. For illustration purposes --
|
251
|
+
Once again, it is important to keep in mind that the argument positions _must_ align with the parameter positions since the parameters are an array of elements too. For illustration purposes -- using the above example -- we can compare the parameters to the arguments as follows:
|
252
252
|
|
253
253
|
[source,ruby]
|
254
254
|
----
|
@@ -280,10 +280,22 @@ ap arguments
|
|
280
280
|
|
281
281
|
This also means that:
|
282
282
|
|
283
|
-
* All positions
|
284
|
-
* The `:rest` (single splat) argument must an array or `nil` if not present because even though it is _optional_, it is still _positional_.
|
283
|
+
* All positions must be filled if you want to supply arguments beyond the first couple of positions because everything is positional due to the nature of how link:https://rubyapi.org/o/method#method-i-parameters[Method#parameters] works. Use `nil` to fill an optional argument when you don't need it.
|
284
|
+
* The `:rest` (single splat) argument must be an array or `nil` if not present because even though it is _optional_, it is still _positional_.
|
285
285
|
* The `:keyrest` (double splat) argument -- much like the `:rest` argument -- must be a hash or `nil` if not present.
|
286
286
|
|
287
|
+
Lastly, in all of the above examples, only an array of arguments has been used but you can pass in a single argument too (i.e. non-array). This is handy for method signatures which have only a single parameter or only use splats. Having to remember to wrap your argument in an array each time can get tedious so when _only_ a single argument is supplied, the categorizer will automatically cast the argument as an array. A good example of this use case is when using structs. Example:
|
288
|
+
|
289
|
+
[source,ruby]
|
290
|
+
----
|
291
|
+
url = Struct.new :label, :url, keyword_init: true
|
292
|
+
|
293
|
+
Marameters.categorize(url.method(:new).parameters, {label: "Eaxmple", url: "https://example.com"})
|
294
|
+
.then { |splat| url.new(*splat.positionals, **splat.keywords) }
|
295
|
+
|
296
|
+
# Yields: #<struct label="Eaxmple", url="https://example.com">
|
297
|
+
----
|
298
|
+
|
287
299
|
For further details, please refer back to my {article_link} article mentioned in the _Requirements_ section.
|
288
300
|
|
289
301
|
=== Signature
|
@@ -14,7 +14,7 @@ module Marameters
|
|
14
14
|
|
15
15
|
def call arguments
|
16
16
|
@record = model.new
|
17
|
-
map arguments
|
17
|
+
map arguments.is_a?(Array) ? arguments : [arguments]
|
18
18
|
record
|
19
19
|
end
|
20
20
|
|
@@ -23,7 +23,13 @@ module Marameters
|
|
23
23
|
attr_reader :parameters, :model, :record
|
24
24
|
|
25
25
|
def map arguments
|
26
|
-
|
26
|
+
return record if arguments.empty?
|
27
|
+
|
28
|
+
size = arguments.size
|
29
|
+
|
30
|
+
parameters.each.with_index do |pair, index|
|
31
|
+
filter pair, arguments[index] if index < size
|
32
|
+
end
|
27
33
|
end
|
28
34
|
|
29
35
|
def filter pair, value
|
data/lib/marameters/probe.rb
CHANGED
data/marameters.gemspec
CHANGED
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: marameters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brooke Kuhlmann
|
@@ -28,7 +28,7 @@ cert_chain:
|
|
28
28
|
CxDe2+VuChj4I1nvIHdu+E6XoEVlanUPKmSg6nddhkKn2gC45Kyzh6FZqnzH/CRp
|
29
29
|
RFE=
|
30
30
|
-----END CERTIFICATE-----
|
31
|
-
date: 2022-09-
|
31
|
+
date: 2022-09-08 00:00:00.000000000 Z
|
32
32
|
dependencies:
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: refinements
|
metadata.gz.sig
CHANGED
Binary file
|