File Coverage

blib/lib/Attean/API/Serializer.pm
Criterion Covered Total %
statement 52 59 88.1
branch n/a
condition n/a
subroutine 18 22 81.8
pod 8 8 100.0
total 78 89 87.6


line stmt bran cond sub pod time code
1 50     50   607 use v5.14;
  50         160  
2 50     50   269 use warnings;
  50         98  
  50         1745  
3              
4             =head1 NAME
5              
6             Attean::API::Serializer - Serializer role
7              
8             =head1 VERSION
9              
10             This document describes Attean::API::Serializer version 0.032
11              
12             =head1 DESCRIPTION
13              
14             The Attean::API::Serializer role defines a common API for all serializers
15             of typed objects to data (either a byte string or printed to a filehandle).
16              
17             =head1 REQUIRED METHODS
18              
19             The following methods are required by the L<Attean::API::Serializer> role:
20              
21             =over 4
22              
23             =item C<< canonical_media_type >>
24              
25             Returns the canonical media type string for the format of this serializer.
26              
27             =item C<< media_types >>
28              
29             Returns an ARRAY reference of media type strings that also identify the format
30             produced by this serializer.
31              
32             =item C<< handled_type >>
33              
34             Returns a L<Type::Tiny> object representing the type of items that are consumed
35             during serialization.
36              
37             =item C<< file_extensions >>
38              
39             Returns an ARRAY reference of file extensions commonly associated with the
40             media types supported by the serializer (and returned by C<< media_types >>).
41             File extensions should NOT include a leading dot.
42              
43             =item C<< serialize_iter_to_io( $fh, $iterator ) >>
44              
45             Serializes the elements from the L<Attean::API::Iterator> C<< $iterator >> to
46             the L<IO::Handle> object C<< $fh >>.
47              
48             =item C<< serialize_iter_to_bytes( $fh ) >>
49              
50             Serializes the elements from the L<Attean::API::Iterator> C<< $iterator >>
51             and returns the serialization as a UTF-8 encoded byte string.
52              
53             =back
54              
55             =head1 METHODS
56              
57             This role provides default implementations of the following methods:
58              
59             =over 4
60              
61             =item C<< serialize_list_to_io( $fh, @elements ) >>
62              
63             Serializes the objects in C<< @elements >> to the L<IO::Handle> object
64             C<< $fh >>.
65              
66             =item C<< serialize_list_to_bytes( @elements ) >>
67              
68             Serializes the objects in C<< @elements >> and returns the serialization as a
69             UTF-8 encoded byte string.
70              
71             =back
72              
73             =cut
74              
75 50     50   244 use Type::Tiny;
  50         96  
  50         1543  
76              
77             use Moo::Role;
78 50     50   244 use Carp qw(confess);
  50         89  
  50         256  
79 50     50   15826
  50         120  
  50         15318  
80             requires 'canonical_media_type'; # => (is => 'ro', isa => 'Str', init_arg => undef);
81             requires 'media_types'; # => (is => 'ro', isa => 'ArrayRef[Str]', init_arg => undef);
82             requires 'handled_type'; # => (is => 'ro', isa => 'Type::Tiny', init_arg => undef);
83             requires 'file_extensions'; # => (is => 'ro', isa => 'ArrayRef[Str]', init_arg => undef);
84              
85             requires 'serialize_iter_to_io'; # serialize_iter_to_io($io, $iter)
86             requires 'serialize_iter_to_bytes'; # $data = serialize_iter_to_bytes($iter)
87              
88             before 'serialize_iter_to_io' => sub {
89             my $self = shift;
90             my $io = shift || confess "No filehandle passed to serialize_iter_to_io";
91             my $iter = shift || confess "No iterator passed to serialize_iter_to_io";
92             };
93              
94             before 'serialize_iter_to_bytes' => sub {
95             my $self = shift;
96             my $iter = shift || confess "No iterator passed to serialize_iter_to_bytes";
97             };
98              
99              
100             my $self = shift;
101             my $io = shift;
102 4     4 1 467 my $iter = Attean::ListIterator->new( values => [@_], item_type => $self->handled_type->role );
103 4         9 return $self->serialize_iter_to_io($io, $iter);
104 4         24 }
105 4         423  
106             my $self = shift;
107             my $iter = Attean::ListIterator->new( values => [@_], item_type => $self->handled_type->role );
108             return $self->serialize_iter_to_bytes($iter);
109 8     8 1 1839 }
110 8         59
111 8         979 }
112              
113             # Serializer that can make use of a base IRI and/or prefix IRI mappings
114 0     0 1 0 use Types::Standard qw(InstanceOf ConsumerOf Maybe Bool);
115             use Types::Namespace qw( NamespaceMap );
116              
117             use Moo::Role;
118              
119 50     50   392 with 'Attean::API::Serializer';
  50         116  
  50         367  
120 50     50   59937  
  50         6775811  
  50         397  
121             has base => (is => 'ro', isa => ConsumerOf['Attean::API::IRI'], predicate => 'has_base');
122 50     50   17502 has namespaces => (is => 'ro', isa => Maybe[NamespaceMap], predicate => 'has_namespaces');
  50         112  
  50         332  
123             has omit_base => (is => 'ro', isa => Bool, default => 0);
124             }
125              
126             # Serializer for a format that allows multiple serialization calls to be appended and remain syntactically valid
127             use Moo::Role;
128             with 'Attean::API::Serializer';
129             }
130              
131             use Moo::Role;
132             with 'Attean::API::Serializer';
133 50     50   23432 state $ITEM_TYPE = Type::Tiny::Role->new(role => 'Attean::API::Term');
  50         151  
  50         205  
134             return $ITEM_TYPE;
135             }
136             }
137              
138 50     50   16073 use Moo::Role;
  50         125  
  50         179  
139             with 'Attean::API::Serializer';
140             state $ITEM_TYPE = Type::Tiny::Role->new(role => 'Attean::API::Triple');
141 0     0 1 0 return $ITEM_TYPE;
142 0         0 }
143             }
144              
145             use Moo::Role;
146             with 'Attean::API::Serializer';
147 50     50   18253
  50         133  
  50         219  
148             state $ITEM_TYPE = Type::Tiny::Role->new(role => 'Attean::API::Quad');
149             return $ITEM_TYPE;
150 8     8 1 393 }
151 8         501 }
152              
153             use Moo::Role;
154             with 'Attean::API::Serializer';
155            
156 50     50   19320 state $ITEM_TYPE = Type::Tiny::Role->new(role => 'Attean::API::TripleOrQuad');
  50         122  
  50         197  
157             return $ITEM_TYPE;
158             }
159             }
160 0     0 1 0  
161 0         0 use Moo::Role;
162             with 'Attean::API::Serializer';
163             state $ITEM_TYPE = Type::Tiny::Role->new(role => 'Attean::API::Result');
164             return $ITEM_TYPE;
165             }
166 50     50   19346
  50         128  
  50         249  
167             around 'serialize_list_to_io' => sub {
168             my $orig = shift;
169             my $self = shift;
170 2     2 1 7 my $io = shift;
171 2         128 my @vars;
172             if (scalar(@_)) {
173             @vars = $_[0]->variables;
174             }
175            
176 50     50   17954 my $iter = Attean::ListIterator->new( values => [@_], item_type => $self->handled_type->role, variables => \@vars );
  50         127  
  50         199  
177             return $self->serialize_iter_to_io($io, $iter);
178             };
179 0     0 1  
180 0           around 'serialize_list_to_bytes' => sub {
181             my $orig = shift;
182             my $self = shift;
183             my @vars;
184             if (scalar(@_)) {
185             @vars = $_[0]->variables;
186             }
187             my $iter = Attean::ListIterator->new( values => [@_], item_type => $self->handled_type->role, variables => \@vars );
188             return $self->serialize_iter_to_bytes($iter);
189             };
190             }
191              
192             1;
193              
194              
195             =head1 BUGS
196              
197             Please report any bugs or feature requests to through the GitHub web interface
198             at L<https://github.com/kasei/attean/issues>.
199              
200             =head1 SEE ALSO
201              
202              
203              
204             =head1 AUTHOR
205              
206             Gregory Todd Williams C<< <gwilliams@cpan.org> >>
207              
208             =head1 COPYRIGHT
209              
210             Copyright (c) 2014--2022 Gregory Todd Williams.
211             This program is free software; you can redistribute it and/or modify it under
212             the same terms as Perl itself.
213              
214             =cut