Base
Base class for collection response wrappers.
Collection wrappers structure responses for index actions that return multiple records. Extend this class to customize how collections are wrapped in your API responses.
Example: Custom collection wrapper
ruby
class MyCollectionWrapper < Wrapper::Collection::Base
shape do
array(root_key.plural.to_sym) do |array|
array.reference(data_type)
end
object?(:meta)
metadata_type_names.each { |type_name| merge(type_name) }
end
def wrap
{ root_key.plural.to_sym => data, meta: meta.presence, **metadata }.compact
end
endClass Methods
.shape
.shape(klass_or_callable = nil, &block)
Defines the response shape for contract generation.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
klass_or_callable | Class<Shape>, Proc, nil | nil | A Shape subclass or callable. |
Returns
Class<Shape>, nil
Instance Methods
#data
#data
The data for this wrapper.
Returns
Hash
#meta
#meta
The meta for this wrapper.
Returns
Hash
#metadata
#metadata
The metadata for this wrapper.
Returns
Hash
#wrap
#wrap
Transforms the data into the final response format.
Returns
Hash