This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Cacheable is a Ruby gem by Splitwise that adds method caching via an AOP (Aspect-Oriented Programming) pattern. Include Cacheable in a class, annotate methods with cacheable :method_name, and results are automatically cached.
# Install dependencies
bundle install
# Run full default task (rubocop + rspec)
bundle exec rake
# Run tests only
bundle exec rspec
# Run a single test file
bundle exec rspec spec/cacheable/cacheable_spec.rb
# Run a single test by line number
bundle exec rspec spec/cacheable/cacheable_spec.rb:45
# Run linter only
bundle exec rubocop
# Auto-fix lint issues
bundle exec rubocop -a
# Watch and auto-run tests/lint on file changes
bundle exec guardThe gem uses module prepending with dynamic method generation to intercept and cache method calls.
-
Cacheable(lib/cacheable.rb) — The module users include. Onincluded, it extends the host class withMethodGeneratorand creates a unique anonymous interceptor module that gets prepended to the class. -
MethodGenerator(lib/cacheable/method_generator.rb) — Whencacheable :method_nameis called, this generates five methods on the interceptor module:method_name(override) — dispatcher that routes towith_cacheorwithout_cachebased on theunless:conditionmethod_with_cache— fetch from cache or compute and storemethod_without_cache— bypass cache, call originalmethod_key_format— generate the cache keyclear_method_cache— invalidate the cache entry
-
CacheAdapter(lib/cacheable/cache_adapter.rb) — Protocol for cache backends. Default is:memory. Required interface:fetch(key, options, &block)anddelete(key). -
MemoryAdapter(lib/cacheable/cache_adapters/memory_adapter.rb) — Built-in hash-backed in-memory cache. Production use typically wires inRails.cacheor a custom adapter.
- Each class that includes
Cacheablegets its own unique interceptor module (created viaModule.new), which is prepended to the class. This is howsuperchains through to the original method. - The
unless:option accepts a proc/symbol that, when truthy, skips caching and calls the original method directly. key_format:accepts a proc receiving(target, method_name, args, **kwargs)for custom cache key generation.
- Max line length: 120 characters
- Max method length: 25 lines
- Rubocop enforced with
NewCops: enable - No frozen string literal comments required