| name | extract-slice |
| license | MIT |
| type | atomic |
| description | Extract code from Hanami app module into a dedicated slice — DO NOT change behavior, every test must pass after the move, identify bounded context, use `create-slice`, `git mv` preserving history, update namespaces App::X→X plus `Deps[...]` and route defs, run full test suite before and after, load supporting files only when needed (progressive disclosure). Covers identifying extraction boundaries, moving files while preserving history, updating imports and dependencies, and verifying the extraction. Trigger words: extract slice, extract to slice, move to slice, create slice from existing, refactor to slice, modularize.
|
| metadata | {"version":"1.0.0","user-invocable":"true"} |
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).
- 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-slice to scaffold the new slice structure.
- Move files — relocate from
app/ or slices/app/ to slices/<new_slice>/ using git mv to preserve history:
git mv app/actions/payments/ slices/payments/actions/
git mv app/operations/payments/ slices/payments/operations/
git mv app/repositories/payments/ slices/payments/repositories/
Directory mapping:
- Actions →
slices/<slice>/actions/
- Operations →
slices/<slice>/operations/
- Repositories →
slices/<slice>/repositories/
- Relations →
slices/<slice>/relations/
- Views →
slices/<slice>/views/
- Update namespaces — change module nesting to the new slice namespace. Example:
module App
module Payments
class CreateOrder < App::Operation
end
end
end
module Payments
class CreateOrder < Payments::Operation
end
end
- Update imports — any remaining code referencing the old namespace must be updated. Check:
include Deps[...] keys
require statements
- 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:
Output Style
- Extraction plan — which files move, what namespaces change.
- Before/after structure — directory tree before and after extraction.
- Import changes — every
Deps key, 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 |