| 1 | 50 | ▶ | use v5.14; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 50 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 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.040 | ||||||||||||
| 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 | ▶ | use Type::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 76 | |||||||||||||
| 77 | 50 | package Attean::API::Serializer 0.040 { | |||||||||||
| 78 | 50 | ▶ | use Moo::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 79 | 50 | ▶ | use Carp qw(confess); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 80 | |||||||||||||
| 81 | 50 | requires 'canonical_media_type'; # => (is => 'ro', isa => 'Str', init_arg => undef); | |||||||||||
| 82 | 50 | requires 'media_types'; # => (is => 'ro', isa => 'ArrayRef[Str]', init_arg => undef); | |||||||||||
| 83 | 50 | requires 'handled_type'; # => (is => 'ro', isa => 'Type::Tiny', init_arg => undef); | |||||||||||
| 84 | 50 | requires 'file_extensions'; # => (is => 'ro', isa => 'ArrayRef[Str]', init_arg => undef); | |||||||||||
| 85 | |||||||||||||
| 86 | 50 | requires 'serialize_iter_to_io'; # serialize_iter_to_io($io, $iter) | |||||||||||
| 87 | 50 | requires 'serialize_iter_to_bytes'; # $data = serialize_iter_to_bytes($iter) | |||||||||||
| 88 | |||||||||||||
| 89 | 110 | ▶ | before 'serialize_iter_to_io' => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.7
| |||||||||||||
| 90 | 110 | my $self = shift; | |||||||||||
| 91 | 110 | ▶ | my $io = shift || confess "No filehandle passed to serialize_iter_to_io"; | ||||||||||
|
Condition50%1/2
shift() || confess('No filehandle passed to serialize_iter_to_io')
MC/DC0%0/1
shift() || confess('No filehandle passed to serialize_iter_to_io')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 92 | 110 | ▶ | my $iter = shift || confess "No iterator passed to serialize_iter_to_io"; | ||||||||||
|
Condition50%1/2
shift() || confess('No iterator passed to serialize_iter_to_io')
MC/DC0%0/1
shift() || confess('No iterator passed to serialize_iter_to_io')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 93 | 50 | }; | |||||||||||
| 94 | |||||||||||||
| 95 | 86 | ▶ | before 'serialize_iter_to_bytes' => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||
| 96 | 86 | my $self = shift; | |||||||||||
| 97 | 86 | ▶ | my $iter = shift || confess "No iterator passed to serialize_iter_to_bytes"; | ||||||||||
|
Condition50%1/2
shift() || confess('No iterator passed to serialize_iter_to_bytes')
MC/DC0%0/1
shift() || confess('No iterator passed to serialize_iter_to_bytes')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 98 | 50 | }; | |||||||||||
| 99 | |||||||||||||
| 100 | |||||||||||||
| 101 | 4 | ▶ | sub serialize_list_to_io { | ||||||||||
|
Subroutine
serialize_list_to_io: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 102 | 4 | my $self = shift; | |||||||||||
| 103 | 4 | my $io = shift; | |||||||||||
| 104 | 4 | my $iter = Attean::ListIterator->new( values => [@_], item_type => $self->handled_type->role ); | |||||||||||
| 105 | 4 | return $self->serialize_iter_to_io($io, $iter); | |||||||||||
| 106 | } | ||||||||||||
| 107 | |||||||||||||
| 108 | 9 | ▶ | sub serialize_list_to_bytes { | ||||||||||
|
Subroutine
serialize_list_to_bytes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 109 | 9 | my $self = shift; | |||||||||||
| 110 | 9 | my $iter = Attean::ListIterator->new( values => [@_], item_type => $self->handled_type->role ); | |||||||||||
| 111 | 9 | return $self->serialize_iter_to_bytes($iter); | |||||||||||
| 112 | } | ||||||||||||
| 113 | |||||||||||||
| 114 | 0 | ▶ | sub file_extensions { return [] } | ||||||||||
|
Subroutine
file_extensions: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 115 | } | ||||||||||||
| 116 | |||||||||||||
| 117 | 50 | package Attean::API::AbbreviatingSerializer 0.040 { | |||||||||||
| 118 | # Serializer that can make use of a base IRI and/or prefix IRI mappings | ||||||||||||
| 119 | 50 | ▶ | use Types::Standard qw(InstanceOf ConsumerOf Maybe Bool); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 120 | 50 | ▶ | use Types::Namespace qw( NamespaceMap ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 121 | |||||||||||||
| 122 | 50 | ▶ | use Moo::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 123 | |||||||||||||
| 124 | 50 | with 'Attean::API::Serializer'; | |||||||||||
| 125 | |||||||||||||
| 126 | 50 | has base => (is => 'ro', isa => ConsumerOf['Attean::API::IRI'], predicate => 'has_base'); | |||||||||||
| 127 | 50 | has namespaces => (is => 'ro', isa => Maybe[NamespaceMap], predicate => 'has_namespaces'); | |||||||||||
| 128 | 50 | has omit_base => (is => 'ro', isa => Bool, default => 0); | |||||||||||
| 129 | } | ||||||||||||
| 130 | |||||||||||||
| 131 | 50 | package Attean::API::AppendableSerializer 0.040 { | |||||||||||
| 132 | # Serializer for a format that allows multiple serialization calls to be appended and remain syntactically valid | ||||||||||||
| 133 | 50 | ▶ | use Moo::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 134 | 50 | with 'Attean::API::Serializer'; | |||||||||||
| 135 | } | ||||||||||||
| 136 | |||||||||||||
| 137 | 50 | package Attean::API::TermSerializer 0.040 { | |||||||||||
| 138 | 50 | ▶ | use Moo::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 139 | 50 | with 'Attean::API::Serializer'; | |||||||||||
| 140 | 0 | ▶ | sub handled_type { | ||||||||||
|
Subroutine
handled_type: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 141 | 0 | state $ITEM_TYPE = Type::Tiny::Role->new(role => 'Attean::API::Term'); | |||||||||||
| 142 | 0 | return $ITEM_TYPE; | |||||||||||
| 143 | } | ||||||||||||
| 144 | } | ||||||||||||
| 145 | |||||||||||||
| 146 | 50 | package Attean::API::TripleSerializer 0.040 { | |||||||||||
| 147 | 50 | ▶ | use Moo::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 148 | 50 | with 'Attean::API::Serializer'; | |||||||||||
| 149 | 8 | ▶ | sub handled_type { | ||||||||||
|
Subroutine
handled_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 150 | 8 | state $ITEM_TYPE = Type::Tiny::Role->new(role => 'Attean::API::Triple'); | |||||||||||
| 151 | 8 | return $ITEM_TYPE; | |||||||||||
| 152 | } | ||||||||||||
| 153 | } | ||||||||||||
| 154 | |||||||||||||
| 155 | 50 | package Attean::API::QuadSerializer 0.040 { | |||||||||||
| 156 | 50 | ▶ | use Moo::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 157 | 50 | with 'Attean::API::Serializer'; | |||||||||||
| 158 | |||||||||||||
| 159 | 0 | ▶ | sub handled_type { | ||||||||||
|
Subroutine
handled_type: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 160 | 0 | state $ITEM_TYPE = Type::Tiny::Role->new(role => 'Attean::API::Quad'); | |||||||||||
| 161 | 0 | return $ITEM_TYPE; | |||||||||||
| 162 | } | ||||||||||||
| 163 | } | ||||||||||||
| 164 | |||||||||||||
| 165 | 50 | package Attean::API::MixedStatementSerializer 0.040 { | |||||||||||
| 166 | 50 | ▶ | use Moo::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 167 | 50 | with 'Attean::API::Serializer'; | |||||||||||
| 168 | |||||||||||||
| 169 | 2 | ▶ | sub handled_type { | ||||||||||
|
Subroutine
handled_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 170 | 2 | state $ITEM_TYPE = Type::Tiny::Role->new(role => 'Attean::API::TripleOrQuad'); | |||||||||||
| 171 | 2 | return $ITEM_TYPE; | |||||||||||
| 172 | } | ||||||||||||
| 173 | } | ||||||||||||
| 174 | |||||||||||||
| 175 | 50 | package Attean::API::ResultSerializer 0.040 { | |||||||||||
| 176 | 50 | ▶ | use Moo::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 177 | 50 | with 'Attean::API::Serializer'; | |||||||||||
| 178 | 0 | ▶ | sub handled_type { | ||||||||||
|
Subroutine
handled_type: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 179 | 0 | state $ITEM_TYPE = Type::Tiny::Role->new(role => 'Attean::API::Result'); | |||||||||||
| 180 | 0 | return $ITEM_TYPE; | |||||||||||
| 181 | } | ||||||||||||
| 182 | |||||||||||||
| 183 | 0 | ▶ | around 'serialize_list_to_io' => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 184 | 0 | my $orig = shift; | |||||||||||
| 185 | 0 | my $self = shift; | |||||||||||
| 186 | 0 | my $io = shift; | |||||||||||
| 187 | 0 | my @vars; | |||||||||||
| 188 | 0 | ▶ | if (scalar(@_)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 189 | 0 | @vars = $_[0]->variables; | |||||||||||
| 190 | } | ||||||||||||
| 191 | |||||||||||||
| 192 | 0 | my $iter = Attean::ListIterator->new( values => [@_], item_type => $self->handled_type->role, variables => \@vars ); | |||||||||||
| 193 | 0 | return $self->serialize_iter_to_io($io, $iter); | |||||||||||
| 194 | 50 | }; | |||||||||||
| 195 | |||||||||||||
| 196 | 0 | ▶ | around 'serialize_list_to_bytes' => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 197 | 0 | my $orig = shift; | |||||||||||
| 198 | 0 | my $self = shift; | |||||||||||
| 199 | 0 | my @vars; | |||||||||||
| 200 | 0 | ▶ | if (scalar(@_)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 201 | 0 | @vars = $_[0]->variables; | |||||||||||
| 202 | } | ||||||||||||
| 203 | 0 | my $iter = Attean::ListIterator->new( values => [@_], item_type => $self->handled_type->role, variables => \@vars ); | |||||||||||
| 204 | 0 | return $self->serialize_iter_to_bytes($iter); | |||||||||||
| 205 | 50 | }; | |||||||||||
| 206 | } | ||||||||||||
| 207 | |||||||||||||
| 208 | 50 | 1; | |||||||||||
| 209 | |||||||||||||
| 210 | __END__ | ||||||||||||