Serialize/Role/Default.pm

filter: Statementstmt 100% 69 / 69 Branchbran 75.0% 18 / 24 Conditioncond 62.5% 5 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 15 / 15 Podpod 25.0% 1 / 4 totaltotal 88.0% 110 / 125 CC 23 SCAR 26.4
CC
23
Cov
91%
__ANON__
22.9
send
2.1
BEGIN
1.0
CRAP 14.1
↑ Summary Serialize/Role/Sink/Stream.pm »
1 package Data::Record::Serialize::Role::Default;
2  
3 # ABSTRACT: Default methods for Data::Record::Serialize
4  
5 21 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 21 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 21 our $VERSION = '2.05';
9  
10 21 ▶use Hash::Util qw[ hv_store ];
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use Ref::Util qw[ is_coderef ];
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 21 ▶use Data::Record::Serialize::Error { errors => ['fields'] }, -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 21 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37 ## no critic( Subroutines::ProhibitBuiltinHomonyms )
38  
39 # provide default if not already defined
40 63 ▶sub send {
Subroutine send: called CC=2 SCAR=7.2
Pod documented
41 63 my $self = shift;
42  
43 63 ▶ $self->_needs_eol
Branch: 1 branch
BranchTrueFalseTotal
$self->_needs_eol ? : 63 0 63
44 ? $self->say( $self->encode( @_ ) )
45 : $self->print( $self->encode( @_ ) );
46 }
47  
48 # just in case they're not defined in preceding roles
49 54 ▶sub setup { }
Subroutine setup: called CC=1 SCAR=0.0
Pod undocumented
50 90 ▶sub finalize { }
Subroutine finalize: called CC=1 SCAR=0.0
Pod undocumented
51 198 ▶sub _map_types { }
Subroutine _map_types: called CC=1 SCAR=0.0
52 5 ▶sub _needs_eol { 1 }
Subroutine _needs_eol: called CC=1 SCAR=0.0
53  
54 82 ▶around 'setup' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
55 82 my ( $orig, $self, $data ) = @_;
56  
57 82 $self->setup_from_record( $data );
58  
59 81 $orig->( $self );
60  
61 81 $self->_set__run_setup( 0 );
62 21 };
63  
64 93 ▶before 'close' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
65 93 my ( $self, @args ) = @_;
66 93 $self->finalize( @args );
67 21 };
68  
69 107 ▶before 'send' => sub {
Subroutine __ANON__: called CC=22 SCAR=31.3
70 107 my ( $self, $data ) = @_;
71  
72 # can't do format or numify until we have types, which might need to
73 # be done from the data, which will be done in setup.
74  
75 107 ▶ $self->setup( $data )
Branch: 1 branch
BranchTrueFalseTotal
if $self->_run_setup 82 25 107
76 if $self->_run_setup;
77  
78 # remove fields that won't be output
79 106 delete @{$data}{ grep { !exists $self->_fieldh->{$_} } keys %{$data} };
80  
81 # nullify fields (set to undef) those that are zero length
82  
83 106 ▶ if ( defined( my $fields = $self->_nullified ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fields = $self->_nullified)) 105 0 105
84 105 ▶ $data->{$_} = undef for grep { defined $data->{$_} && !length $data->{$_} } @$fields;
Condition100%3/3
defined $data->{$_} && !length($data->{$_})
!ll&&!rl&&r
2157
MC/DC100%2/2
defined $data->{$_} && !length($data->{$_})
defined $data->{$_} !length($data->{$_})
Truth table100%3/3
A && not(B)
A= defined $data->{$_}B= !length($data->{$_})
ABresult
0X0
100
111
85 }
86  
87 105 ▶ if ( defined( my $fields = $self->_numified ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fields = $self->_numified)) 104 0 104
88 104 ▶ $data->{$_} = ( $data->{$_} || 0 ) +0 for grep { defined $data->{$_} } @{$fields};
Condition50%1/2
$data->{$_} || 0
l!l
220
MC/DC0%0/1
$data->{$_} || 0
$data->{$_}
Truth table50%1/2
A
A= $data->{$_}
Aresult
11
00
89 }
90  
91 104 ▶ if ( defined( my $fields = $self->_stringified ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fields = $self->_stringified)) 103 0 103
92 103 $data->{$_} = "@{[ $data->{$_}]}" for grep { defined $data->{$_} } @{$fields};
93 }
94  
95 103 ▶ if ( my $format = $self->_format ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $format = $self->_format) 4 99 103
96 $data->{$_}
97 = is_coderef( $format->{$_} )
98 ? $format->{$_}( $data->{$_} )
99 : sprintf( $format->{$_}, $data->{$_} )
100 4 ▶ foreach grep { defined $data->{$_} && length $data->{$_} }
Condition33%1/3
defined $data->{$_} && length $data->{$_}
!ll&&!rl&&r
0013
MC/DC0%0/2
defined $data->{$_} && length $data->{$_}
defined $data->{$_} length $data->{$_}
Truth table33%1/3
A && B
A= defined $data->{$_}B= length $data->{$_}
ABresult
0X0
100
111
101 4 ▶ keys %{$format};
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_coderef($format->{$_}) ? : 5 8 13
102 }
103  
104  
105 # handle boolean
106 103 ▶ if ( $self->_boolify ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_boolify) 103 0 103
107 103 my @fields = grep { exists $data->{$_} } @{ $self->boolean_fields };
108  
109 103 ▶ if ( $self->_can_bool ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_can_bool) { } 32 71 103
110 32 $data->{$_} = $self->to_bool( $data->{$_} ) for @fields;
111 }
112  
113 # the encoder doesn't have native boolean, must convert a
114 # truthy value to 0/1;
115 else {
116 71 ▶ $data->{$_} = $data->{$_} ? 1 : 0 foreach @fields;
Branch: 1 branch
BranchTrueFalseTotal
$data->{$_} ? : 3 4 7
117 }
118 }
119  
120 103 ▶ if ( my $rename = $self->rename_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rename = $self->rename_fields) 103 0 103
121 103 for my $from ( @{ $self->fields } ) {
122 218 ▶ my $to = $rename->{$from}
Branch: 1 branch
BranchTrueFalseTotal
my $to = $rename->{$from} or next 2 216 218
123 or next;
124  
125 2 hv_store( %$data, $to, $data->{$from} );
126 2 delete $data->{$from};
127 }
128 }
129 21 };
130  
131 86 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=1 SCAR=0.0
Pod undocumented
132 86 my ( $self, $in_global_destruction ) = @_;
133  
134 # we can't make the decision about whether to pay attention during
135 # Global Destruction. the objects have to do that
136 86 $self->close( $in_global_destruction );
137 86 return;
138 }
139  
140 21 1;
141  
142 #
143 # This file is part of Data-Record-Serialize
144 #
145 # This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
146 #
147 # This is free software, licensed under:
148 #
149 # The GNU General Public License, Version 3, June 2007
150 #
151  
152 __END__
↑ Summary Serialize/Role/Sink/Stream.pm »