standard_ledger 0.4.0 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 411ad3db9a71ca8adddecc84d407dabddbd167c7ed00e534e975a700b3a2e8f2
4
- data.tar.gz: aeb938a4e1fb16c6df67fbb76a93a5687c40116be040df5da44e65541ce4d282
3
+ metadata.gz: 74163637dfcb14c019f6481391644b45849cad04108b580e5a8de524720ed1ca
4
+ data.tar.gz: d022e52db16ba6e838c47c9e94ead8fa1f876afb37ffa5f8b3b5390d4877b157
5
5
  SHA512:
6
- metadata.gz: 2d0cd11b66c8d22b76cf9444e8840d92c35ccd81d4eba224e8123b208aa5c706503bc9cf6e9bbf1dc7bff564df4728adac79eeeacc254a976efb8cfa1fd0907a
7
- data.tar.gz: 5f352fe00a36e01ef43aa4d8efb197a32314c43f0b31657342996dc91bcf422933e4076b851813bd6c6fd7ab69a52d18f53e9650fbced76292b9deca52b6ce0b
6
+ metadata.gz: e9b7f1b51851431f1e44c5129dac815517ce064860a83d210d8f94ed8b30e7a90dc393d92af4e702516fcc0a804e613cc7859d582b4ce042d31220970835fb5d
7
+ data.tar.gz: c11d063fd3e01c8feb06176742df0167ff363c375de4c03d979a7dee52ca21e1388917fd2e1b10e770409b2a703aa5f223f496bc8f2619368b40fa0c323a1cb9
data/CHANGELOG.md CHANGED
@@ -6,7 +6,50 @@ project adheres to [Semantic Versioning](https://semver.org/).
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
- Nothing yet.
9
+ ## [0.5.1] - 2026-09-24
10
+
11
+ ### Fixed
12
+ - The `post_ledger_entry` RSpec matcher now listens on the channel
13
+ `StandardLedger::EventEmitter` emits through. It only subscribed to
14
+ `ActiveSupport::Notifications`, but on Rails 8.1+ the gem emits through
15
+ `Rails.event.notify`, so the matcher saw no events at all: every positive
16
+ expectation failed and every `not_to post_ledger_entry` passed without
17
+ checking anything. When `EventEmitter.rails_event_available?` is true, the
18
+ matcher now subscribes a collector to `Rails.event` for the block and
19
+ unsubscribes it afterwards.
20
+
21
+ ### Documentation
22
+ - README **Testing** section (and `AGENTS.md`) said the RSpec support hook
23
+ calls `StandardLedger.reset!`. It calls `reset_mode_overrides!`, which keeps
24
+ host initializer configuration intact.
25
+ - The engine initializer comment now tells hosts to subscribe via
26
+ `Rails.event.subscribe` on Rails 8.1+ rather than
27
+ `ActiveSupport::Notifications.subscribe`.
28
+
29
+ ## [0.5.0] - 2026-09-22
30
+
31
+ ### Changed
32
+ - `required_ruby_version` raised from `>= 3.4` to `>= 4.0`, and the repo's
33
+ `.ruby-version` moved from 3.4.4 to 4.0.1 to match. The 3.4 floor was never
34
+ exercised — CI has only ever run the 4.0.x matrix — and every consumer runs
35
+ 4.0.x. This aligns the gem with the rest of the `standard_*` family.
36
+
37
+ ### Documentation
38
+ - New README **Events** section documenting all five lifecycle events
39
+ (`entry.created`, `projection.applied`, `projection.failed`,
40
+ `projection.refreshed`, `projection.rebuilt`), their payloads, and the four
41
+ places a payload shape differs from the obvious one: `:sql` mode sends
42
+ `target: nil`, `:matview` events carry `view:`/`concurrently:` instead of
43
+ `entry:`/`target:`, `projection.rebuilt` carries `entry_class:` rather than
44
+ `entry:`, and input errors (matview name validation,
45
+ `RefreshInsideTransaction`) propagate without firing `projection.failed`.
46
+ Also records that subscriber exceptions are swallowed by design.
47
+ - README installation now shows the RubyGems pin. It still said "private during
48
+ incubation, pin from git"; the gem has been on RubyGems since 2026-05-07 and
49
+ the last git-pinned consumer was converted in 2026-07.
50
+ - `CLAUDE.md` consumer list corrected: it listed two consumers and claimed
51
+ git+tag consumption. There are four (`fundbright-web`, `luminality-web`,
52
+ `sidekick-web`, `jumpdrive-web`), all plain rubygems.
10
53
 
11
54
  ## [0.4.0] - 2026-05-07
12
55
 
@@ -423,7 +466,10 @@ roadmap.
423
466
  and `:trigger` (host-owned, gem records rebuild SQL).
424
467
  - `standard_ledger:doctor` rake task (verifies trigger presence, etc.).
425
468
 
426
- [Unreleased]: https://github.com/rarebit-one/standard_ledger/compare/v0.3.0...HEAD
469
+ [Unreleased]: https://github.com/rarebit-one/standard_ledger/compare/v0.5.1...HEAD
470
+ [0.5.1]: https://github.com/rarebit-one/standard_ledger/compare/v0.5.0...v0.5.1
471
+ [0.5.0]: https://github.com/rarebit-one/standard_ledger/compare/v0.4.0...v0.5.0
472
+ [0.4.0]: https://github.com/rarebit-one/standard_ledger/compare/v0.3.0...v0.4.0
427
473
  [0.3.0]: https://github.com/rarebit-one/standard_ledger/compare/v0.2.0...v0.3.0
428
474
  [0.2.0]: https://github.com/rarebit-one/standard_ledger/compare/v0.1.0...v0.2.0
429
475
  [0.1.0]: https://github.com/rarebit-one/standard_ledger/releases/tag/v0.1.0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Rarebit One
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -283,12 +283,16 @@ Five projection modes — pick per declaration:
283
283
 
284
284
  ## Installation
285
285
 
286
- The gem is private during incubation. Pin from git:
286
+ The gem is published on RubyGems:
287
287
 
288
288
  ```ruby
289
- gem "standard_ledger", git: "https://github.com/rarebit-one/standard_ledger", ref: "<sha>"
289
+ gem "standard_ledger", "~> 0.4"
290
290
  ```
291
291
 
292
+ (It was git-pinned during incubation. Don't reintroduce a `git:` reference —
293
+ it makes a bare `bundle install` a prerequisite for every other command in a
294
+ fresh checkout, blocking `rubocop`/`rspec` until it has been run.)
295
+
292
296
  Then run the install generator to drop a configured initializer in place:
293
297
 
294
298
  ```bash
@@ -317,6 +321,57 @@ StandardLedger.configure do |c|
317
321
  end
318
322
  ```
319
323
 
324
+ ## Events
325
+
326
+ The gem emits five lifecycle events. On Rails 8.1+ they go through
327
+ `Rails.event.notify(name, **payload)`; on older Rails (or any host without the
328
+ structured reporter) they fall back to
329
+ `ActiveSupport::Notifications.instrument(name, payload)`. The backend is
330
+ detected per call, not cached at load — the gem is required before Rails has
331
+ finished booting.
332
+
333
+ Every name is prefixed with `Config#notification_namespace` (default
334
+ `standard_ledger`), so a host that renames the namespace renames all five.
335
+
336
+ | Event | Fired when | Payload |
337
+ |---|---|---|
338
+ | `<prefix>.entry.created` | after the entry's transaction commits | `entry:`, `kind:`, `targets:` (a `{ name => target }` hash) |
339
+ | `<prefix>.projection.applied` | a projection wrote successfully | `entry:`, `target:`, `projection:`, `mode:`, `duration_ms:` — plus `attempt:` in `:async` mode |
340
+ | `<prefix>.projection.failed` | a projection raised | the `applied` payload plus `error:` (the exception) |
341
+ | `<prefix>.projection.refreshed` | a matview refresh succeeded | `view:`, `concurrently:`, `duration_ms:` |
342
+ | `<prefix>.projection.rebuilt` | `StandardLedger.rebuild!` finished one target | `entry_class:`, `target:`, `projection:`, `mode:` |
343
+
344
+ Four payload shapes are worth knowing before you write a subscriber that
345
+ assumes a key is always present:
346
+
347
+ - **`:sql` mode sends `target: nil`.** The recompute statement is bound by
348
+ `:target_id` and never loads the record, so there is no object to hand you.
349
+ - **`:matview` events carry `view:`/`concurrently:` instead of
350
+ `entry:`/`target:`.** A refresh is view-wide — Postgres has no partial-refresh
351
+ primitive — so no single entry or target caused it. The matview variant of
352
+ `projection.failed` also carries `mode: :matview`, while
353
+ `projection.refreshed` carries no `mode:` at all.
354
+ - **`projection.rebuilt` carries `entry_class:`, not `entry:`.** Rebuild is
355
+ log replay across an entire class; there is no originating entry.
356
+ - **`projection.failed` is not fired for input errors.** An `ArgumentError`
357
+ from the matview name validator, or `RefreshInsideTransaction` from the
358
+ boundary check, propagates without an event — the SQL was never issued, so
359
+ nothing failed to project.
360
+
361
+ **Subscriber exceptions are swallowed** (warned to stderr, not re-raised).
362
+ Ledger observability must never take down a host's request path: by emit time
363
+ the projection has already either succeeded or been rolled back, so there is
364
+ nothing a raising subscriber could usefully abort. Don't put work in a
365
+ subscriber that you need to have happened.
366
+
367
+ Retries: `:async` projections are capped by `Config#default_async_retries`
368
+ (default 3), and both `applied` and `failed` carry `attempt:` so subscribers
369
+ can tell first-try success from retry success.
370
+
371
+ `standard_audit` consumers can subscribe to `entry.created` to write an audit
372
+ row; the gem itself never calls into audit. That coupling is deliberately the
373
+ host's to opt into — see "Relationship to standard_audit" below.
374
+
320
375
  ## Testing
321
376
 
322
377
  The gem ships an opt-in RSpec support file. Hosts add this to their
@@ -326,12 +381,15 @@ The gem ships an opt-in RSpec support file. Hosts add this to their
326
381
  require "standard_ledger/rspec"
327
382
  ```
328
383
 
329
- That registers a `before(:each)` hook that calls `StandardLedger.reset!`
330
- between examples (so per-spec configuration doesn't leak), and exposes:
384
+ That registers a `before(:each)` hook that calls
385
+ `StandardLedger.reset_mode_overrides!` between examples (so `with_modes`
386
+ overrides don't leak). It deliberately does not call the full `reset!`, which
387
+ would wipe configuration your initializers set up. It also exposes:
331
388
 
332
389
  - `post_ledger_entry(EntryClass).with(...)` — a block matcher that
333
- subscribes to the `<namespace>.entry.created` notification for the
334
- duration of the block and asserts an entry of the expected class was
390
+ subscribes to the `<namespace>.entry.created` event for the duration of
391
+ the block, on the same channel the gem emits through (`Rails.event` on
392
+ Rails 8.1+, `ActiveSupport::Notifications` otherwise), and asserts an entry of the expected class was
335
393
  written (with optional `kind:`/`targets:`/`attrs:` constraints).
336
394
 
337
395
  ```ruby
@@ -382,4 +440,4 @@ Neither subsumes the other.
382
440
 
383
441
  ## License
384
442
 
385
- MIT. See [MIT-LICENSE](MIT-LICENSE).
443
+ MIT. See [LICENSE](LICENSE).
@@ -12,8 +12,10 @@ module StandardLedger
12
12
 
13
13
  initializer "standard_ledger.notifications", after: :load_config_initializers do
14
14
  # Notification wiring lands here in a follow-up PR. Today this is a
15
- # no-op — the gem emits events but ships no internal subscribers; hosts
16
- # subscribe directly via ActiveSupport::Notifications.subscribe.
15
+ # no-op — the gem emits events but ships no internal subscribers. On
16
+ # Rails 8.1+ events go through `Rails.event.notify`, so hosts subscribe
17
+ # with `Rails.event.subscribe`; on older Rails they fall back to
18
+ # ActiveSupport::Notifications (see StandardLedger::EventEmitter).
17
19
  end
18
20
 
19
21
  # Engines auto-discover `lib/tasks/*.rake` in most Rails versions, but
@@ -1,5 +1,28 @@
1
1
  require "rspec/expectations"
2
2
  require "active_support/notifications"
3
+ require "standard_ledger/event_emitter"
4
+
5
+ module StandardLedger
6
+ module RSpec
7
+ # Collects `<namespace>.entry.created` payloads from `Rails.event` for the
8
+ # duration of a `post_ledger_entry` block. A dedicated object (not a
9
+ # Proc) because `EventReporter#unsubscribe` matches with `===`.
10
+ #
11
+ # @api private
12
+ class RailsEventCollector
13
+ attr_reader :payloads
14
+
15
+ def initialize(event_name)
16
+ @event_name = event_name
17
+ @payloads = []
18
+ end
19
+
20
+ def emit(event)
21
+ @payloads << event[:payload] if event[:name] == @event_name
22
+ end
23
+ end
24
+ end
25
+ end
3
26
 
4
27
  # `post_ledger_entry` — assert that a block of code wrote a ledger entry.
5
28
  #
@@ -22,7 +45,9 @@ require "active_support/notifications"
22
45
  # expect { ... }.to_not post_ledger_entry(VoucherRecord)
23
46
  #
24
47
  # The matcher subscribes to `<namespace>.entry.created` for the duration of
25
- # the block, captures every fired event, and asserts that at least one event
48
+ # the block, on whichever channel `StandardLedger::EventEmitter` emits through
49
+ # (`Rails.event` on Rails 8.1+, `ActiveSupport::Notifications` otherwise),
50
+ # captures every fired event, and asserts that at least one event
26
51
  # matched the expected class (and, when chained, the expected `kind`,
27
52
  # `targets`, and `attrs`). The notification namespace is read from
28
53
  # `StandardLedger.config.notification_namespace`, so a host that customised
@@ -71,10 +96,35 @@ RSpec::Matchers.define :post_ledger_entry do |entry_class|
71
96
  # Helpers
72
97
  # ----------------------------------------------------------------------
73
98
 
99
+ # Listens on the same channel `EventEmitter.emit` picks. Listening only on
100
+ # AS::Notifications meant that on Rails 8.1 (where emit goes through
101
+ # `Rails.event`) the matcher saw nothing — every positive expectation
102
+ # failed and every negated one passed vacuously.
74
103
  def capture_entry_created_events(&block)
104
+ if StandardLedger::EventEmitter.rails_event_available?
105
+ capture_via_rails_event(&block)
106
+ else
107
+ capture_via_notifications(&block)
108
+ end
109
+ end
110
+
111
+ def capture_via_rails_event(&block)
112
+ bus = ::Rails.event
113
+ collector = StandardLedger::RSpec::RailsEventCollector.new(notification_event_name)
114
+ bus.subscribe(collector)
115
+
116
+ begin
117
+ block.call
118
+ ensure
119
+ bus.unsubscribe(collector)
120
+ end
121
+
122
+ collector.payloads
123
+ end
124
+
125
+ def capture_via_notifications(&block)
75
126
  events = []
76
- name = notification_event_name
77
- subscriber = ActiveSupport::Notifications.subscribe(name) do |*args|
127
+ subscriber = ActiveSupport::Notifications.subscribe(notification_event_name) do |*args|
78
128
  events << ActiveSupport::Notifications::Event.new(*args).payload
79
129
  end
80
130
 
@@ -1,3 +1,3 @@
1
1
  module StandardLedger
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standard_ledger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaryl Sim
@@ -134,7 +134,7 @@ extensions: []
134
134
  extra_rdoc_files: []
135
135
  files:
136
136
  - CHANGELOG.md
137
- - MIT-LICENSE
137
+ - LICENSE
138
138
  - README.md
139
139
  - Rakefile
140
140
  - lib/generators/standard_ledger/install/install_generator.rb
@@ -175,7 +175,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
175
175
  requirements:
176
176
  - - ">="
177
177
  - !ruby/object:Gem::Version
178
- version: '3.4'
178
+ version: '4.0'
179
179
  required_rubygems_version: !ruby/object:Gem::Requirement
180
180
  requirements:
181
181
  - - ">="
data/MIT-LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 Jaryl Sim
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.