marameters 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: caaadc60d0c381ae6e86e9d7c9f5ceb6c11e5f0438b8d301f0e4fd43bb4b25f3
4
- data.tar.gz: 5e174ba972eeafe5040207e481db675a3bd54d13ef5effbc8c08de3e100c0579
3
+ metadata.gz: b609969a342c64c1ab5c475f0198bee7f8677d2bb9641234a9f13acbdfd39120
4
+ data.tar.gz: 11cdc1166b5c66f13d81558f1691e833d4378ddd4c04c547a0e6b5135e53a1d2
5
5
  SHA512:
6
- metadata.gz: d00d1a2e34d71d913ec250521e2f269fe12810fe032e70ce8d4c4297a3534d33b9c9a5bee5ad9efe167dee2042a8fcfa6d8b5d35defcaa5e63a19c2293aa7f35
7
- data.tar.gz: 342d1b8ac5013166ca937c79bd1b530abfe298ba7b527d316625d18738d7f30af543380d242d0d2253d1e19dbf5c82e6383ac8999e84dc92f11f24e107bd850e
6
+ metadata.gz: 6a1b20eb861d4ca7b1d821f52e77631dd23a71896973c259235c959e96e81f8e3dafc4bdea64729e758ff4ed0e42b06967a96e27e091f2ddaebb80c8d345fc4e
7
+ data.tar.gz: 0d7d78973c128a0281dd868d21ac56a0f742578c82f42531c78408487432ec0af217bfbe103fc0c2c1f6aa95545733df76aeb5d4236072e44df8bf72f6e1c80d
checksums.yaml.gz.sig CHANGED
Binary file
data/README.adoc CHANGED
@@ -2,30 +2,29 @@
2
2
  :toclevels: 5
3
3
  :figure-caption!:
4
4
 
5
+ :amazing_print_link: link:https://github.com/amazing-print/amazing_print[Amazing Print]
6
+ :article_link: link:https://www.alchemists.io/articles/ruby_method_parameters_and_arguments[method parameters and arguments]
7
+
5
8
  = Marameters
6
9
 
7
- Marameters is a portmanteau (i.e. `[m]ethod + p[arameters] = marameters`) which is designed to
8
- provide additional insight and diagnostics to method parameters. For context, the difference between
9
- a method's parameters and arguments is:
10
+ Marameters is a portmanteau (i.e. `[m]ethod + p[arameters] = marameters`) which is designed to provide additional insight and diagnostics for method parameters. For context, the difference between a method's parameters and arguments is:
10
11
 
11
- * *Parameters*: Represents the _expected_ values to be passed to a method when
12
- messaged as defined when the method is implemented. Example: `def demo(one, two: nil)`.
13
- * *Arguments*: Represents the _actual_ values passed to the method when messaged.
14
- Example: `demo 1, two: 2`.
12
+ * *Parameters*: Represents the _expected_ values to be passed to a method when messaged as defined when the method is implemented. Example: `def demo one, two: nil`.
13
+ * *Arguments*: Represents the _actual_ values passed to the method when messaged. Example: `demo 1, two: 2`.
15
14
 
16
- This gem will help you debug methods or -- more importantly -- aid your workflow when
17
- metaprogramming, as used in the link:https://www.alchemists.io/projects/auto_injector[Auto Injector]
18
- gem, when architecting more sophisticated applications.
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/auto_injector[Auto Injector] gem -- when architecting more sophisticated applications.
19
17
 
20
18
  toc::[]
21
19
 
22
20
  == Features
23
21
 
24
- * Provides specialized objects for keyword, positional, and splatted parameters.
22
+ * Provides specialized objects for keyword, positional, and block parameters.
25
23
 
26
24
  == Requirements
27
25
 
28
26
  . link:https://www.ruby-lang.org[Ruby].
27
+ . A solid understanding of {article_link}.
29
28
 
30
29
  == Setup
31
30
 
@@ -45,16 +44,54 @@ gem "marameters"
45
44
 
46
45
  == Usage
47
46
 
48
- There are two main objects you'll want to interact with:
47
+ At a high level, you can use `Marameters` as a single object interface for accessing all capabilities provided by this gem. Here's an overview:
48
+
49
+ [source,ruby]
50
+ ----
51
+ # Setup
52
+ def demo(one, two = 2, three: 3) = puts "One: #{one}, Two: #{two}, Three: #{three}"
53
+
54
+ parameters = method(:demo).parameters
55
+ arguments = %w[one two]
56
+
57
+ # Marameters::Categorizer wrapper
58
+
59
+ Marameters.categorize parameters, arguments
60
+ # #<struct Marameters::Splat positionals=["one", "two"], keywords={}, block=nil>
61
+
62
+ # Marameters::Probe wrapper
63
+
64
+ Marameters.of self, :demo # []
65
+
66
+ probe = Marameters.probe parameters
67
+ probe.to_h # {:req=>:one, :opt=>:two, :key=>:three}
68
+ probe.to_a # [[:req, :one], [:opt, :two], [:key, :three]]
69
+ probe.positionals # [:one, :two]
70
+ probe.keywords # [:three]
71
+ probe.block # nil
72
+
73
+ # Marameters::Signature wrapper
74
+
75
+ Marameters.signature({req: :one, opt: [:two, 2], key: [:three, 3]})
76
+ # one, two = 2, three: 3
77
+ ----
78
+
79
+ Read on to learn more about the details on how each of these methods work and the objects they wrap.
49
80
 
50
- * *Marameters::Probe*: Allows you to analyze a method's parameters.
51
- * *Marameters::Signature*: Allows you to dynamically build a method signature from raw parameters.
81
+ === Kinds
52
82
 
53
- Both of these objects are meant to serve as building blocks to more complex architectures.
83
+ Should you need a list of the kinds of parameters supported, you can use the `KINDS` constant. Example:
84
+
85
+ [source,ruby]
86
+ ----
87
+ Marameters::KINDS # [:req, :opt, :rest, :keyreq, :key, :keyrest, :block]
88
+ ----
89
+
90
+ All keys are listed in order of precedence.
54
91
 
55
92
  === Probe
56
93
 
57
- To understand how to analyze a method's parameters, consider the following demonstration class:
94
+ The probe allows you to analyze a method's parameters. To understand how, consider the following demonstration class:
58
95
 
59
96
  [source,ruby]
60
97
  ----
@@ -128,11 +165,143 @@ probe.to_a # []
128
165
  probe.to_h # {}
129
166
  ----
130
167
 
168
+ === Categorizer
169
+
170
+ The categorizer allows you to dynamically build positional, keyword, and block arguments for message passing. This is most valuable when you know the object, method, and arguments while also needing to assemble _and validate_ the arguments are in the right order. Here's a demonstration where {amazing_print_link} (i.e. `ap`) is used to format the output:
171
+
172
+ [source,ruby]
173
+ ----
174
+ function = proc { "test" }
175
+
176
+ module Demo
177
+ def self.test one, two = nil, *three, four:, five: nil, **six, &seven
178
+ puts "The .#{__method__} method received the following arguments:\n"
179
+
180
+ [one, two, three, four, five, six, seven].each.with_index 1 do |argument, index|
181
+ puts "#{index}. #{argument.inspect}"
182
+ end
183
+
184
+ puts
185
+ end
186
+ end
187
+
188
+ module Inspector
189
+ def self.call arguments
190
+ Marameters::Categorizer.new(Demo.method(:test).parameters)
191
+ .call(arguments).then do |splat|
192
+ ap splat
193
+ puts
194
+ Demo.test(*splat.positionals, **splat.keywords, &splat.block)
195
+ end
196
+ end
197
+ end
198
+
199
+ Inspector.call [1, nil, nil, {four: 4}]
200
+
201
+ # #<Struct:Marameters::Splat:0x00001068
202
+ # block = nil,
203
+ # keywords = {
204
+ # :four => 4
205
+ # },
206
+ # positionals = [
207
+ # 1
208
+ # ]
209
+ # >
210
+ #
211
+ # The .test method received the following arguments:
212
+ # 1. 1
213
+ # 2. nil
214
+ # 3. []
215
+ # 4. 4
216
+ # 5. nil
217
+ # 6. {}
218
+ # 7. nil
219
+ ----
220
+
221
+ When we step through the above implementation and output, we see the following unfold:
222
+
223
+ . The `Demo` module allows us define a maximum set of parameters and then print the arguments received for inspection purposes.
224
+ . The `Inspector` module provides a wrapper around the `Categorizer` so we can conveniently pass in different arguments for experimentation purposes.
225
+ . We pass in our arguments to `Inspector.call` where `nil` is used for optional arguments and hashes for keyword arguments.
226
+ . Once inside `Inspector.call`, the `Categorizer` is initialized with the `Demo.test` method parameters.
227
+ . Then the `splat` (i.e. Struct) is printed out so you can see the categorized positional, keyword, and block arguments.
228
+ . Finally, `Demo.test` method is called with the splatted arguments.
229
+
230
+ The above example satisfies the minimum required arguments but if we pass in the maximum arguments -- loosely speaking -- we see more detail:
231
+
232
+ [source,ruby]
233
+ ----
234
+ Inspector.call [1, 2, [98, 99], {four: 4}, {five: 5}, {twenty: 20, thirty: 30}, function]
235
+
236
+ # Output
237
+
238
+ #<Struct:Marameters::Splat:0x00001068
239
+ block = #<Proc:0x000000011b19e300 /snippet:32>,
240
+ keywords = {
241
+ :four => 4,
242
+ :five => 5,
243
+ :twenty => 20,
244
+ :thirty => 30
245
+ },
246
+ positionals = [
247
+ 1,
248
+ 2,
249
+ 98,
250
+ 99
251
+ ]
252
+ >
253
+
254
+ The .test method received the following arguments:
255
+ 1. 1
256
+ 2. 2
257
+ 3. [98, 99]
258
+ 4. 4
259
+ 5. 5
260
+ 6. {:twenty=>20, :thirty=>30}
261
+ 7. #<Proc:0x000000011b19e300 /snippet:32>
262
+ ----
263
+
264
+ 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 -- and using the above example -- we can compare the parameters to the arguments as follows:
265
+
266
+ [source,ruby]
267
+ ----
268
+ parameters = Demo.method(:test).parameters
269
+ arguments = [1, 2, [98, 99], {four: 4}, {five: 5}, {twenty: 20, thirty: 30}, function]
270
+ ----
271
+
272
+ With {amazing_print_link}, we can print out this information:
273
+
274
+ [source,ruby]
275
+ ----
276
+ ap parameters
277
+ ap arguments
278
+ ----
279
+
280
+ ...which can be further illustrated by this comparison table:
281
+
282
+ [options="header"]
283
+ |===
284
+ | Parameter | Argument
285
+ | `%i[reg one]` | `1`
286
+ | `%i[opt two]` | `2`
287
+ | `%i[rest three]` | `[98, 99]`
288
+ | `%i[keyreq four]` | `{four: 4}`
289
+ | `%i[key five]` | `{five: 5}`
290
+ | `%i[keyrest six]` | `{twenty: 20, thirty: 30}`
291
+ | `%i[block seven]` | `#<Proc:0x0000000108edc778>`
292
+ |===
293
+
294
+ This also means that:
295
+
296
+ * All positions much 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.
297
+ * The `:rest` (single splat) argument must an array or `nil` if not present because even though it is _optional_, it is still _positional_.
298
+ * The `:keyrest` (double splat) argument -- much like the `:rest` argument -- must be a hash or `nil` if not present.
299
+
300
+ For further details, please refer back to my {article_link} article mentioned in the _Requirements_ section.
301
+
131
302
  === Signature
132
303
 
133
- The signature class is the opposite of the probe in that you want to feed it parameters for turning
134
- into a method signature. This is useful when dynamically building method signatures or using the
135
- same signature when metaprogramming multiple methods.
304
+ The signature class is the inverse of the probe class in that you want to feed it parameters for turning into a method signature. This is useful when dynamically building method signatures or using the same signature when metaprogramming multiple methods.
136
305
 
137
306
  The following demonstrates how you might construct a method signature with all possible parameters:
138
307
 
@@ -7,7 +7,6 @@ module Marameters
7
7
  @defaulter = defaulter
8
8
  end
9
9
 
10
- # :reek:DuplicateMethodCall
11
10
  def call kind, name, default: nil
12
11
  case kind
13
12
  when :req then name
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "refinements/structs"
4
+
5
+ module Marameters
6
+ # Builds the primary argument categories based on method parameters and arguments.
7
+ class Categorizer
8
+ using Refinements::Structs
9
+
10
+ def initialize parameters, model: Splat
11
+ @parameters = parameters
12
+ @model = model
13
+ end
14
+
15
+ def call arguments
16
+ @record = model.new
17
+ map arguments
18
+ record
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :parameters, :model, :record
24
+
25
+ def map arguments
26
+ parameters.each.with_index { |pair, index| filter pair, arguments[index], arguments }
27
+ end
28
+
29
+ def filter pair, value, arguments
30
+ case pair
31
+ in [:rest] | [:rest, :*] then splat_positionals arguments
32
+ in [:keyrest] | [:keyrest, :**] then splat_keywords arguments
33
+ in [:block, :&] then forward_block arguments
34
+ in [:req, *] then record.positionals.append value
35
+ in [:opt, *] then record.positionals.append value if value
36
+ in [:rest, *] then record.positionals.append(*value)
37
+ in [:keyreq, *] | [:key, *] then record.keywords.merge! value if value
38
+ in [:keyrest, *] then record.keywords.merge!(**value) if value
39
+ in [:block, *] then record.block = value
40
+ else fail ArgumentError, "Invalid parameter kind: #{pair.first.inspect}."
41
+ end
42
+ rescue TypeError
43
+ raise TypeError, "#{value.inspect} is an invalid #{pair.first.inspect} value."
44
+ end
45
+
46
+ def splat_positionals arguments
47
+ arguments.reject { |item| item in Hash | Proc }
48
+ .flatten
49
+ .then { |values| record.positionals.append(*values) }
50
+ end
51
+
52
+ def splat_keywords arguments
53
+ arguments.each { |value| record.keywords.merge! value if value.is_a? Hash }
54
+ end
55
+
56
+ def forward_block arguments
57
+ arguments.find { |item| item.is_a? Proc }
58
+ .then { |block| record.block = block }
59
+ end
60
+ end
61
+ end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Marameters
4
- # Calculates the default for a given value when used within a method's parameter.
4
+ # Computes a method parameter's default value.
5
5
  class Defaulter
6
6
  PASSTHROUGH = "*"
7
7
 
@@ -3,7 +3,7 @@
3
3
  require "refinements/arrays"
4
4
 
5
5
  module Marameters
6
- # Provides insight into a method's parameters.
6
+ # Provides information on a method's parameters.
7
7
  class Probe
8
8
  using Refinements::Arrays
9
9
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Marameters
4
- # Produces a method signature for given parameters.
4
+ # Builds a method's parameter signature.
5
5
  class Signature
6
6
  def initialize parameters, builder: Builder.new
7
7
  @parameters = parameters
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Marameters
4
+ # Captures arguments, by category, for message splatting.
5
+ Splat = Struct.new :positionals, :keywords, :block, keyword_init: true do
6
+ def initialize *arguments
7
+ super
8
+
9
+ self[:positionals] ||= []
10
+ self[:keywords] ||= {}
11
+ end
12
+ end
13
+ end
data/lib/marameters.rb CHANGED
@@ -6,4 +6,13 @@ Zeitwerk::Loader.for_gem.setup
6
6
 
7
7
  # Main namespace.
8
8
  module Marameters
9
+ KINDS = %i[req opt rest keyreq key keyrest block].freeze
10
+
11
+ def self.categorize(parameters, arguments) = Categorizer.new(parameters).call(arguments)
12
+
13
+ def self.of(...) = Probe.of(...)
14
+
15
+ def self.probe(...) = Probe.new(...)
16
+
17
+ def self.signature(...) = Signature.new(...)
9
18
  end
data/marameters.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "marameters"
5
- spec.version = "0.6.0"
5
+ spec.version = "0.7.0"
6
6
  spec.authors = ["Brooke Kuhlmann"]
7
7
  spec.email = ["brooke@alchemists.io"]
8
8
  spec.homepage = "https://www.alchemists.io/projects/marameters"
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.6.0
4
+ version: 0.7.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-08-13 00:00:00.000000000 Z
31
+ date: 2022-09-01 00:00:00.000000000 Z
32
32
  dependencies:
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: refinements
@@ -71,9 +71,11 @@ files:
71
71
  - README.adoc
72
72
  - lib/marameters.rb
73
73
  - lib/marameters/builder.rb
74
+ - lib/marameters/categorizer.rb
74
75
  - lib/marameters/defaulter.rb
75
76
  - lib/marameters/probe.rb
76
77
  - lib/marameters/signature.rb
78
+ - lib/marameters/splat.rb
77
79
  - marameters.gemspec
78
80
  homepage: https://www.alchemists.io/projects/marameters
79
81
  licenses:
@@ -101,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
103
  - !ruby/object:Gem::Version
102
104
  version: '0'
103
105
  requirements: []
104
- rubygems_version: 3.3.19
106
+ rubygems_version: 3.3.21
105
107
  signing_key:
106
108
  specification_version: 4
107
109
  summary: Provides dynamic method parameter construction and deconstruction.
metadata.gz.sig CHANGED
Binary file