Skill v1.0.1
currentAutomated scan100/1002 files
version: "1.0.1" name: hanami-workflow description: Hanami framework workflow guidelines. Activate when working with Hanami projects, Hanami CLI, or Hanami-specific patterns. location: user
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.
Hanami Workflow
Tool Grid
| Task | Tool | Command | |
|---|---|---|---|
| Lint | StandardRB | bundle exec standardrb | |
| Test | RSpec | bundle exec rspec | |
| Console | Hanami CLI | bundle exec hanami console | |
| Server | Hanami CLI | bundle exec hanami server | |
| Generate | Hanami CLI | bundle exec hanami generate <type> | |
| DB Migrate | Hanami CLI | bundle exec hanami db migrate | |
| Routes | Hanami CLI | bundle exec hanami routes |
Slices Architecture
Hanami applications MUST be organized into isolated slices (slices/admin/, slices/api/, slices/main/).
- Each slice MUST have its own container and dependencies
- Slices SHOULD NOT directly depend on other slices
- Cross-slice communication SHOULD use explicit interfaces or events
- Shared code MUST live in
lib/orapp/(application layer)
# slices/admin/config/slice.rbmodule Adminclass Slice < Hanami::Sliceimport keys: ["persistence.rom"], from: :mainendend
Dependency Injection via Deps
Hanami uses dry-system for dependency injection. All dependencies MUST be injected, not hardcoded.
module Main::Actions::Booksclass Show < Main::Actioninclude Deps["repositories.book_repository"]def handle(request, response)book = book_repository.find(request.params[:id])response.body = book.to_jsonendendend
- MUST use
include Deps[...]for injecting dependencies - SHOULD prefer constructor injection for testability
- MUST NOT use global state or singletons directly
Entities, Relations, and Repositories (ROM)
Hanami uses ROM (Ruby Object Mapper) for persistence.
Entities
Entities MUST be simple domain objects without persistence logic.
module MyApp::Entitiesclass Book < Hanami::Entityattribute :id, Types::Integerattribute :title, Types::Stringendend
Relations
Relations MUST define the database schema mapping.
module MyApp::Relationsclass Books < Hanami::DB::Relationschema :books, infer: true doassociations { belongs_to :author; has_many :reviews }endendend
Repositories
Repositories MUST encapsulate all persistence operations.
module MyApp::Repositoriesclass BookRepository < Hanami::DB::Repodef find(id) = books.by_pk(id).onedef published = books.where { published_at <= Date.today }.to_adef create(attrs) = books.changeset(:create, attrs).commitendend
- Repositories MUST NOT expose ROM internals to actions
- Relations SHOULD use
infer: truewhen schema matches database - Changesets SHOULD be used for create/update operations
Validation with dry-validation
Input validation MUST use dry-validation contracts.
module Main::Contracts::Booksclass Create < Hanami::Action::Contractparams dorequired(:title).filled(:string, min_size?: 1)required(:author).filled(:string)optional(:published_at).maybe(:date)endrule(:title) dokey.failure("must be unique") if BookRepository.new.exists?(title: value)endendend
- Contracts MUST validate all external input
- Params SHOULD define type coercion
- Error messages MUST be user-friendly
Actions
Actions MUST be single-purpose request handlers.
module Main::Actions::Booksclass Create < Main::Actioninclude Deps["repositories.book_repository"]contract Contracts::Books::Createdef handle(request, response)result = request.paramsif result.valid?book = book_repository.create(result.to_h)response.status = 201response.body = book.to_jsonelseresponse.status = 422response.body = { errors: result.errors.to_h }.to_jsonendendendend
- Each action MUST handle exactly one request type
- Actions MUST use contracts for input validation
- Actions SHOULD delegate business logic to interactors/services
Views and Templates
Views MUST separate presentation logic from templates.
module Main::Views::Booksclass Show < Main::Viewexpose :book do |book:|BookPresenter.new(book)endendend
<%# slices/main/templates/books/show.html.erb %><article><h1><%= book.title %></h1><p>By <%= book.author %></p></article>
- Views MUST use
exposefor passing data to templates - Templates SHOULD contain minimal logic
- Presenters SHOULD format data for display
Configuration
# config/app.rbmodule MyAppclass App < Hanami::Appconfig.actions.content_security_policy[:script_src] = "'self'"config.logger.level = :infoendend# slices/api/config/slice.rbmodule Apiclass Slice < Hanami::Sliceconfig.actions.format :jsonconfig.actions.default_response_format = :jsonendend
- Sensitive config MUST use environment variables
- Per-slice config SHOULD override app defaults only when needed
Testing Patterns
# Action test - mock dependenciesRSpec.describe Main::Actions::Books::Show dosubject(:action) { described_class.new(book_repository: repository) }let(:repository) { instance_double(BookRepository) }it "returns book" doallow(repository).to receive(:find).with(1).and_return(Book.new(id: 1))expect(action.call(id: 1).status).to eq(200)endend# Repository test - real databaseRSpec.describe BookRepository, type: :database doit "finds published" dosubject.create(title: "Old", published_at: Date.today - 30)expect(subject.published.map(&:title)).to eq(["Old"])endend
- Actions MUST be tested with mocked dependencies
- Repositories SHOULD be tested against real database
Interactor/Service Pattern
module MyApp::Interactors::Booksclass Publishinclude Deps["repositories.book_repository", "events.publisher"]def call(book_id)book = book_repository.find(book_id) or return Failure(:not_found)updated = book_repository.update(book_id, published_at: Date.today)publisher.publish("book.published", book_id: book_id)Success(updated)endendend
- Services SHOULD return Result objects (Success/Failure)
- Actions SHOULD use
haltfor error responses