blib/lib/Mojolicious/Plugin/Data/Transfigure.pm

filter: Statementstmt 60.0% 3 / 5 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 71.4% 5 / 7 CC 1 SCAR 1.4
CC
1
Cov
60%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Mojolicious::Plugin::Data::Transfigure 0.01;
2 1 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # ABSTRACT: Mojolicious adapter for Data::Transfigure
5  
6 0 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=2.6
7  
8 use Data::Transfigure;
9 use List::Util qw(any);
10 use Readonly;
11  
12 Readonly::Scalar my $DEFAULT_PREFIX => 'transfig';
13  
14 use experimental qw(signatures);
15  
16 sub register($self, $app, $conf) {
17 my @renderers = ($conf->{renderers} // [qw(json)])->@*;
18 my $prefix = $conf->{prefix} // $DEFAULT_PREFIX;
19 my $bare = $conf->{bare};
20  
21 # default OUTPUT transfigurator
22 my $t_out = $bare ? Data::Transfigure->bare() : Data::Transfigure->dbix();
23 $t_out->add_transfigurators(
24 qw(
25 Data::Transfigure::HashKeys::CamelCase
26 Data::Transfigure::HashKeys::CapitalizedIDSuffix
27 Data::Transfigure::HashFilter::Undef
28 Data::Transfigure::Tree::Merge
29 )
30 )
31 unless ($bare);
32  
33 # default INPUT transfigurator
34 my $t_in = $bare ? Data::Transfigure->bare() : Data::Transfigure->new();
35 $t_in->add_transfigurators(
36 qw(
37 Data::Transfigure::HashKeys::SnakeCase
38 )
39 )
40 unless ($bare);
41  
42 # helpers to provide access to default transfigurators (for adding transfigurations)
43 $app->helper("$prefix.input" => sub($c) {$t_in});
44 $app->helper("$prefix.output" => sub($c) {$t_out});
45  
46 # helper to apply transfigurator (default or custom) to request body JSON
47 $app->helper(
48 "$prefix.json" => sub($c, %args) {
49 my $lt = exists($args{transfigurator}) ? delete($args{transfigurator}) : $t_in;
50 my $data = $c->req->json;
51 return defined($lt) ? $lt->transfigure($data) : $data;
52 }
53 );
54  
55 # Render hook to apply transfigurator (default or custom) to request output
56 $app->hook(
57 before_render => sub ($c, $args) {
58 my $lt = exists($args->{transfigurator}) ? delete($args->{transfigurator}) : $t_out;
59 foreach my $k (keys($args->%*)) {
60 if (defined($lt) && any {$_ eq $k} @renderers) {
61 $args->{$k} = $lt->transfigure($args->{$k});
62 }
63 }
64 }
65 );
66 }
67  
68 =head1 NAME
69  
70 Mojolicious::Plugin::Data::Transfigure - Mojolicious adapter for Data::Transfigure
71  
72 =head1 SYNOPSIS
73  
74 # in startup
75 $app->plugin('Data::Transfigure' => {
76 renderers => [qw(json openapi)]
77 });
78  
79 $app->transfig->output->add_transfigurators(
80 Data::Transfigure::Type->new(
81 type => "App::Model::Result::Book",
82 handler => sub($data) {
83 +{
84 id => $data->id,
85 name => $data->name,
86 author => $data->author,
87 }
88 }
89 ),
90 Data::Transfigure::Type->new(
91 type => 'App::Model::Result::Person',
92 handler => sub($data) {
93 +{
94 id => $data->id,
95 firstname => $data->names->[0],
96 lastname => $data->names->[1],
97 }
98 }
99 )
100 );
101  
102 $app->transfig->input->add_transfigurators(
103 Data::Transfigure::Position->new(
104 position => '/**/author',
105 handler => sub($data) {
106 +{
107 id => $data->id,
108 names => [$data->{firstname}, $data->{lastname}]
109 }
110 }
111 )
112 );
113  
114 # in controller
115 sub get_book($self) {
116 my $book = $self->model("Book")->find($self->param('id'));
117 $self->render(json => $book);
118 }
119  
120 sub update_book($self) {
121 my $book = $self->model("Book")->find($self->param('id'));
122 my $data = $self->transfig->json;
123  
124 $book->author->update(delete($data->{author}));
125 $book->update($data);
126 $book->discard_changes;
127 $self->render(json => $book);
128 }
129  
130 =head1 DESCRIPTION
131  
132 This plugin is an adapter to make L<Data::Transfigure> a bit more convenient to
133 use in L<Mojolicious> applications. Two transfigurators are created for you:
134 one for data input, and the other for output. The default output transfigurator
135 is automatically invoked when rendering data via any of the methods configured
136 as L</renderers>. The default input transfigurator is manually invoked by
137 calling the L<transfig.json> helper rather than, e.g., C<$c-E<gt>req-E<gt>json>.
138  
139 =head1 METHODS
140  
141 L<Mojolicious::Plugin::Data::Transfigure> inherits all methods from
142 L<Mojolicious::Plugin> and implements the following new ones
143  
144 =head2 register
145  
146 Register the plugin in a Mojolicious application. Configuration via named
147 arguments:
148  
149 =head4 bare
150  
151 Configures the default input and output transfigurators to be initialized with
152 no transfigurations instead of their usual default sets.
153  
154 =head4 prefix
155  
156 Configures the prefix used for the module's Mojolicious helper functions. This
157 documentation assumes that it is left unchanged
158  
159 Default: C<transfig>
160  
161 =head4 renderers
162  
163 Controls which output rendering functions (e.g., C<text>, C<json>) are
164 intercepted and automatically transfigured before being delivered to the client
165  
166 Default: C<['json']>
167  
168 =head1 HELPERS
169  
170 =head2 transfig.input
171  
172 app->transfig->input
173  
174 Returns the default input transfigurator. Add transfigurations to it by calling
175 C<add_transfigurator()>/C<add_transfigurator_at()> on the return value.
176  
177 By default, the following transfigurators are configured, unless the L</bare>
178 configuration option is enabled:
179  
180 =over
181  
182 =item * L<Data::Transfigure::Default::ToString>
183  
184 =item * L<Data::Transfigure::HashKeys::SnakeCase>
185  
186 =back
187  
188 =head2 transfig.output
189  
190 app->transfig->output
191  
192 Returns the default output transfigurator. Add transfigurations to it by calling
193 C<add_transfigurator()>/C<add_transfigurator_at()> on the return value.
194  
195 By default, the following transfigurators are configured, unless the L</bare>
196 configuration option is enabled:
197  
198 =over
199  
200 =item * L<Data::Transfigure::Default::ToString>
201  
202 =item * L<Data::Transfigure::HashKeys::CamelCase>
203  
204 =item * L<Data::Transfigure::HashKeys::CapitalizedIDSuffix>
205  
206 =item * L<Data::Transfigure::HashFilter::Undef>
207  
208 =item * L<Data::Transfigure::Tree::Merge>
209  
210 =item * L<Data::Transfigure::Type::DBIx::Recursive>
211  
212 =back
213  
214 =head2 transfig.json
215  
216 app->transfig->json
217 app->transfig->json(transfigurator => $t)
218  
219 Returns the request body, decoded as JSON, and passed through the C<input>
220 transfigurator. L</transfig.input> is used by default, but an alternative
221 transfigurator may be passed in via the C<transfigurator> argument.
222  
223 =head1 AUTHOR
224  
225 Mark Tyrrell C<< <mark@tyrrminal.dev> >>
226  
227 =head1 LICENSE
228  
229 Copyright (c) 2024 Mark Tyrrell
230  
231 Permission is hereby granted, free of charge, to any person obtaining a copy
232 of this software and associated documentation files (the "Software"), to deal
233 in the Software without restriction, including without limitation the rights
234 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
235 copies of the Software, and to permit persons to whom the Software is
236 furnished to do so, subject to the following conditions:
237  
238 The above copyright notice and this permission notice shall be included in all
239 copies or substantial portions of the Software.
240  
241 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
242 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
243 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
244 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
245 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
246 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
247 SOFTWARE.
248  
249 =cut
250  
251 1;
252  
253 __END__
↑ Summary