marameters 0.0.0 → 0.3.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 +158 -36
- data/lib/marameters/builder.rb +28 -0
- data/lib/marameters/defaulter.rb +29 -0
- data/lib/marameters/probe.rb +72 -0
- data/lib/marameters/signature.rb +25 -0
- data/lib/marameters.rb +0 -4
- data/marameters.gemspec +2 -2
- data.tar.gz.sig +0 -0
- metadata +18 -18
- metadata.gz.sig +0 -0
- data/lib/marameters/core.rb +0 -64
- data/lib/marameters/keyword.rb +0 -25
- data/lib/marameters/positional.rb +0 -23
- data/lib/marameters/splat.rb +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 832b6f5bce93dd251a0e1972c9e4ef234237dc1d1e94f6b8ca15814dec7ddc00
|
4
|
+
data.tar.gz: 23c459d6e85fa64da272f5c9b319084f188c31d0360856be9826a92f59b619d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0338c575983adaff7b72af01ddc941ba2084b095f073992e853c740ac2aac70f8c4f3eaff9cea4001802240393e52b0a6cceb00ac3d20b47eda320591546e7b7'
|
7
|
+
data.tar.gz: 9bddaf3365b3b3d60f766479a036d754f6323082d606a1120a99343145858ba92436177236bf6f1cb6901b810bba55184ff0512262061fe25d58cf3db36a3663
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/README.adoc
CHANGED
@@ -4,23 +4,23 @@
|
|
4
4
|
|
5
5
|
= Marameters
|
6
6
|
|
7
|
-
Marameters is
|
8
|
-
|
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
10
|
|
11
|
-
* *Parameters*: Represents the _expected_ values to be passed to a method when
|
12
|
-
when the method is implemented. Example: `def demo(one, two: nil)`.
|
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
13
|
* *Arguments*: Represents the _actual_ values passed to the method when messaged.
|
14
|
-
Example: `demo 1, two
|
14
|
+
Example: `demo 1, two: 2`.
|
15
15
|
|
16
16
|
This gem will help you debug methods or -- more importantly -- aid your workflow when
|
17
|
-
metaprogramming
|
17
|
+
metaprogramming, as used in the link:https://www.alchemists.io/projects/auto_injector[Auto Injector]
|
18
|
+
gem, when architecting more sophisticated applications.
|
18
19
|
|
19
20
|
toc::[]
|
20
21
|
|
21
22
|
== Features
|
22
23
|
|
23
|
-
* Provides a core object as a primary interface.
|
24
24
|
* Provides specialized objects for keyword, positional, and splatted parameters.
|
25
25
|
|
26
26
|
== Requirements
|
@@ -45,50 +45,172 @@ gem "marameters"
|
|
45
45
|
|
46
46
|
== Usage
|
47
47
|
|
48
|
-
|
49
|
-
|
48
|
+
There are two main objects you'll want to interact with:
|
49
|
+
|
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.
|
52
|
+
|
53
|
+
Both of these objects are meant to serve as building blocks to more complex architectures.
|
54
|
+
|
55
|
+
=== Probe
|
56
|
+
|
57
|
+
To understand how to analyze a method's parameters, consider the following demonstration class:
|
50
58
|
|
51
59
|
[source,ruby]
|
52
60
|
----
|
53
61
|
class Demo
|
54
|
-
def
|
55
|
-
|
62
|
+
def initialize logger: Logger.new(STDOUT)
|
63
|
+
@logger = logger
|
64
|
+
end
|
65
|
+
|
66
|
+
def all one, two = nil, *three, four:, five: nil, **six, &seven
|
67
|
+
logger.debug [one, two, three, four, five, six, seven]
|
56
68
|
end
|
69
|
+
|
70
|
+
def none = logger.debug "Nothing to see here."
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
attr_reader :logger
|
57
75
|
end
|
76
|
+
----
|
58
77
|
|
59
|
-
|
78
|
+
You can then probe the `#all` method's parameters as follows:
|
60
79
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
80
|
+
[source,ruby]
|
81
|
+
----
|
82
|
+
probe = Marameters::Probe.new Demo.instance_method(:all).parameters
|
83
|
+
|
84
|
+
probe.block # :seven
|
85
|
+
probe.block? # true
|
86
|
+
probe.empty? # false
|
87
|
+
probe.keywords # [:four, :five]
|
88
|
+
probe.keywords? # true
|
89
|
+
probe.kind?(:keyrest) # true
|
90
|
+
probe.kinds # [:req, :opt, :rest, :keyreq, :key, :keyrest, :block]
|
91
|
+
probe.name?(:three) # true
|
92
|
+
probe.names # [:one, :two, :three, :four, :five, :six, :seven]
|
93
|
+
probe.only_bare_splats? # false
|
94
|
+
probe.only_double_splats? # false
|
95
|
+
probe.only_single_splats? # false
|
96
|
+
probe.positionals # [:one, :two]
|
97
|
+
probe.positionals? # true
|
98
|
+
probe.splats # [:three, :six]
|
99
|
+
probe.splats? # true
|
100
|
+
probe.to_a # [[:req, :one], [:opt, :two], [:rest, :three], [:keyreq, :four], [:key, :five], [:keyrest, :six], [:block, :seven]]
|
101
|
+
probe.to_h # {:req=>:one, :opt=>:two, :rest=>:three, :keyreq=>:four, :key=>:five, :keyrest=>:six, :block=>:seven}
|
72
102
|
----
|
73
103
|
|
74
|
-
|
104
|
+
In contrast the above, we can also probe the `#none` method which has no parameters for a completely
|
105
|
+
different result:
|
75
106
|
|
76
107
|
[source,ruby]
|
77
108
|
----
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
109
|
+
probe = Marameters::Probe.new Demo.instance_method(:none).parameters
|
110
|
+
|
111
|
+
probe.block # nil
|
112
|
+
probe.block? # false
|
113
|
+
probe.empty? # true
|
114
|
+
probe.keywords # []
|
115
|
+
probe.keywords? # false
|
116
|
+
probe.kind?(:req) # true
|
117
|
+
probe.kinds # []
|
118
|
+
probe.name?(:three) # false
|
119
|
+
probe.names # []
|
120
|
+
probe.only_bare_splats? # false
|
121
|
+
probe.only_double_splats? # false
|
122
|
+
probe.only_single_splats? # false
|
123
|
+
probe.positionals # []
|
124
|
+
probe.positionals? # false
|
125
|
+
probe.splats # []
|
126
|
+
probe.splats? # false
|
127
|
+
probe.to_a # []
|
128
|
+
probe.to_h # {}
|
129
|
+
----
|
130
|
+
|
131
|
+
=== Signature
|
132
|
+
|
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.
|
83
136
|
|
84
|
-
|
85
|
-
|
137
|
+
The following demonstrates how you might construct a method signature with all possible parameters:
|
138
|
+
|
139
|
+
[source,ruby]
|
140
|
+
----
|
141
|
+
signature = Marameters::Signature.new(
|
142
|
+
{
|
143
|
+
req: :one,
|
144
|
+
opt: [:two, 2],
|
145
|
+
rest: :three,
|
146
|
+
keyreq: :four,
|
147
|
+
key: [:five, 5],
|
148
|
+
keyrest: :six,
|
149
|
+
block: :seven
|
150
|
+
}
|
151
|
+
)
|
152
|
+
|
153
|
+
puts signature
|
154
|
+
# one, two = 2, *three, four:, five: 5, **six, &seven
|
155
|
+
----
|
156
|
+
|
157
|
+
You'll notice that the parameters are a hash _and_ some values can be tuples. The reason is that
|
158
|
+
it's easier to write a hash than a double nested array as normally produced by the probe or directly
|
159
|
+
from `Method#parameters`. The optional positional and keyword parameters use tuples because you
|
160
|
+
might want to supply a default value and this provides a way for you to do that with minimal syntax.
|
161
|
+
This can be demonstrated further by using optional keywords (same applies for optional positionals):
|
162
|
+
|
163
|
+
[source,ruby]
|
164
|
+
----
|
165
|
+
# With no default
|
166
|
+
puts Marameters::Signature.new({key: :demo})
|
167
|
+
# demo: nil
|
168
|
+
|
169
|
+
# With explicit nil as default
|
170
|
+
puts Marameters::Signature.new({key: [:demo, nil]})
|
171
|
+
# demo: nil
|
172
|
+
|
173
|
+
# With string as default
|
174
|
+
puts Marameters::Signature.new({key: [:demo, "test"]})
|
175
|
+
# demo: "test"
|
176
|
+
|
177
|
+
# With symbol as default
|
178
|
+
puts Marameters::Signature.new({key: [:demo, :test]})
|
179
|
+
# demo: :test
|
180
|
+
|
181
|
+
# With object(dependency) as default
|
182
|
+
puts Marameters::Signature.new({key: [:demo, "*Object.new"]})
|
183
|
+
# demo: Object.new
|
184
|
+
----
|
185
|
+
|
186
|
+
In the case of object dependencies, you need to wrap these in a string _and_ prefix them with a star
|
187
|
+
(`*`) so the signature builder won't confuse them as normal strings. There are two reasons why this
|
188
|
+
is important:
|
189
|
+
|
190
|
+
* The star (`*`) signifies you want an object to be passed through without further processing while
|
191
|
+
also not being confused as a normal string.
|
192
|
+
* Objects wrapped as strings allows your dependency to be lazy loaded. Otherwise, if `Object.new`
|
193
|
+
was pass in directly, you'd be passing the evaluated instance (i.e.
|
194
|
+
`#<Object:0x0000000107df4028>`) which is not what you want until much later when your method is
|
195
|
+
defined.
|
196
|
+
|
197
|
+
When you put all of this together, you can dynamically build a method as follows:
|
198
|
+
|
199
|
+
[source,ruby]
|
200
|
+
----
|
201
|
+
signature = Marameters::Signature.new({opt: [:text, "This is a test."]})
|
202
|
+
|
203
|
+
Example = Module.new do
|
204
|
+
module_eval <<~DEFINITION, __FILE__, __LINE__ + 1
|
205
|
+
def self.say(#{signature}) = text
|
206
|
+
DEFINITION
|
207
|
+
end
|
86
208
|
|
87
|
-
|
88
|
-
|
209
|
+
puts Example.say
|
210
|
+
# This is a test.
|
89
211
|
|
90
|
-
|
91
|
-
|
212
|
+
puts Example.say "Hello"
|
213
|
+
# Hello
|
92
214
|
----
|
93
215
|
|
94
216
|
== Development
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Marameters
|
4
|
+
# Builds a single parameter of a method's parameter signature.
|
5
|
+
class Builder
|
6
|
+
def initialize defaulter: Defaulter
|
7
|
+
@defaulter = defaulter
|
8
|
+
end
|
9
|
+
|
10
|
+
# :reek:DuplicateMethodCall
|
11
|
+
def call kind, name, default: nil
|
12
|
+
case kind
|
13
|
+
when :req then name
|
14
|
+
when :opt then "#{name} = #{defaulter.call default}"
|
15
|
+
when :rest then "*#{name}"
|
16
|
+
when :keyreq then "#{name}:"
|
17
|
+
when :key then "#{name}: #{defaulter.call default}"
|
18
|
+
when :keyrest then "**#{name}"
|
19
|
+
when :block then "&#{name}"
|
20
|
+
else fail ArgumentError, "Wrong kind (#{kind}), name (#{name}), or default (#{default})."
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
attr_reader :defaulter
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Marameters
|
4
|
+
# Calculates the default for a given value when used within a method's parameter.
|
5
|
+
class Defaulter
|
6
|
+
PASSTHROUGH = "*"
|
7
|
+
|
8
|
+
def self.call(...) = new(...).call
|
9
|
+
|
10
|
+
def initialize value, passthrough: PASSTHROUGH
|
11
|
+
@value = value
|
12
|
+
@passthrough = passthrough
|
13
|
+
end
|
14
|
+
|
15
|
+
def call
|
16
|
+
case value
|
17
|
+
when nil then "nil"
|
18
|
+
when /\A#{Regexp.escape passthrough}/ then value.delete_prefix passthrough
|
19
|
+
when String then value.dump
|
20
|
+
when Symbol then value.inspect
|
21
|
+
else value
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
attr_reader :value, :passthrough
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "refinements/arrays"
|
4
|
+
|
5
|
+
module Marameters
|
6
|
+
# Provides insight into a method's parameters.
|
7
|
+
class Probe
|
8
|
+
using Refinements::Arrays
|
9
|
+
|
10
|
+
# :reek:TooManyStatements
|
11
|
+
def self.of klass, name, collection: []
|
12
|
+
method = klass.instance_method name
|
13
|
+
|
14
|
+
return collection unless method
|
15
|
+
|
16
|
+
collection << new(method.parameters)
|
17
|
+
super_method = method.super_method
|
18
|
+
of super_method.owner, super_method.name, collection:
|
19
|
+
rescue NameError
|
20
|
+
collection
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize parameters
|
24
|
+
@parameters = parameters
|
25
|
+
@items = parameters.reduce({}) { |all, (kind, name)| all.merge kind => name }
|
26
|
+
end
|
27
|
+
|
28
|
+
def block = items[:block]
|
29
|
+
|
30
|
+
def block? = items.key? :block
|
31
|
+
|
32
|
+
def empty? = items.empty?
|
33
|
+
|
34
|
+
def keyword_slice collection, keys:
|
35
|
+
collection.select { |key| !keys.include?(key) || keywords.include?(key) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def keywords = items.values_at(:keyreq, :key).compress!
|
39
|
+
|
40
|
+
def keywords? = keywords.any?
|
41
|
+
|
42
|
+
def kind?(kind) = items.key? kind
|
43
|
+
|
44
|
+
def kinds = items.keys
|
45
|
+
|
46
|
+
def name?(name) = items.value? name
|
47
|
+
|
48
|
+
def names = items.values
|
49
|
+
|
50
|
+
def only_bare_splats? = (parameters in [[:rest]] | [[:keyrest]] | [[:rest], [:keyrest]])
|
51
|
+
|
52
|
+
def only_double_splats? = (parameters in [[:keyrest, *]])
|
53
|
+
|
54
|
+
def only_single_splats? = (parameters in [[:rest, *]])
|
55
|
+
|
56
|
+
def positionals = items.values_at(:req, :opt).compress!
|
57
|
+
|
58
|
+
def positionals? = positionals.any?
|
59
|
+
|
60
|
+
def splats = items.values_at(:rest, :keyrest).compress!
|
61
|
+
|
62
|
+
def splats? = splats.any?
|
63
|
+
|
64
|
+
def to_a = parameters
|
65
|
+
|
66
|
+
def to_h = items
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
attr_reader :parameters, :items
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Marameters
|
4
|
+
# Produces a method signature for given parameters.
|
5
|
+
class Signature
|
6
|
+
def initialize parameters, builder: Builder.new
|
7
|
+
@parameters = parameters
|
8
|
+
@builder = builder
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s = build.join ", "
|
12
|
+
|
13
|
+
alias to_str to_s
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
attr_reader :parameters, :builder
|
18
|
+
|
19
|
+
def build
|
20
|
+
parameters.reduce [] do |signature, (kind, (name, default))|
|
21
|
+
signature << builder.call(kind, name, default:)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/marameters.rb
CHANGED
@@ -6,8 +6,4 @@ Zeitwerk::Loader.for_gem.setup
|
|
6
6
|
|
7
7
|
# Main namespace.
|
8
8
|
module Marameters
|
9
|
-
TRANSFORMER = lambda do |parameters, pattern|
|
10
|
-
parameters.select { |kind, _name| pattern.include? kind }
|
11
|
-
.reduce({}) { |collection, (kind, name)| collection.merge kind => name }
|
12
|
-
end
|
13
9
|
end
|
data/marameters.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
4
|
spec.name = "marameters"
|
5
|
-
spec.version = "0.
|
5
|
+
spec.version = "0.3.0"
|
6
6
|
spec.authors = ["Brooke Kuhlmann"]
|
7
7
|
spec.email = ["brooke@alchemists.io"]
|
8
8
|
spec.homepage = "https://www.alchemists.io/projects/marameters"
|
9
|
-
spec.summary = "Provides method parameter
|
9
|
+
spec.summary = "Provides dynamic method parameter construction and deconstruction."
|
10
10
|
spec.license = "Hippocratic-2.1"
|
11
11
|
|
12
12
|
spec.metadata = {
|
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.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brooke Kuhlmann
|
@@ -10,9 +10,9 @@ bindir: bin
|
|
10
10
|
cert_chain:
|
11
11
|
- |
|
12
12
|
-----BEGIN CERTIFICATE-----
|
13
|
-
MIIC/
|
14
|
-
|
15
|
-
|
13
|
+
MIIC/jCCAeagAwIBAgIBBTANBgkqhkiG9w0BAQsFADAlMSMwIQYDVQQDDBpicm9v
|
14
|
+
a2UvREM9YWxjaGVtaXN0cy9EQz1pbzAeFw0yMjAzMTkxNzI0MzJaFw0yMzAzMTkx
|
15
|
+
NzI0MzJaMCUxIzAhBgNVBAMMGmJyb29rZS9EQz1hbGNoZW1pc3RzL0RDPWlvMIIB
|
16
16
|
IjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6l1qpXTiomH1RfMRloyw7MiE
|
17
17
|
xyVx/x8Yc3EupdH7uhNaTXQGyORN6aOY//1QXXMHIZ9tW74nZLhesWMSUMYy0XhB
|
18
18
|
brs+KkurHnc9FnEJAbG7ebGvl/ncqZt72nQvaxpDxvuCBHgJAz+8i5wl6FhLw+oT
|
@@ -20,15 +20,15 @@ cert_chain:
|
|
20
20
|
D5vkU0YlAm1r98BymuJlcQ1qdkVEI1d48ph4kcS0S0nv1RiuyVb6TCAR3Nu3VaVq
|
21
21
|
3fPzZKJLZBx67UvXdbdicWPiUR75elI4PXpLIic3xytaF52ZJYyKZCNZJhNwfQID
|
22
22
|
AQABozkwNzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQU0nzow9vc
|
23
|
-
2CdikiiE3fJhP/
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
23
|
+
2CdikiiE3fJhP/gY4ggwDQYJKoZIhvcNAQELBQADggEBAJbbNyWzFjqUNVPPCUCo
|
24
|
+
IMrhDa9xf1xkORXNYYbmXgoxRy/KyNbUr+jgEEoWJAm9GXlcqxxWAUI6pK/i4/Qi
|
25
|
+
X6rPFEFmeObDOHNvuqy8Hd6AYsu+kP94U/KJhe9wnWGMmGoNKJNU3EkW3jM/osSl
|
26
|
+
+JRxiH5t4WtnDiVyoYl5nYC02rYdjJkG6VMxDymXTqn7u6HhYgZkGujq1UPar8x2
|
27
|
+
hNIWJblDKKSu7hA2d6+kUthuYo13o1sg1Da/AEDg0hoZSUvhqDEF5Hy232qb3pDt
|
28
|
+
CxDe2+VuChj4I1nvIHdu+E6XoEVlanUPKmSg6nddhkKn2gC45Kyzh6FZqnzH/CRp
|
29
|
+
RFE=
|
30
30
|
-----END CERTIFICATE-----
|
31
|
-
date: 2022-
|
31
|
+
date: 2022-04-07 00:00:00.000000000 Z
|
32
32
|
dependencies:
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: refinements
|
@@ -70,10 +70,10 @@ files:
|
|
70
70
|
- LICENSE.adoc
|
71
71
|
- README.adoc
|
72
72
|
- lib/marameters.rb
|
73
|
-
- lib/marameters/
|
74
|
-
- lib/marameters/
|
75
|
-
- lib/marameters/
|
76
|
-
- lib/marameters/
|
73
|
+
- lib/marameters/builder.rb
|
74
|
+
- lib/marameters/defaulter.rb
|
75
|
+
- lib/marameters/probe.rb
|
76
|
+
- lib/marameters/signature.rb
|
77
77
|
- marameters.gemspec
|
78
78
|
homepage: https://www.alchemists.io/projects/marameters
|
79
79
|
licenses:
|
@@ -100,8 +100,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
100
|
- !ruby/object:Gem::Version
|
101
101
|
version: '0'
|
102
102
|
requirements: []
|
103
|
-
rubygems_version: 3.3.
|
103
|
+
rubygems_version: 3.3.10
|
104
104
|
signing_key:
|
105
105
|
specification_version: 4
|
106
|
-
summary: Provides method parameter
|
106
|
+
summary: Provides dynamic method parameter construction and deconstruction.
|
107
107
|
test_files: []
|
metadata.gz.sig
CHANGED
Binary file
|
data/lib/marameters/core.rb
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Marameters
|
4
|
-
# Provides access to method arguments for meta-programming and/or debugging purposes.
|
5
|
-
class Core
|
6
|
-
# Order matters.
|
7
|
-
DELEGATES = {positional: Positional, keyword: Keyword, splat: Splat}.freeze
|
8
|
-
|
9
|
-
# :reek:TooManyStatements
|
10
|
-
def self.of klass, name, collection: []
|
11
|
-
method = klass.instance_method name
|
12
|
-
|
13
|
-
return collection unless method
|
14
|
-
|
15
|
-
collection << new(method.parameters)
|
16
|
-
super_method = method.super_method
|
17
|
-
of super_method.owner, super_method.name, collection:
|
18
|
-
rescue NameError
|
19
|
-
collection
|
20
|
-
end
|
21
|
-
|
22
|
-
def initialize parameters, delegates: DELEGATES
|
23
|
-
@parameters = parameters
|
24
|
-
|
25
|
-
@delegates = delegates.reduce({}) do |accumulator, (key, klass)|
|
26
|
-
accumulator.merge key => klass.new(parameters)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def empty? = parameters.empty?
|
31
|
-
|
32
|
-
def keyword?(name) = keyword.name? name
|
33
|
-
|
34
|
-
def keywords = keyword.names
|
35
|
-
|
36
|
-
def kinds = delegates.values.reduce([]) { |collection, delegate| collection + delegate.kinds }
|
37
|
-
|
38
|
-
def named_single_splat_only? = splat.named_single_only?
|
39
|
-
|
40
|
-
def names = delegates.values.reduce([]) { |collection, delegate| collection + delegate.names }
|
41
|
-
|
42
|
-
def positional? = !positional.empty?
|
43
|
-
|
44
|
-
def positionals = positional.names
|
45
|
-
|
46
|
-
def slice collection, keys:
|
47
|
-
collection.select { |key| !keys.include?(key) || keyword.names.include?(key) }
|
48
|
-
end
|
49
|
-
|
50
|
-
def to_a = parameters
|
51
|
-
|
52
|
-
def unnamed_splats_only? = splat.unnamed_only?
|
53
|
-
|
54
|
-
private
|
55
|
-
|
56
|
-
attr_reader :parameters, :delegates
|
57
|
-
|
58
|
-
def keyword = delegates.fetch(__method__)
|
59
|
-
|
60
|
-
def splat = delegates.fetch(__method__)
|
61
|
-
|
62
|
-
def positional = delegates.fetch(__method__)
|
63
|
-
end
|
64
|
-
end
|
data/lib/marameters/keyword.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Marameters
|
4
|
-
# Specializes in providing keyword method parameter information only.
|
5
|
-
class Keyword
|
6
|
-
PATTERN = %i[key keyreq].freeze
|
7
|
-
|
8
|
-
def initialize parameters, pattern: PATTERN, transformer: TRANSFORMER
|
9
|
-
@parameters = parameters
|
10
|
-
@collection = transformer.call parameters, pattern
|
11
|
-
end
|
12
|
-
|
13
|
-
def empty? = collection.empty?
|
14
|
-
|
15
|
-
def kinds = collection.keys
|
16
|
-
|
17
|
-
def name?(name) = collection.value? name
|
18
|
-
|
19
|
-
def names = collection.values
|
20
|
-
|
21
|
-
private
|
22
|
-
|
23
|
-
attr_reader :parameters, :collection
|
24
|
-
end
|
25
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Marameters
|
4
|
-
# Specializes in providing positional method paratemer information only.
|
5
|
-
class Positional
|
6
|
-
PATTERN = %i[req opt].freeze
|
7
|
-
|
8
|
-
def initialize parameters, pattern: PATTERN, transformer: TRANSFORMER
|
9
|
-
@parameters = parameters
|
10
|
-
@collection = transformer.call parameters, pattern
|
11
|
-
end
|
12
|
-
|
13
|
-
def empty? = collection.empty?
|
14
|
-
|
15
|
-
def kinds = collection.keys
|
16
|
-
|
17
|
-
def names = collection.values
|
18
|
-
|
19
|
-
private
|
20
|
-
|
21
|
-
attr_reader :parameters, :collection
|
22
|
-
end
|
23
|
-
end
|
data/lib/marameters/splat.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Marameters
|
4
|
-
# Specializes in providing passthrough method parameter information only.
|
5
|
-
class Splat
|
6
|
-
PATTERN = %i[rest keyrest].freeze
|
7
|
-
|
8
|
-
def initialize parameters, pattern: PATTERN, transformer: TRANSFORMER
|
9
|
-
@parameters = parameters
|
10
|
-
@collection = transformer.call parameters, pattern
|
11
|
-
end
|
12
|
-
|
13
|
-
def empty? = collection.empty?
|
14
|
-
|
15
|
-
def kinds = collection.keys
|
16
|
-
|
17
|
-
def names = collection.values
|
18
|
-
|
19
|
-
def named_double_only? = (parameters in [[:keyrest, *]])
|
20
|
-
|
21
|
-
def named_single_only? = (parameters in [[:rest, *]])
|
22
|
-
|
23
|
-
def unnamed_only? = (parameters in [[:rest]] | [[:keyrest]] | [[:rest], [:keyrest]])
|
24
|
-
|
25
|
-
private
|
26
|
-
|
27
|
-
attr_reader :parameters, :collection
|
28
|
-
end
|
29
|
-
end
|