Attributes
An attribute defines a single value exposed through the API. Each attribute declares its type, visibility, and behavior — the adapter uses these declarations to derive contracts, request shapes, and query capabilities.
What Attributes Do
Every attribute declares:
- Attribute exposure — which model columns appear in API responses
- Type information — data type, nullability, and enum values (auto-detected from the database)
- Behavior flags — whether the attribute is writable, filterable, or sortable
- Metadata — descriptions, examples, and deprecation markers for generated exports
A Minimal Declaration
ruby
class InvoiceRepresentation < Apiwork::Representation::Base
attribute :number, filterable: true, sortable: true, writable: true
attribute :status, filterable: true
attribute :issued_on, sortable: true
attribute :total
endApiwork detects types, nullability, and enum values from the database. The adapter interprets behavior flags like filterable and sortable at runtime.
Next Steps
- Declaration — auto-detection, options reference, and batch configuration
- Custom — virtual attributes backed by methods
- Writable — controlling create and update access
- Encode & Decode — transforming values during serialization
- Inline Types — defining shapes for JSON/JSONB columns
- Metadata — descriptions, examples, deprecation, and format hints
See also
- Representation::Element — block context reference