一键导入
representers
Roar::Decorator representers that turn domain entities into JSON
菜单
Roar::Decorator representers that turn domain entities into JSON
| name | representers |
| description | Roar::Decorator representers that turn domain entities into JSON |
Each representer is a Roar::Decorator that turns a single entity (or
response DTO) into JSON / hash output for the API.
Inline example: ./prompt_log_representer.rb
Entity::PromptLog →
Representer::PromptLogTyla::Representer::*Roar::Decorator, include Roar::JSONproperty :field_name per output field, in the order the API
contract documentsgetter: lambda only for formatting (e.g. created_at →
iso8601). Not for computing values; not for conditional inclusionRepresenter::PromptLog.new(entity).to_hash # for the halt/JSON response body
Representer::PromptLog.new(entity).to_json # if you need the string
For collections, map representers — do not write a single wrapping representer:
entities.map { |e| Representer::PromptLog.new(e).to_hash }
entity.attendances.where(...) — that is a
query. Belongs in a repository, called by a service that builds a
response DTO; the representer then serializes the DTO.OpenStruct — typos silently become nil
on the wire. Use a Data.define response DTO or a real entity.Request contracts validate input AND map external API names onto domain entities
Domain-Driven Design architecture patterns and conventions for this project
Presentation layer serializes domain entities to wire formats (JSON, etc.)
Repository pattern for translating between ORM rows and domain entities
How orm/ and repositories/ split responsibility in app/infrastructure/database/
When to use Dry::Struct DTO entities vs. plain Ruby class entities in domain/entities/