Add conditional emission to emits DSL (v0.5.1) - #40
Merged
Conversation
Adds `if:` and `unless:` options to the `emits` macro, allowing services to gate event emission on runtime state without affecting callers or breaking any existing declarations. Also removes the `active_model_serializers` dependency, which was required but never used — and was the source of an ActiveSupport::Configurable deprecation warning targeting Rails 8.2 removal. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
if:andunless:options to theemitsmacro for runtime-conditional event emissionactive_model_serializersdependency (was triggering a Rails 8.2 deprecation warning)0.5.1Conditional emission
Services can now gate whether an event fires based on runtime state — without the event reaching the bus, building a payload, or running validation:
Both options accept a lambda/proc or a Symbol naming a private instance method. The condition receives the
resultobject, giving full access toresult.data,result.error,result.success?, etc.Fully backwards compatible —
emitsdeclarations without conditions are unaffected.The
with:option is unchanged in behaviour but is now part of**optionsalongsideif:andunless:for a uniform signature.active_model_serializers removal
The gem was declared as a runtime dependency and required at load time, but nothing in the codebase called into its APIs. Removing it drops one runtime dependency from consuming apps and eliminates the
ActiveSupport::Configurable is deprecatedwarning that AMS was triggering.Full test suite (768 examples) passes without it.
Changes
lib/servus/events/emitter.rbemitsaccepts**options; newemission_condition_met?andevaluate_emission_conditionprivate methods;emit_events_forskips blocked emissionslib/servus/testing/matchers.rbfailure_message_when_negatedtoemit_eventfor cleannot_to emit_event(...)failuresspec/servus/base_events_spec.rbsite/features/event-bus.mdsite/testing/services.mdnot_to emit_eventpatternlib/servus.rb/servus.gemspec/Gemfileactive_model_serializerslib/servus/version.rb0.5.1CHANGELOG.md0.5.1entryTest plan
bundle exec rspec— 768 examples, 0 failuresbundle exec rubocop— no offenses🤖 Generated with Claude Code