configure-slice
Use this skill when configuring Hanami 2.x Slices.
Core principle: Each Slice configures its own container, settings, and providers independently of the main app.
Quick Reference
| Scenario | Approach |
|---|---|
| Configure a Slice | Edit slices/<name>/config/slice.rb |
| Define slice-level settings | Use config.settings in the Slice class |
| Register slice providers | Use register_provider in the Slice class |
| Configure auto-registration | Set config.auto_register_paths |
| Exclude paths from auto-registration | Set config.no_auto_register_paths |
| Access slice settings | target[:settings] in providers or slice.settings |
| Override main app config | Define config in the Slice that takes precedence |
| Container imports | import from: :other_slice |
| Container exports | export ["component.key"] |
Core Rules
Create the Slice configuration file:
# slices/api/config/slice.rb # frozen_string_literal: true module MyApp module Slices module Api class Slice < Hanami::Slice # Slice-level configuration end end end endDefine slice-level settings:
class Slice < Hanami::Slice config.settings do setting :api_key, constructor: Types::String setting :rate_limit, default: 100, constructor: Types::Integer end endRegister slice-specific providers:
class Slice < Hanami::Slice register_provider(:api_client) do start do client = ApiClient.new( api_key: target[:settings].api_key, base_url: "https://api.example.com" ) register("api.client", client) end end endConfigure auto-registration paths:
class Slice < Hanami::Slice config.auto_register_paths = ["app/actions", "app/views", "app/repos"] config.no_auto_register_paths = ["app/relations", "app/structs", "app/entities"] endImport components from another Slice:
class Slice < Hanami::Slice import from: :main do # Import all exported components from the main slice end endExport components for other slices:
class Slice < Hanami::Slice export ["repositories.users", "services.auth"] endOverride main app configuration for slice-specific needs:
class Slice < Hanami::Slice # Use a different database for this slice config.database_url = target[:settings].database_url endKeep configuration minimal. Only override defaults when necessary. Prefer convention over configuration.
Verify configuration after changes. Run
bundle exec hanami consoleand confirm components resolve correctly:# Verify settings are loaded MyApp::Slices::Api::Slice[:settings].api_key # => expected value # Verify a registered provider component is available MyApp::Slices::Api::Slice["api.client"] # => #<ApiClient ...> # Verify an imported component resolves MyApp::Slices::Api::Slice["repositories.users"] # => #<Repositories::Users ...>
Common Mistakes
| Mistake | Reality |
|---|---|
| "I'll duplicate main app configuration in every slice" | Slices inherit main app configuration. Only override when the slice genuinely needs different behavior. |
| "I'll forget to export components other slices depend on" | If Slice B imports from Slice A, Slice A must export those components. Verify with Slice["component.key"] in the console. |
"I'll use ENV directly instead of slice settings" |
Use config.settings for typed, validated configuration. Access via target[:settings]. |
| "I'll configure auto-registration to include ROM-managed directories" | Never auto-register relations/, structs/, or entities/. ROM manages these. |
Integration
| Related Skill | When to chain |
|---|---|
| create-slice | Configuration follows slice creation. Understand slices before configuring. |
| register-provider | Slice-specific providers are registered in slice configuration. |
| manage-settings | Slice-level settings are defined in the Slice class. |
| inject-dependencies | Configured components are injected via Deps[]. |
| create-new-slice (workflow) | Full workflow includes configuration step. |