Skip to content

Fix proc/block self binding in emits (v0.5.2) - #41

Merged
sebscholl merged 1 commit into
mainfrom
fix/emits-proc-instance-exec
May 25, 2026
Merged

Fix proc/block self binding in emits (v0.5.2)#41
sebscholl merged 1 commit into
mainfrom
fix/emits-proc-instance-exec

Conversation

@sebscholl

Copy link
Copy Markdown
Contributor

Summary

Patch fix for a bug introduced in 0.5.1 where self inside payload builder blocks and if:/unless: condition procs was bound to the class rather than the service instance.

The bug

emits accepts procs/lambdas for payload building and conditions. These were executed via call, which preserves the proc's original self — the class at definition time. This meant instance variables set in initialize or call were silently unreadable:

emits :event, on: :success, unless: ->(_result) { @suppress }  # always nil — reads class ivar
emits :event, on: :success do |_result| { label: @label } end  # @label always nil

The fix

Switch from builder.call(result) to instance_exec(result, &builder) in both build_event_payload and evaluate_emission_condition. This binds self to the service instance, matching the existing behaviour of Symbol method references.

# Now works correctly
emits :event, on: :success, unless: ->(_result) { @suppress }
emits :event, on: :success do |_result| { label: @label } end

Any code reading only from result (all documented usage prior to 0.5.1) is completely unaffected.

Test plan

  • Regression test: condition proc reads instance variable set in initialize
  • Regression test: payload builder block reads instance variable set in initialize
  • bundle exec rspec — 770 examples, 0 failures
  • bundle exec rubocop — no offenses

🤖 Generated with Claude Code

Payload builder blocks and if:/unless: condition procs were executing
with self bound to the class rather than the service instance, making
instance variables set in initialize or call silently unreadable.
Switching from call to instance_exec matches the behaviour of Symbol
method references and is consistent with how Rails DSLs handle this.

Regression tests added for both the condition and payload builder cases.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@sebscholl
sebscholl merged commit 1de4fdc into main May 25, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant