Exporter/XSD.pm

filter: Statementstmt 100% 55 / 55 Branchbran 75.0% 9 / 12 Conditioncond 55.5% 5 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 3 totaltotal 84.5% 82 / 97 CC 10 SCAR 9.8
CC
10
Cov
91%
add
5.1
BUILD
3.2
commit
3.0
CRAP 2.7
« Importer/XSD.pm ↑ Summary Fix/xpath_map.pm »
1 package Catmandu::Exporter::XSD;
2  
3 1 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.05';
6  
7 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Catmandu;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Catmandu::XSD;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 with 'Catmandu::Exporter';
12  
13 1 has 'root' => (is => 'ro' , required => 1);
14 1 has 'schemas' => (is => 'ro' , required => 1);
15 3 ▶has 'mixed' => (is => 'ro' , default => sub { 'ATTRIBUTES' });
Subroutine __ANON__: called CC=1 SCAR=0.0
16 1 has 'prefixes' => (is => 'ro');
17  
18 1 has 'split' => (is => 'ro');
19 2 ▶has 'split_pattern' => (is => 'ro' , default => sub { '%s.xml' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
20 2 ▶has 'split_directory' => (is => 'ro' , default => sub { '.' });
Subroutine __ANON__: called CC=1 SCAR=0.0
21  
22 1 has 'template_before' => (is => 'ro');
23 1 has 'template' => (is => 'ro');
24 1 has 'template_after' => (is => 'ro');
25  
26 1 has 'tt' => (is => 'lazy');
27 1 has 'xsd' => (is => 'lazy');
28  
29 3 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.7
Pod undocumented
30 3 my ($self,$args) = @_;
31  
32 die "split and template can't be set at the same time"
33 3 ▶ if (exists $args->{split} && exists $args->{template});
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{"split"} and exists $args->{"template"} 0 3 3
Condition66%2/3
exists $args->{"split"} and exists $args->{"template"}
!ll&&!rl&&r
210
MC/DC0%0/2
exists $args->{"split"} and exists $args->{"template"}
exists $args->{"split"} exists $args->{"template"}
Truth table66%2/3
A and B
A= exists $args->{"split"}B= exists $args->{"template"}
ABresult
0X0
100
111
34 }
35  
36 3 ▶sub _build_xsd {
Subroutine _build_xsd: called CC=1 SCAR=0.0
37 3 my $self = $_[0];
38 3 return Catmandu::XSD->new(
39 root => $self->root ,
40 schemas => $self->schemas ,
41 mixed => $self->mixed ,
42 prefixes => $self->prefixes ,
43 );
44 }
45  
46 1 ▶sub _build_tt {
Subroutine _build_tt: called CC=2 SCAR=7.1
47 1 my $self = $_[0];
48  
49 1 ▶ if ($self->template) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->template) 1 0 1
50 1 Catmandu->exporter(
51 'Template',
52 fh => $self->fh ,
53 template_before => $self->template_before ,
54 template => $self->template ,
55 template_after => $self->template_after ,
56 );
57 }
58 }
59  
60 3 ▶sub add {
Subroutine add: called CC=5 SCAR=16.2
Pod undocumented
61 3 my ($self, $data) = @_;
62  
63 3 my $xml = $self->xsd->to_xml($data);
64  
65 3 ▶ if ($self->template) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->template) { } 1 2 3
elsif ($self->split) { } 1 1 2
66 1 $xml =~ s{<\?xml version="1.0" encoding="UTF-8"\?>}{};
67 1 $data->{xml} = $xml;
68 1 $self->tt->add($data);
69 }
70 elsif ($self->split) {
71 1 ▶ my $id = $data->{_id} // $self->count;
Condition33%1/3
$data->{"_id"} // $self->count
l!l&&r!l&&!r
010
MC/DC0%0/2
$data->{"_id"} // $self->count
$data->{"_id"} $self->count
Truth table33%1/3
A // B
A= $data->{"_id"}B= $self->count
ABresult
1X1
011
000
72 1 my $directory = $self->split_directory;
73 1 my $filename = sprintf $self->split_pattern , $id;
74 1 local(*F);
75 1 ▶ open(F,'>:encoding(utf-8)',"$directory/$filename")
Branch: 1 branch
BranchTrueFalseTotal
open F, ">:encoding(utf-8)", "$directory/$filename" or die "failed to open $directory/$filename for writing : $!" 1 0 1
76 || die "failed to open $directory/$filename for writing : $!";
77 1 print F $xml;
78 1 close(F);
79 }
80 else {
81 1 $self->fh->print($xml);
82 }
83 }
84  
85 3 ▶sub commit {
Subroutine commit: called CC=3 SCAR=11.0
Pod undocumented
86 3 my $self = $_[0];
87  
88 3 ▶ if ($self->template && $self->count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->template and $self->count) 1 2 3
Condition66%2/3
$self->template and $self->count
!ll&&!rl&&r
201
MC/DC50%1/2
$self->template and $self->count
$self->template $self->count
Truth table66%2/3
A and B
A= $self->templateB= $self->count
ABresult
0X0
100
111
89 1 $self->tt->commit;
90 }
91  
92 3 1;
93 }
94  
95 1 1;
96  
97 __END__
« Importer/XSD.pm ↑ Summary Fix/xpath_map.pm »