File Coverage

blib/lib/Kelp/Module/YAML.pm
Criterion Covered Total %
statement 20 20 100.0
branch 2 2 100.0
condition n/a
subroutine 8 8 100.0
pod 1 3 33.3
total 31 33 93.9


line stmt bran cond sub pod time code
1             package Kelp::Module::YAML;
2             $Kelp::Module::YAML::VERSION = '2.00';
3 3     3   1784516 use Kelp::Base qw(Kelp::Module::Encoder);
  3         6  
  3         17  
4 3     3   6244 use YAML::PP;
  3         67967  
  3         981  
5              
6             our $content_type = 'text/yaml';
7             our $content_type_re = qr{^text/yaml}i;
8              
9 8     8 0 3207 sub encoder_name { 'yaml' }
10              
11             sub build_encoder
12             {
13 5     5 0 296 my ($self, $args) = @_;
14              
15 5         65 return Kelp::Module::YAML::Facade->new(
16             engine => YAML::PP->new(%$args)
17             );
18             }
19              
20             sub build
21             {
22 3     3 1 335 my ($self, %args) = @_;
23              
24             require Kelp::Module::YAML::KelpExtensions
25 3 100       723 if delete $args{kelp_extensions};
26              
27 3         33 $self->SUPER::build(%args);
28              
29 3         16 $self->register(yaml => $self->get_encoder);
30             }
31              
32             package Kelp::Module::YAML::Facade {
33 3     3   26 use Kelp::Base;
  3         10  
  3         45  
34              
35             attr -engine;
36              
37             sub encode
38             {
39 5     5   25415 my $self = shift;
40 5         20 $self->engine->dump_string(@_);
41             }
42              
43             sub decode
44             {
45 11     11   35457 my $self = shift;
46 11         47 $self->engine->load_string(@_);
47             }
48             }
49             $Kelp::Module::YAML::Facade::VERSION = '2.00';
50              
51             1;
52             __END__
53              
54             =head1 NAME
55              
56             Kelp::Module::YAML - YAML encoder / decoder for Kelp
57              
58             =head1 SYNOPSIS
59              
60             # in config
61             modules => [qw(YAML)],
62             modules_init => {
63             YAML => {
64             # options for the constructor
65             },
66             },
67              
68             # in your application
69             my $encoded = $self->yaml->encode({
70             type => 'structure',
71             name => [qw(testing yaml)],
72             });
73              
74             # Kelp 2.10 encoder factory
75             my $new_yaml = $self->get_encoder(yaml => 'name');
76              
77              
78             =head1 DESCRIPTION
79              
80             This is a very straightforward module that enriches the L<Kelp> framework
81             with L<YAML> serialization. It uses L<YAML::PP> behind the scenes.
82              
83             This module is compatible with Kelp 2.10 encoders feature, so you can use it as
84             a factory for YAML processors by calling C<get_encoder> on the Kelp app object.
85             It registers itself under the name B<yaml>.
86              
87             =head1 METHODS INTRODUCED TO KELP
88              
89             =head2 yaml
90              
91             my $yaml_facade = $kelp->yaml;
92              
93             Returns the instance of an yaml facade. You can use this instance to invoke
94             the methods listed below.
95              
96             =head1 METHODS
97              
98             =head2 encode
99              
100             my $encoded = $kelp->yaml->encode($data);
101              
102             A shortcut to C<< $kelp->yaml->engine->dump_string >>
103              
104             =head2 decode
105              
106             my $decoded = $kelp->yaml->decode($yaml);
107              
108             A shortcut to C<< $kelp->yaml->engine->load_string >>
109              
110             =head2 engine
111              
112             my $yaml_engine = $kelp->yaml->engine;
113              
114             Returns the instance of L<YAML::PP>
115              
116             =head1 CONFIGURATION
117              
118             A single special flag exists, C<kelp_extensions> - if passed and true, YAML
119             extensions for Kelp modules will be installed, adding some new methods to base
120             Kelp packages:
121              
122             =over
123              
124             =item * Kelp::Request
125              
126             Adds C<is_yaml>, C<yaml_param> and C<yaml_content> methods, all working like
127             json counterparts.
128              
129             =item * Kelp::Response
130              
131             Adds C<yaml> method and an ability for C<render> to turn a reference into YAML
132             with proper content type.
133              
134             =item * Kelp::Test
135              
136             Adds C<yaml_cmp> and C<yaml_content> methods, working like their json counterparts.
137              
138             =back
139              
140             The rest of the configuration is fed to L<YAML::PP/new>.
141              
142             YAML content type for the extensions is deduced based on content of
143             C<$Kelp::Module::YAML::content_type> and
144             C<$Kelp::Module::YAML::content_type_re> variables and is C<text/yaml> by
145             default.
146              
147             =head1 CAVEATS
148              
149             While C<encode> and C<decode> methods in the facade will handle call context
150             and multiple YAML documents just fine, the installed extensions to Kelp
151             components will work in a scalar (single-document) mode. To avoid mistakes,
152             C<yaml_content> in request will return C<undef> if there is more than one
153             document in the request, since in scalar context YAML::PP will return just the
154             first one, ignoring the rest of the data.
155              
156             =head1 SEE ALSO
157              
158             =over
159              
160             =item * L<Kelp>, the framework
161              
162             =back
163              
164             =head1 AUTHOR
165              
166             Bartosz Jarzyna, E<lt>bbrtj.pro@gmail.comE<gt>
167              
168             =head1 COPYRIGHT AND LICENSE
169              
170             Copyright (C) 2024 by Bartosz Jarzyna
171              
172             This library is free software; you can redistribute it and/or modify
173             it under the same terms as Perl itself.
174              
175             =cut
176