Extracting Code into a Hanami Slice
Move functionality from the monolithic app module into an isolated slice. Preserve behavior — only change structure.
Quick Reference
- Goal: Increase modularity by isolating a domain into its own slice.
- Pattern: Identify bounded context → create slice → move files → update imports → verify.
- Rule: Extraction must not change behavior. Run tests before and after.
HARD-GATE
DO NOT change behavior during extraction. Every existing test must pass after the move.
DO NOT extract code without tests. Characterization tests first if missing.
DO leave the original module empty after extraction — remove dead code.
Core Process
- Identify the boundary — what code belongs together? Look for:
- Files that share a namespace (e.g.,
App::Payments::*). - Operations, repositories, and actions that serve a single domain.
- Code that only references itself (no cross-domain coupling).
- Files that share a namespace (e.g.,
- Characterize behavior — ensure all existing functionality is tested. If not, write characterization tests first. Capture a baseline:
bundle exec rspec - Create the target slice — use
create-sliceto scaffold the new slice structure. - Move files — relocate from
app/orslices/app/toslices/<new_slice>/usinggit mvto preserve history:
Directory mapping:git mv app/actions/payments/ slices/payments/actions/ git mv app/operations/payments/ slices/payments/operations/ git mv app/repositories/payments/ slices/payments/repositories/- Actions →
slices/<slice>/actions/ - Operations →
slices/<slice>/operations/ - Repositories →
slices/<slice>/repositories/ - Relations →
slices/<slice>/relations/ - Views →
slices/<slice>/views/
- Actions →
- Update namespaces — change module nesting to the new slice namespace. Example:
# Before module App module Payments class CreateOrder < App::Operation # ... end end end # After module Payments class CreateOrder < Payments::Operation # ... end end - Update imports — any remaining code referencing the old namespace must be updated. Check:
include Deps[...]keysrequirestatements- Route definitions
- Provider registrations
- Verify — run the full test suite. Every test that passed before must pass after:
bundle exec rspec - Remove old code — clean up the original module. Remove empty directories.
Extended Resources (Progressive Disclosure)
Load these files only when needed:
- EXTRACTION_PATTERNS.md — Detailed file moving guide, namespace rewriting, common pitfalls.
Output Style
- Extraction plan — which files move, what namespaces change.
- Before/after structure — directory tree before and after extraction.
- Import changes — every
Depskey,require, and reference that changed. - Verification — test results before and after extraction.
- English only unless user requests otherwise.
Integration
| Skill | When to chain |
|---|---|
| create-slice | Create the target slice before moving files |
| test-slice | Verify the extracted slice works in isolation |
| review-slice-boundaries | After extraction, review for boundary violations |
| slice-lifecycle | Part of the slice development lifecycle agent |