Mojolicious-Plugin-Data-Transfigure
view release on metacpan or search on metacpan
# NAME
Mojolicious::Plugin::Data::Transfigure - Mojolicious adapter for Data::Transfigure
# SYNOPSIS
# in startup
$app->plugin('Data::Transfigure' => {
renderers => [qw(json openapi)]
});
$app->transfig->output->add_transfigurators(
Data::Transfigure::Type->new(
type => "App::Model::Result::Book",
handler => sub($data) {
+{
id => $data->id,
name => $data->name,
author => $data->author,
}
}
),
Data::Transfigure::Type->new(
type => 'App::Model::Result::Person',
handler => sub($data) {
+{
id => $data->id,
firstname => $data->names->[0],
lastname => $data->names->[1],
}
}
)
);
$app->transfig->input->add_transfigurators(
Data::Transfigure::Position->new(
position => '/**/author',
handler => sub($data) {
+{
id => $data->id,
names => [$data->{firstname}, $data->{lastname}]
}
}
)
);
# in controller
sub get_book($self) {
my $book = $self->model("Book")->find($self->param('id'));
$self->render(json => $book);
}
sub update_book($self) {
my $book = $self->model("Book")->find($self->param('id'));
my $data = $self->transfig->json;
$book->author->update(delete($data->{author}));
$book->update($data);
$book->discard_changes;
$self->render(json => $book);
}
# DESCRIPTION
This plugin is an adapter to make [Data::Transfigure](https://metacpan.org/pod/Data%3A%3ATransfigure) a bit more convenient to
use in [Mojolicious](https://metacpan.org/pod/Mojolicious) applications. Two transfigurators are created for you:
one for data input, and the other for output. The default output transfigurator
is automatically invoked when rendering data via any of the methods configured
as ["renderers"](#renderers). The default input transfigurator is manually invoked by
calling the [transfig.json](https://metacpan.org/pod/transfig.json) helper rather than, e.g., `$c->req->json`.
# METHODS
[Mojolicious::Plugin::Data::Transfigure](https://metacpan.org/pod/Mojolicious%3A%3APlugin%3A%3AData%3A%3ATransfigure) inherits all methods from
[Mojolicious::Plugin](https://metacpan.org/pod/Mojolicious%3A%3APlugin) and implements the following new ones
## register
Register the plugin in a Mojolicious application. Configuration via named
arguments:
#### bare
Configures the default input and output transfigurators to be initialized with
no transfigurations instead of their usual default sets.
#### prefix
Configures the prefix used for the module's Mojolicious helper functions. This
documentation assumes that it is left unchanged
Default: `transfig`
#### renderers
Controls which output rendering functions (e.g., `text`, `json`) are
intercepted and automatically transfigured before being delivered to the client
Default: `['json']`
# HELPERS
## transfig.input
app->transfig->input
Returns the default input transfigurator. Add transfigurations to it by calling
`add_transfigurator()`/`add_transfigurator_at()` on the return value.
By default, the following transfigurators are configured, unless the ["bare"](#bare)
configuration option is enabled:
- [Data::Transfigure::Default::ToString](https://metacpan.org/pod/Data%3A%3ATransfigure%3A%3ADefault%3A%3AToString)
( run in 1.273 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )