Syndication/Object.pm

filter: Statementstmt 93.0% 134 / 144 Branchbran 66.6% 52 / 78 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 96.6% 29 / 30 Podpod 87.5% 14 / 16 totaltotal 85.6% 232 / 271 SCAR 39.3
CC
43
Cov
84%
set_element
10.3
init
6.4
set_attribute
6.3
CRAP 50.7
« Syndication/Content.pm ↑ Summary Syndication/Text.pm »
1 package XML::Atom::Syndication::Object;
2 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 21 ▶use base qw( Class::ErrorHandler );
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 ▶use constant XMLNS => 'http://www.w3.org/XML/1998/namespace';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 21 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use XML::Elemental;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use XML::Elemental::Util qw( process_name );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use XML::Atom::Syndication::Util qw( nodelist utf8_off );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 21 ▶use XML::Atom::Syndication::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub new {
15 330 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
16 330 my $atom = bless {}, $class;
17 330 ▶ $atom->init(@_) or return $class->error($atom->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$atom->init(@_) or return $class->error($atom->errstr) 0 330 330
18 330 $atom;
19 }
20  
21 sub init {
22 123 ▶ my $atom = shift;
Subroutine init: called CC=6 SCAR=18.6
Pod undocumented
23 123 ▶ my %param = @_ == 1 ? (Elem => $_[0]) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 123 123
24 123 $atom->set_ns(\%param);
25 123 ▶ unless ($atom->{elem} = $param{Elem}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atom->{'elem'} = $param{'Elem'}) { } 119 4 123
26 4 ▶ unless ($atom->element_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($atom->element_name) 0 4 4
27 $atom->{name} = $param{Name}
28 0 ▶ or croak('An Elem or Name parameter is required.');
Branch: 1 branch
BranchTrueFalseTotal
$atom->{"name"} = $param{"Name"} or croak("An Elem or Name parameter is required.") 0 0 0
29 }
30 4 $atom->{elem} = XML::Elemental::Element->new;
31 4 $atom->{elem}->name('{' . $atom->ns . '}' . $atom->element_name);
32 } else {
33 119 ▶ unless ($atom->element_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($atom->element_name) 70 49 119
34 70 my ($ns, $name) = process_name($atom->{elem}->name);
35 70 $atom->{name} = $name;
36 }
37 }
38 123 $atom;
39 }
40  
41 504 ▶sub ns { $_[0]->{ns} }
Subroutine ns: called CC=1 SCAR=0.0
Pod documented
42 389 ▶sub elem { $_[0]->{elem} }
Subroutine elem: called CC=1 SCAR=0.0
Pod documented
43 70 ▶sub element_name { $_[0]->{name} }
Subroutine element_name: called CC=1 SCAR=0.0
Pod documented
44  
45 sub remove {
46 1 ▶ my $atom = shift;
Subroutine remove: called CC=1 SCAR=0.0
Pod documented
47 1 _remove($atom->elem, @_);
48 }
49  
50 sub as_xml {
51 0 ▶ my $w = XML::Atom::Syndication::Writer->new;
Subroutine as_xml: not called CC=1 SCAR=6.9
Pod documented
52 0 $w->set_prefix('', $_[0]->ns);
53 0 $w->as_xml($_[0]->elem, 1);
54 }
55  
56 #--- Atom common attributes
57  
58 sub base {
59 1 ▶ @_ > 1
Subroutine base: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 1 1
60 ? $_[0]->set_attribute(XMLNS, 'base', @_[1 .. $#_])
61 : $_[0]->get_attribute(XMLNS, 'base');
62 }
63  
64 sub lang {
65 1 ▶ @_ > 1
Subroutine lang: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 1 1
66 ? $_[0]->set_attribute(XMLNS, 'lang', @_[1 .. $#_])
67 : $_[0]->get_attribute(XMLNS, 'lang');
68 }
69  
70 #--- accessors
71  
72 sub mk_accessors {
73 346 ▶ my $class = shift;
Subroutine mk_accessors: called CC=4 SCAR=13.9
Pod documented
74 346 my $type = shift;
75 21 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
76 346 foreach my $e (@_) {
77 564 my $accessor = join '::', $class, $e;
78 564 ▶ if ($type eq 'element') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'element') { } 229 335 564
elsif ($type eq 'attribute') { } 77 258 335
79 *$accessor = sub {
80 37 ▶ @_ > 1
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 4 33 37
81 ? $_[0]->set_element($_[0]->ns, $e, @_[1 .. $#_])
82 : $_[0]->get_element($_[0]->ns, $e);
83 229 };
84 } elsif ($type eq 'attribute') {
85 *$accessor = sub {
86 84 ▶ @_ > 1
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 8 76 84
87 ? $_[0]->set_attribute($_[0]->ns, $e, @_[1 .. $#_])
88 : $_[0]->get_attribute($_[0]->ns, $e);
89 77 };
90 } else { # type is the class to instaniate
91 *$accessor = sub {
92 125 ▶ @_ > 1
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 9 116 125
93 ? $_[0]->set_element($_[0]->ns, $e, @_[1 .. $#_])
94 : $_[0]->get_class($type, $_[0]->ns, $e);
95 258 };
96 }
97 }
98 }
99  
100 sub get_element {
101 33 ▶ my ($atom, $ns, $name) = @_;
Subroutine get_element: called CC=4 SCAR=15.2
Pod documented
102 my $ns_uri =
103 33 ▶ ref($ns) eq 'XML::Atom::Syndication::Namespace' ? $ns->{uri} : $ns;
Branch: 1 branch
BranchTrueFalseTotal
ref $ns eq 'XML::Atom::Syndication::Namespace' ? : 0 33 33
104 33 my @nodes = nodelist($atom, $ns_uri, $name);
105 33 ▶ return unless @nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @nodes 0 33 33
106 wantarray
107 33 ▶ ? map { utf8_off($_->text_content) } @nodes
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 33 33
108 : utf8_off($nodes[0]->text_content);
109 }
110  
111 sub get_class {
112 116 ▶ my ($atom, $class, $ns, $name) = @_;
Subroutine get_class: called CC=5 SCAR=16.4
Pod documented
113 my $ns_uri =
114 116 ▶ ref($ns) eq 'XML::Atom::Syndication::Namespace' ? $ns->{uri} : $ns;
Branch: 1 branch
BranchTrueFalseTotal
ref $ns eq 'XML::Atom::Syndication::Namespace' ? : 0 116 116
115 116 my @nodes = nodelist($atom, $ns_uri, $name);
116 116 ▶ return unless @nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @nodes 0 116 116
117 116 eval "require $class";
118 116 ▶ croak("Error creating accessor {$ns}$name: $@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 116 116
119 wantarray
120 116 ▶ ? map { $class->new(Elem => $_, Namespace => $ns_uri) } @nodes
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 10 106 116
121 : $class->new(Elem => $nodes[0], Namespace => $ns_uri);
122 }
123  
124 sub get_attribute {
125 78 ▶ my $atom = shift;
Subroutine get_attribute: called CC=4 SCAR=14.6
Pod documented
126 78 my ($val);
127 78 ▶ if (@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 78 78
elsif (@_ == 2) { } 78 0 78
128 0 my ($attr) = @_;
129 0 $val = $atom->{elem}->attributes->{"{}$attr"};
130 } elsif (@_ == 2) {
131 78 my ($ns, $attr) = @_;
132 78 ▶ $ns = '' if $atom->ns eq $ns;
Branch: 1 branch
BranchTrueFalseTotal
if $atom->ns eq $ns 76 2 78
133 78 $val = $atom->{elem}->attributes->{"{$ns}$attr"};
134 }
135 78 utf8_off($val);
136 }
137  
138 sub set_element {
139 16 ▶ my $atom = shift;
Subroutine set_element: called CC=10 SCAR=23.3
Pod documented
140 16 my ($ns, $name, $val, $attr, $add) = @_;
141 16 ▶ $add = $attr if ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 7 9 16
142 my $ns_uri =
143 16 ▶ ref($ns) eq 'XML::Atom::Syndication::Namespace' ? $ns->{uri} : $ns;
Branch: 1 branch
BranchTrueFalseTotal
ref $ns eq 'XML::Atom::Syndication::Namespace' ? : 0 16 16
144 16 ▶ unless ($add) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($add) 10 6 16
145 10 my @nodes = nodelist($atom, $ns_uri, $name);
146 10 foreach my $node (@nodes) {
147 0 ▶ _remove($node) || return $atom->error($node->errstr);
Branch: 1 branch
BranchTrueFalseTotal
_remove($node) or return $atom->error($node->errstr) 0 0 0
148 }
149 }
150 16 ▶ if (my $class = ref $val) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $class = ref $val) { } 7 9 16
elsif (defined $val) { } 9 0 9
151 7 ▶ $val = $val->elem if $class =~ /^XML::Atom::Syndication::/;
Branch: 1 branch
BranchTrueFalseTotal
if $class =~ /^XML::Atom::Syndication::/ 4 3 7
152 7 $val->parent($atom->elem);
153 7 push @{$atom->elem->contents}, $val;
154 } elsif (defined $val) {
155 9 my $elem = XML::Elemental::Element->new;
156 9 $elem->name("{$ns_uri}$name");
157 9 ▶ $elem->attributes($attr) if $attr;
Branch: 1 branch
BranchTrueFalseTotal
if $attr 0 9 9
158 9 $elem->parent($atom->elem);
159 9 push @{$atom->elem->contents}, $elem;
160 21 ▶ use XML::Elemental::Characters;
Subroutine BEGIN: called CC=1 SCAR=0.0
161 9 my $chars = XML::Elemental::Characters->new;
162 9 $chars->data($val);
163 9 $chars->parent($elem);
164 9 push @{$elem->contents}, $chars;
165 }
166 16 $val;
167 }
168  
169 sub set_attribute {
170 8 ▶ my $atom = shift;
Subroutine set_attribute: called CC=5 SCAR=18.4
Pod documented
171 8 ▶ if (@_ == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 2) { } 0 8 8
elsif (@_ == 3) { } 8 0 8
172 0 my ($attr, $val) = @_;
173 0 $atom->{elem}->attributes->{"{}$attr"} = $val;
174 } elsif (@_ == 3) {
175 8 my ($ns, $attr, $val) = @_;
176 my $ns_uri =
177 8 ▶ ref($ns) eq 'XML::Atom::Syndication::Namespace' ? $ns->{uri} : $ns;
Branch: 1 branch
BranchTrueFalseTotal
ref $ns eq 'XML::Atom::Syndication::Namespace' ? : 0 8 8
178 8 ▶ $ns_uri = '' if $atom->ns eq $ns_uri;
Branch: 1 branch
BranchTrueFalseTotal
if $atom->ns eq $ns_uri 8 0 8
179 8 $atom->{elem}->attributes->{"{$ns_uri}$attr"} = $val;
180 }
181 }
182  
183 #--- utility
184  
185 sub _remove {
186 1 ▶ my $elem = shift;
Subroutine _remove: called CC=2 SCAR=7.0
187 1 ▶ my $parent = $elem->parent
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $elem->parent or die "Element parent is not defined" 0 1 1
188 or die 'Element parent is not defined';
189 1 my @contents = grep { $elem ne $_ } @{$parent->contents};
190 1 $parent->contents(\@contents);
191 1 $elem->parent(undef);
192 1 1;
193 }
194  
195 21 our %NS_MAP = (
196 '0.3' => 'http://purl.org/atom/ns#',
197 '1.0' => 'http://www.w3.org/2005/Atom',
198 );
199 21 our %NS_VERSION = reverse %NS_MAP;
200  
201 sub set_ns {
202 330 ▶ my $atom = shift;
Subroutine set_ns: called CC=5 SCAR=16.1
Pod undocumented
203 330 my $param = shift;
204 330 ▶ if (my $ns = delete $param->{Namespace}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ns = delete $param->{'Namespace'}) { } 202 128 330
205 202 $atom->{ns} = $ns;
206 202 $atom->{version} = $NS_VERSION{$ns};
207 } else {
208 128 ▶ my $version = delete $param->{Version} || '1.0';
Condition100%2/2
delete $param->{'Version'} || '1.0'
l!l
6122
MC/DC100%1/1
delete $param->{'Version'} || '1.0'
delete $param->{'Version'}
Truth table100%2/2
A
A= delete $param->{'Version'}
Aresult
11
00
209 128 ▶ $version = '1.0' if $version == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $version == 1 125 3 128
210 128 ▶ my $ns = $NS_MAP{$version}
Branch: 1 branch
BranchTrueFalseTotal
my $ns = $XML::Atom::Syndication::Object::NS_MAP{$version} or return $atom->error("Unknown version: $version") 0 128 128
211 or return $atom->error("Unknown version: $version");
212 128 $atom->{ns} = $ns;
213 128 $atom->{version} = $version;
214 }
215 }
216  
217 21 1;
218  
219 __END__
« Syndication/Content.pm ↑ Summary Syndication/Text.pm »