Refactor Coercer Caching to Use Grape::Util::Cache #2623
Merged
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.
Refactor Coercer Caching to Use Grape::Util::Cache
Summary
This PR refactors the coercer caching mechanism to use the standardized
Grape::Util::Cachepattern instead of manual cache management. This improves code consistency, maintainability, and follows the existing patterns used throughout the Grape codebase.Fix #2515
Changes
Core Refactoring
Grape::Util::Cachesingleton patternGrape::DryTypes::StrictCache- handles strict type mappingsGrape::DryTypes::ParamsCache- handles params type mappingsbuild_coercermethod by removing manual cache key generation and mutex-based synchronizationCoercerCacheclass that extendsGrape::Util::Cachefor coercer instancesCode Improvements
@__cacheand@__cache_write_lock) fromTypesmodulePrimitiveCoercerto use centralized cache classes instead of maintaining its own mappingsArrayCoercerto use cache classes directly instead of scope-based lookupDocumentation Updates
1.2tomain(current documentation)Dry.Types()inclusion to only what Grape uses (:params,:coercible,:strict)Test Updates
Benefits
PartsCache,PatternCache,JoinedSpaceCache)Files Changed
lib/grape/dry_types.rb- AddedStrictCacheandParamsCacheclasseslib/grape/validations/types.rb- Refactored caching to useCoercerCachelib/grape/validations/types/array_coercer.rb- Updated to use cache classeslib/grape/validations/types/dry_type_coercer.rb- Simplified to use cache classeslib/grape/validations/types/primitive_coercer.rb- Refactored to use centralized cachespec/grape/validations/types/primitive_coercer_spec.rb- Updated test expectationsspec/grape/validations/types_spec.rb- Simplified cache testsTesting
All existing tests pass. The refactoring maintains backward compatibility while improving the internal implementation.