FeedWriter/Base.pm

filter: Statementstmt 22.4% 26 / 116 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 28.0% 7 / 25 Podpod 100% 5 / 5 totaltotal 19.6% 38 / 193 CC 25 SCAR 27.3
CC
25
Cov
17%
_datetime_element
42.0
_validate
42.0
new
12.0
CRAP 15.3
« FeedWriter/RSS20.pm ↑ Summary FeedWriter.pm »
1 package XML::FeedWriter::Base;
2
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base qw( Class::Accessor::Fast Class::Data::Inheritable );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use DateTimeX::Web;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use XML::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 1 __PACKAGE__->mk_accessors(qw(
12 xml dtx
13 _closed _output _encoding _use_cdata
14 ));
15
16 1 __PACKAGE__->mk_classdata( _alias => {} );
17 1 __PACKAGE__->mk_classdata( _requires => {} );
18 1 __PACKAGE__->mk_classdata( _sort_order => {} );
19
20 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
21 0 my ($class, %options) = @_;
22
23 0 delete $options{version}; # this is for XML::FeedWriter only.
24
25 0 ▶ my $encoding = delete $options{encoding} || 'utf-8';
Condition0%0/2
delete $options{'encoding'} || 'utf-8'
l!l
00
MC/DC0%0/1
delete $options{'encoding'} || 'utf-8'
delete $options{'encoding'}
Truth table0%0/2
A
A= delete $options{'encoding'}
Aresult
11
00
26
27 0 my $output;
28 0 my $self = bless {
29 xml => XML::Writer->new( OUTPUT => \$output ),
30 dtx => DateTimeX::Web->new,
31 _output => \$output,
32 _encoding => $encoding,
33 _closed => 0,
34 }, $class;
35
36 0 $self->_extra_options( \%options );
37
38 0 ▶ my $modules = delete $options{modules} || {};
Condition0%0/2
delete $options{'modules'} || {}
l!l
00
MC/DC0%0/1
delete $options{'modules'} || {}
delete $options{'modules'}
Truth table0%0/2
A
A= delete $options{'modules'}
Aresult
11
00
39 0 my %channel = $self->_canonize( \%options );
40
41 0 $self->_validate( channel => \%channel );
42
43 0 $self->xml->xmlDecl( $self->_encoding );
44
45 0 $self->_root_element( $modules );
46
47 0 $self->xml->setDataMode(1);
48 0 $self->xml->setDataIndent(2);
49
50 0 $self->_channel( \%channel );
51
52 0 return $self;
53 }
54
55 0 ▶sub _extra_options {}
Subroutine _extra_options: not called CC=1 SCAR=0.0
56 0 ▶sub _root_element {}
Subroutine _root_element: not called CC=1 SCAR=0.0
57 0 ▶sub _channel {}
Subroutine _channel: not called CC=1 SCAR=0.0
58
59 0 ▶sub add_items {}
Subroutine add_items: not called CC=1 SCAR=0.0
Pod documented
60
61 0 ▶sub close {}
Subroutine close: not called CC=1 SCAR=0.0
Pod documented
62
63 0 ▶sub save {
Subroutine save: not called CC=2 SCAR=17.9
Pod documented
64 0 my ($self, $file) = @_;
65
66 0 ▶ $self->close unless $self->_closed;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_closed 0 0 0
67
68 0 open my $fh, '>', $file;
69 0 binmode $fh;
70 0 print $fh encode( $self->_encoding, $self->as_string );
71 0 CORE::close $fh;
72 }
73
74 0 ▶sub as_string { ${ shift->_output } }
Subroutine as_string: not called CC=1 SCAR=6.9
Pod documented
75
76 0 ▶sub _data_element {
Subroutine _data_element: not called CC=3 SCAR=24.8
77 0 my ($self, $key, $data) = @_;
78
79 0 ▶ if ( ref $data eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 0 0 0
elsif (ref $data eq 'HASH') { } 0 0 0
80 0 $self->xml->dataElement( $key => @{ $data } );
81 }
82 elsif ( ref $data eq 'HASH' ) {
83 0 my %attr = %{ $data };
84 0 my $value = delete $attr{value};
85 0 $self->xml->dataElement( $key => $value, %attr );
86 }
87 else {
88 0 $self->xml->dataElement( $key => $data );
89 }
90 }
91
92 0 ▶sub _cdata_element {
Subroutine _cdata_element: not called CC=2 SCAR=17.9
93 0 my ($self, $key, $data) = @_;
94
95 0 ▶ if ( $self->_use_cdata ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_use_cdata) { } 0 0 0
96 0 $self->xml->cdataElement( $key => $data );
97 }
98 else {
99 0 $self->_data_element( $key => $data );
100 }
101 }
102
103 0 ▶sub _datetime_element {
Subroutine _datetime_element: not called CC=6 SCAR=37.4
104 0 my ($self, $key, $data) = @_;
105
106 0 my $datetime;
107 0 ▶ if ( ref $data eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "ARRAY") 0 0 0
108 0 $datetime = $self->dtx->for_rss20( @{ $data } );
109 }
110 0 ▶ if ( ref $data eq 'HASH' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 0 0 0
elsif (ref $data) { } 0 0 0
elsif ($data and $data =~ /^\d+$/) { } 0 0 0
111 0 $datetime = $self->dtx->for_rss20( %{ $data } );
112 }
113 elsif ( ref $data ) {
114 0 $datetime = $self->dtx->for_rss20( $data );
115 }
116 ▶ elsif ( $data && $data =~ /^\d+$/ ) {
Condition0%0/3
$data and $data =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$data and $data =~ /^\d+$/
$data $data =~ /^\d+$/
Truth table0%0/3
A and B
A= $dataB= $data =~ /^\d+$/
ABresult
0X0
100
111
117 0 $datetime = $self->dtx->for_rss20( epoch => $data );
118 }
119 else {
120 0 $datetime = $self->dtx->for_rss20;
121 }
122
123 0 $self->_data_element( $key => $datetime );
124 }
125
126 0 ▶sub _empty_element {
Subroutine _empty_element: not called CC=1 SCAR=6.9
127 0 my ($self, $key, $hashref) = @_;
128
129 0 $self->_validate( $key => $hashref );
130 0 $self->xml->emptyTag( $key => %{ $hashref } );
131 }
132
133 0 ▶sub _element_with_children {
Subroutine _element_with_children: not called CC=2 SCAR=17.9
134 0 my ($self, $key, $children) = @_;
135
136 0 $self->_validate( $key => $children );
137
138 0 $self->xml->startTag($key);
139 0 foreach my $subkey ( $self->_sort_keys( $children ) ) {
140 0 $self->_data_element( $subkey => $children->{$subkey} );
141 }
142 0 $self->xml->endTag($key);
143 }
144
145 0 ▶sub _duplicable_elements {
Subroutine _duplicable_elements: not called CC=3 SCAR=24.8
146 0 my ($self, $key, $data) = @_;
147
148 0 ▶ if ( ref $data eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 0 0 0
149 0 foreach my $item ( @{ $data } ) {
150 0 $self->_data_element( $key => $item );
151 }
152 }
153 else {
154 0 $self->_data_element( $key => $data );
155 }
156 }
157
158 0 ▶sub _element_with_duplicable_children {
Subroutine _element_with_duplicable_children: not called CC=3 SCAR=24.8
159 0 my ($self, $key, $data, $children_name) = @_;
160 my @items = ( ref $data eq 'ARRAY' )
161 0 ▶ ? @{ $data }
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'ARRAY' ? : 0 0 0
162 : ( $data );
163
164 0 $self->xml->startTag($key);
165 0 foreach my $item ( @items ) {
166 0 $self->_data_element( $children_name => $item );
167 }
168 0 $self->xml->endTag($key);
169 }
170
171 0 ▶sub _validate {
Subroutine _validate: not called CC=6 SCAR=37.4
172 0 my ($self, $type, $hashref) = @_;
173
174 0 foreach my $req ( @{ $self->_requires->{$type} } ) {
175 0 ▶ if ( ref $req eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $req eq 'ARRAY') { } 0 0 0
176 croak "$type: $req is required"
177 0 ▶ unless defined $hashref->{$req->[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$hashref{$req->[0]} 0 0 0
178 croak "$type: $req is too long"
179 0 ▶ unless length( $hashref->{$req->[0]} ) < $req->[1];
Branch: 1 branch
BranchTrueFalseTotal
unless length $$hashref{$req->[0]} < $req->[1] 0 0 0
180 }
181 else {
182 croak "$type: $req is required"
183 0 ▶ unless defined $hashref->{$req};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hashref->{$req} 0 0 0
184 }
185 }
186 }
187 0 ▶sub _canonize {
Subroutine _canonize: not called CC=3 SCAR=24.8
188 0 my ($self, $hashref) = @_;
189
190 0 my %hash;
191 0 foreach my $key ( keys %{ $hashref } ) {
192 0 ▶ $hash{ $self->_alias->{$key} || $key } = $hashref->{$key};
Condition0%0/3
$self->_alias->{$key} || $key
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_alias->{$key} || $key
$self->_alias->{$key} $key
Truth table0%0/3
A || B
A= $self->_alias->{$key}B= $key
ABresult
1X1
011
000
193 }
194 0 return %hash;
195 }
196
197 0 ▶sub _sort_keys {
Subroutine _sort_keys: not called CC=2 SCAR=17.9
198 0 my ($self, $hashref) = @_;
199
200 0 return map { $_->{key} }
201 0 sort { $b->{order} <=> $a->{order} }
202 map { +{
203 key => $_,
204 0 ▶ order => $self->_sort_order->{$_} || 0,
Condition0%0/2
$self->_sort_order->{$_} || 0
l!l
00
MC/DC0%0/1
$self->_sort_order->{$_} || 0
$self->_sort_order->{$_}
Truth table0%0/2
A
A= $self->_sort_order->{$_}
Aresult
11
00
205 }}
206 0 keys %{ $hashref };
207 }
208
209 1 1;
210
211 __END__
« FeedWriter/RSS20.pm ↑ Summary FeedWriter.pm »