candor 0.2.0 → 0.2.1
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 +4 -4
- data/CHANGELOG.md +14 -4
- data/README.md +67 -30
- data/candor.gemspec +3 -2
- data/lib/candor/signature.rb +3 -3
- data/lib/candor/version.rb +1 -1
- metadata +7 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4f762b3abe3c9761a038e98d5070ac771bada1d677bd84695e34c7ec623091fc
|
|
4
|
+
data.tar.gz: b2ae106ed934e85f4c55b2a28d070bbb57a1218f0f27e40c6620c23acd6a922d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 60b025033f7e8b208b74eb110b252814c9cabd5272b0c1681fd1d0421e2f48fe3511334c8378a86c4d489ade82ced0a5648b7e48d7da66552cc7bf277682e3f1
|
|
7
|
+
data.tar.gz: a2984ef22907efc8134743c2e2da12ea0b8bb1c381f6df792a1385a778957c9288a19f4fcfd509095cfd75eff572384d1eff2fbe06ba93d90780196ae7164d17
|
data/CHANGELOG.md
CHANGED
|
@@ -4,20 +4,30 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) and to [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/).
|
|
6
6
|
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [0.2.1] - 2026-07-31
|
|
10
|
+
|
|
11
|
+
No change to the library.
|
|
12
|
+
|
|
13
|
+
The gem now ships from a tagged CI run through RubyGems trusted publishing. This release and every later one carries a sigstore attestation. The GitHub release attaches the built gem beside a checksum manifest with build provenance.
|
|
14
|
+
|
|
7
15
|
## [0.2.0] - 2026-07-10
|
|
8
16
|
|
|
9
17
|
### Added
|
|
10
|
-
- `Candor.define(..., source_location:)
|
|
11
|
-
- `Candor::Signature.source_location
|
|
18
|
+
- `Candor.define(..., source_location:)`, the location every fabricated name reports, overriding the body's. A caller whose body is a proc it generated on the user's behalf can now point the wrapper at what the user actually wrote, rather than at the generator. It is also the only way to fabricate from a body carrying no location of its own (a curried proc, a symbol-to-proc, a C-defined method), which is the caller assuming responsibility for the honesty the gem otherwise derives. A `#call` object is not one of them and no `source_location:` rescues it: that refusal is about the kind. A `source_location:` that is not a `[String, Integer]` pair, or whose line is one `eval` will not take, raises `ArgumentError` before the target is touched.
|
|
19
|
+
- `Candor::Signature.source_location!`, the location gate, public beside `method_name!` and `parameters!`. `Signature.compile` calls it, so a consumer installing dispatch itself can no longer forge a location `eval` refuses, nor one it silently misreads. Its line must fit `eval`'s: a C `int`.
|
|
12
20
|
|
|
13
21
|
## [0.1.0] - 2026-07-10
|
|
14
22
|
|
|
15
23
|
### Added
|
|
16
|
-
- `Candor.define(target, name, aliases:, via:, parameters:, body:)`
|
|
17
|
-
- `Candor::Signature
|
|
24
|
+
- `Candor.define(target, name, aliases:, via:, parameters:, body:)` fabricates a real method whose arity, `parameters` kinds and `source_location` are the body's. Bodies may be a block, a `Proc`, a `Method` or an `UnboundMethod`; a `#call` object or any body with a `nil` `source_location` (a curried proc, a C-defined method) raises `TypeError`. With `via:` every call routes to that interceptor, which reaches the body through `Candor.body_name`; without it the wrapper forwards straight to the body. A wrong-arity call or an unknown keyword raises `ArgumentError` at the wrapper, before the interceptor or the body runs. An unpassed optional is dropped from the forwarded arguments, so the body applies its own default.
|
|
25
|
+
- `Candor::Signature`, the low-level compiler, public on its own: a parameter shape, a call-site name and a `source_location` in, a dispatch lambda out. Renders off a parameter's kind, never its name, so `end:`, `it`, `_1`, `**nil` and destructuring parameters all wrap at full fidelity. Both call-site names are validated against a strict method-name pattern, and a malformed `parameters` shape raises `ArgumentError` rather than a `SyntaxError` from inside `eval`. That includes a shape whose entries are each legal but whose combination Ruby's parser rejects, which is compiled before the target is mutated so a rejected fabrication cannot destroy the method it was replacing.
|
|
18
26
|
- Dispatch allocates nothing for shapes up to `Candor::Signature::KEYWORD_BRANCH_LIMIT` (2) optional keywords; beyond it, exactly one Hash per call. Past the limit a body's keyrest *is* that Hash, so a `**kw` in the signature costs only the capture and re-splat Ruby charges any wrapper. Exact from Ruby 3.4: on earlier Rubies the VM charges a `define_method`-created method for arguments crossing into it, and both the wrapper and the body are ones. A call carrying no keyword allocates nothing on every supported Ruby.
|
|
19
27
|
- Fabrication is thread-safe through one global `Monitor`; call-time dispatch takes no lock and never needs one. Re-fabricating a name overwrites the wrapper and the body in place, so it is warning-free under `ruby -w`, leaves no orphaned body, and a concurrent caller sees the old method or the new one, never a `NoMethodError`. A frozen target, an unbindable `Method`/`UnboundMethod` body, a name under the reserved `Candor::BODY_PREFIX`, an uncallable `via:` and a malformed `parameters:` all raise before the target is touched.
|
|
20
28
|
- `sig/` ships RBS for the public API: `Candor.define`, `Candor.body_name`, `BODY_PREFIX`, and `Candor::Signature` with `KINDS` and `KEYWORD_BRANCH_LIMIT`. Everything else is `private_constant`, which RBS cannot express and therefore does not declare.
|
|
21
29
|
|
|
30
|
+
[Unreleased]: https://github.com/svyatov/candor/compare/v0.2.1...HEAD
|
|
31
|
+
[0.2.1]: https://github.com/svyatov/candor/releases/tag/v0.2.1
|
|
22
32
|
[0.2.0]: https://github.com/svyatov/candor/releases/tag/v0.2.0
|
|
23
33
|
[0.1.0]: https://github.com/svyatov/candor/releases/tag/v0.1.0
|
data/README.md
CHANGED
|
@@ -1,21 +1,33 @@
|
|
|
1
|
-
# Candor
|
|
1
|
+
# Candor [](https://rubygems.org/gems/candor) [](https://app.codecov.io/gh/svyatov/candor) [](https://github.com/svyatov/candor/actions?query=workflow%3ACI)
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
[](https://codecov.io/gh/svyatov/candor)
|
|
3
|
+
Turn a block or a callable into a real Ruby method that reports the body's own arity, parameters and
|
|
4
|
+
source location.
|
|
6
5
|
|
|
7
|
-
**Ruby
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
- **Zero runtime dependencies.** Ruby 3.2 or newer and nothing else. Tested on 3.2, 3.3, 3.4, 4.0 and
|
|
7
|
+
`ruby-head`.
|
|
8
|
+
- **Every shape Ruby can express.** All eight parameter kinds wrap at full fidelity, including `end:`,
|
|
9
|
+
`it`, `_1`, `**nil` and destructuring. There is no degraded fallback.
|
|
10
|
+
- **Faster than the `|*args, **kwargs|` wrapper it replaces.** 52 ns and zero allocations per call
|
|
11
|
+
against 122 ns and two, on the no-argument shape. Full table below.
|
|
12
|
+
- **RBS signatures shipped.** `sig/` declares the public API, and the test suite pins it against the
|
|
13
|
+
code in both directions.
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
bundle add candor
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Or drop `gem "candor"` into your Gemfile, or run `gem install candor` without Bundler.
|
|
22
|
+
|
|
23
|
+
Then, anywhere in your code:
|
|
12
24
|
|
|
13
25
|
```ruby
|
|
14
26
|
Candor.define(Greeter, :greet) { |name, greeting: "hi"| "#{greeting}, #{name}" }
|
|
15
27
|
|
|
16
28
|
Greeter.instance_method(:greet).arity # => -2
|
|
17
29
|
Greeter.instance_method(:greet).parameters # => [[:req, :__p0], [:key, :greeting]]
|
|
18
|
-
Greeter.instance_method(:greet).source_location # => ["app/greeter.rb", 3]
|
|
30
|
+
Greeter.instance_method(:greet).source_location # => ["app/greeter.rb", 3] the block, not the gem
|
|
19
31
|
|
|
20
32
|
Greeter.new.greet # => ArgumentError: wrong number of arguments (given 0, expected 1)
|
|
21
33
|
Greeter.new.greet("bob", grating: "yo") # => ArgumentError: unknown keyword: :grating
|
|
@@ -37,12 +49,6 @@ Candor generates the parameter list as source and `eval`s it, once, at definitio
|
|
|
37
49
|
degraded fallback: every shape Ruby can express wraps at full fidelity, including `end:`, `it`, `_1`,
|
|
38
50
|
`**nil`, and destructuring parameters.
|
|
39
51
|
|
|
40
|
-
## Install
|
|
41
|
-
|
|
42
|
-
```ruby
|
|
43
|
-
gem "candor"
|
|
44
|
-
```
|
|
45
|
-
|
|
46
52
|
## Use
|
|
47
53
|
|
|
48
54
|
### Direct forward
|
|
@@ -103,14 +109,16 @@ klass.define_method(:greet, &dispatch)
|
|
|
103
109
|
```
|
|
104
110
|
|
|
105
111
|
`compile` gates all three of its inputs. `method_name!`, `parameters!` and `source_location!` are public
|
|
106
|
-
for a consumer that wants to fail earlier than the compile does.
|
|
112
|
+
for a consumer that wants to fail earlier than the compile does. `Candor::Signature::KINDS` is the frozen
|
|
113
|
+
list of the eight parameter kinds `Method#parameters` can emit, and the only ones `parameters!` and
|
|
114
|
+
`compile` accept.
|
|
107
115
|
|
|
108
116
|
## The contract
|
|
109
117
|
|
|
110
118
|
**Body kinds.** A block, a `Proc`, a `Method` or an `UnboundMethod`, the three things `define_method`
|
|
111
|
-
takes. A `#call` object
|
|
112
|
-
So
|
|
113
|
-
Ruby exposes no `curried?` predicate, and a nil `source_location` is the one reliable discriminator.
|
|
119
|
+
takes. A `#call` object raises `TypeError`, and no `source_location:` rescues it: the refusal is about the
|
|
120
|
+
kind. So does any body whose `source_location` is `nil`: a curried proc, a `Symbol#to_proc`, a C-defined
|
|
121
|
+
method. Ruby exposes no `curried?` predicate, and a nil `source_location` is the one reliable discriminator.
|
|
114
122
|
Without it the honest-`source_location` guarantee cannot be kept. An explicit `source_location:` is the
|
|
115
123
|
one way to fabricate from those three, and it is the caller taking responsibility for the honesty the gem
|
|
116
124
|
otherwise derives. Inside a block body, `self` is the receiver.
|
|
@@ -148,14 +156,32 @@ serialize. Call-time dispatch takes no lock at all, and never needs one: re-fabr
|
|
|
148
156
|
method in place rather than removing and reinstalling it, so a caller racing a `Candor.define` gets
|
|
149
157
|
the old method or the new one, never a `NoMethodError`.
|
|
150
158
|
|
|
151
|
-
**Failing fast.**
|
|
152
|
-
|
|
153
|
-
or
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
`
|
|
159
|
+
**Failing fast.** Every check runs *before* the target is touched, so a rejected fabrication leaves it
|
|
160
|
+
exactly as it was. A frozen target raises `FrozenError`. A non-Module target, a body that is none of the
|
|
161
|
+
three kinds or carries no location, and a `Method` or `UnboundMethod` whose owner is not an ancestor of
|
|
162
|
+
the target all raise `TypeError`. A name under the reserved prefix, a malformed `parameters:`, a malformed
|
|
163
|
+
`source_location:` or an uncallable `via:` raises `ArgumentError`. A `source_location:` is malformed
|
|
164
|
+
unless it is a `[String, Integer]` pair whose line `eval` will take. A hand-written `parameters:` never
|
|
165
|
+
passed Ruby's parser, so it is compiled before the first mutation: a shape whose *combination* is illegal
|
|
166
|
+
(two rests, a duplicate keyword) is an `ArgumentError` too. Nothing ever surfaces as a `SyntaxError` from
|
|
167
|
+
inside `eval`, which a `rescue` would not catch.
|
|
168
|
+
|
|
169
|
+
**The `eval`.** The one `eval` runs at definition time, never per call, and never on anything a caller
|
|
170
|
+
passes at runtime. Its source is rendered off the parameter *kinds*, not their names: every positional,
|
|
171
|
+
rest, keyrest and block name becomes a generated one, so the only user text that survives into the source
|
|
172
|
+
is a keyword name. That name is checked before it is rendered, and it cannot shadow the lambda's own
|
|
173
|
+
locals, which all carry a run-of-underscores prefix no keyword name starts with. The two call-site names,
|
|
174
|
+
the body's or `via:`, pass `method_name!` first, so a reserved word or anything `eval` would not call bare
|
|
175
|
+
raises `ArgumentError` before a character is compiled. You can read exactly what it will run: `Signature.render`
|
|
176
|
+
returns the source `compile` would `eval`, without evaluating it.
|
|
177
|
+
|
|
178
|
+
```ruby
|
|
179
|
+
Candor::Signature.render([%i[req a]], name: :greet, via: :__call)
|
|
180
|
+
# => "__u = ::Object.new.freeze; ->(__p0) { __call(:greet, __p0) }"
|
|
181
|
+
|
|
182
|
+
Candor::Signature.render([[:req, :name], [:key, :greeting]], name: Candor.body_name(:greet))
|
|
183
|
+
# => "__u = ::Object.new.freeze; ->(__p0, greeting: __u) { __u.equal?(greeting) ? __candor_body_greet(__p0) : __candor_body_greet(__p0, greeting: greeting) }"
|
|
184
|
+
```
|
|
159
185
|
|
|
160
186
|
**A Ruby 3.4 wrinkle.** On 3.4 alone, a body written with the implicit `it` parameter receives its
|
|
161
187
|
arguments packed into an Array when it is reached through `(name, ...)` forwarding, `send`, or a splat,
|
|
@@ -184,7 +210,7 @@ optional-argument rows carry a few percent of noise.
|
|
|
184
210
|
|
|
185
211
|
- **hand-written wrapper** is the ceiling: a real `def` with the same signature forwarding to the same
|
|
186
212
|
private body, with the defaults hard-coded. It is what you would write by hand if you knew the shape
|
|
187
|
-
*and* the default expressions. Candor is within ~10
|
|
213
|
+
*and* the default expressions. Candor is within ~10-40% of it, and allocates the same nothing.
|
|
188
214
|
- **bare method** is a single `def` doing the work inline. It is roughly twice as fast as any wrapper,
|
|
189
215
|
because it is one method call rather than two. That is the price of wrapping at all, not of candor.
|
|
190
216
|
- **variadic wrapper** is the `|*args, **kwargs, &block|` retreat: slower than candor on every shape
|
|
@@ -203,10 +229,21 @@ so the body defaults is the semantics that replaces it.
|
|
|
203
229
|
Definition must happen on the main Ractor. Calling a fabricated method from a non-main Ractor raises, as
|
|
204
230
|
it does for any `define_method`-installed `Proc`, unless both the dispatch and the body are shareable.
|
|
205
231
|
|
|
232
|
+
## Status and support
|
|
233
|
+
|
|
234
|
+
Candor is maintained. It is pre-1.0, so the API can still change between minor versions, and only the
|
|
235
|
+
latest release gets fixes.
|
|
236
|
+
|
|
237
|
+
Ask a question or report a bug in [GitHub issues](https://github.com/svyatov/candor/issues). Report a
|
|
238
|
+
security problem privately through the
|
|
239
|
+
[advisory form](https://github.com/svyatov/candor/security/advisories/new) instead, as
|
|
240
|
+
[SECURITY.md](SECURITY.md) describes.
|
|
241
|
+
|
|
206
242
|
## Contributing
|
|
207
243
|
|
|
208
|
-
|
|
209
|
-
[
|
|
244
|
+
Pull requests are welcome. [CONTRIBUTING.md](CONTRIBUTING.md) has the setup and test commands, and
|
|
245
|
+
states what an acceptable change has to satisfy. [CHANGELOG.md](CHANGELOG.md) records what changed in
|
|
246
|
+
each release, and the API docs are at [rubydoc.info/gems/candor](https://rubydoc.info/gems/candor).
|
|
210
247
|
|
|
211
248
|
## License
|
|
212
249
|
|
data/candor.gemspec
CHANGED
|
@@ -8,8 +8,9 @@ Gem::Specification.new do |spec|
|
|
|
8
8
|
spec.authors = ["Leonid Svyatov"]
|
|
9
9
|
spec.email = ["leonid@svyatov.com"]
|
|
10
10
|
|
|
11
|
-
spec.summary = "Turn a block or a callable into a real method
|
|
12
|
-
|
|
11
|
+
spec.summary = "Turn a block or a callable into a real Ruby method that reports the body's own " \
|
|
12
|
+
"arity, parameters and source location."
|
|
13
|
+
spec.description = "Candor fabricates real methods from blocks and " \
|
|
13
14
|
"callables: same arity, same parameters, source_location pointing at your code, and " \
|
|
14
15
|
"allocation-free dispatch. Zero runtime dependencies."
|
|
15
16
|
spec.homepage = "https://github.com/svyatov/candor"
|
data/lib/candor/signature.rb
CHANGED
|
@@ -75,9 +75,9 @@ module Candor
|
|
|
75
75
|
LINE_RANGE = (-(2**31))...(2**31)
|
|
76
76
|
|
|
77
77
|
# How the source is spelled, and what the two name gates happen to be spelled as. A consumer builds
|
|
78
|
-
# against {compile}, {method_name!}, {parameters!} and {source_location!} — which raise — not
|
|
79
|
-
# the patterns and tables they are written in terms of. Only {KINDS} and {KEYWORD_BRANCH_LIMIT}
|
|
80
|
-
# something a caller has to know to use the compiler, so only those two stay public.
|
|
78
|
+
# against {compile}, {render}, {method_name!}, {parameters!} and {source_location!} — which raise — not
|
|
79
|
+
# against the patterns and tables they are written in terms of. Only {KINDS} and {KEYWORD_BRANCH_LIMIT}
|
|
80
|
+
# name something a caller has to know to use the compiler, so only those two stay public.
|
|
81
81
|
private_constant :RESERVED_WORDS, :NAMED_KINDS, :OPTIONAL_KINDS, :SUFFIXES, :DECLARATIONS,
|
|
82
82
|
:METHOD_NAME, :KEYWORD_NAME, :NUMBERED_PARAMETERS, :LINE_RANGE
|
|
83
83
|
|
data/lib/candor/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: candor
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Leonid Svyatov
|
|
@@ -9,9 +9,9 @@ bindir: bin
|
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies: []
|
|
12
|
-
description: '
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
description: 'Candor fabricates real methods from blocks and callables: same arity,
|
|
13
|
+
same parameters, source_location pointing at your code, and allocation-free dispatch.
|
|
14
|
+
Zero runtime dependencies.'
|
|
15
15
|
email:
|
|
16
16
|
- leonid@svyatov.com
|
|
17
17
|
executables: []
|
|
@@ -52,7 +52,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
52
52
|
- !ruby/object:Gem::Version
|
|
53
53
|
version: '0'
|
|
54
54
|
requirements: []
|
|
55
|
-
rubygems_version: 4.0.
|
|
55
|
+
rubygems_version: 4.0.16
|
|
56
56
|
specification_version: 4
|
|
57
|
-
summary: Turn a block or a callable into a real method
|
|
57
|
+
summary: Turn a block or a callable into a real Ruby method that reports the body's
|
|
58
|
+
own arity, parameters and source location.
|
|
58
59
|
test_files: []
|