Add plugin/extension architecture: SafeMemoize::Extension API#51
Merged
Conversation
Third-party gems can now extend memoize without monkey-patching:
module MyExtension
extend SafeMemoize::Extension
handles_option :active_record_bust do |value, method_name, _opts|
{ cache_bust: -> { send(:updated_at) } }
end
on_cache_event :miss do |klass, method_name, _key, _record|
MyMetrics.increment("cache.miss")
end
end
SafeMemoize.register_extension(:active_record_bust, MyExtension)
SafeMemoize::Extension DSL (via extend):
handles_option(name, &processor) — declare a custom memoize option;
processor returns a Hash of standard options to inject at definition time
on_cache_event(*types, &handler) — global lifecycle event handler;
fires after every matching event across all memoized methods
Registry API on SafeMemoize module:
register_extension(name, ext) / unregister_extension(name)
extensions / reset_extensions!
extension_for_option(name) / dispatch_extension_events(...)
memoize now accepts **extension_options for unknown kwargs; each key
is validated against registered extensions at definition time and the
injected standard options (ttl:, namespace:, cache_bust:, etc.) are
applied before the normal memoize logic runs.
Duck-type compatibility: any object responding to handled_options,
process_memoize_option, and dispatch_cache_event works without
requiring extend SafeMemoize::Extension.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Shipped items (namespace, cross-instance sharing, cache_bust, extension architecture) are removed. DSL refinements remains as the only planned item — it requires community feedback before implementation. Co-Authored-By: Claude Sonnet 4.6 <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
SafeMemoize::Extension— a mixin DSL for building extensions that add custommemoizeoptions and global lifecycle handlers without monkey-patching SafeMemoize internalsmemoizenow accepts**extension_options; unknown kwargs are validated against registered extensions at definition time and raiseArgumentErrorif unclaimedcache_bust:,ttl:,namespace:,store:, etc.) atmemoizedefinition time viahandles_optionprocessor blockson_cache_eventhandlers fire after every matching lifecycle event across all memoized methods (main Ractor only)extend SafeMemoize::ExtensionrequiredRegistry API
Test plan
bundle exec rake— 788 examples, 0 failures, lint cleanspec/extension_spec.rb— 25 new examples covering DSL, registry, custom option injection, global event dispatch, and duck-type compatibility🤖 Generated with Claude Code