Builder/List.pm

filter: Statementstmt 95.6% 88 / 92 Branchbran 76.4% 26 / 34 Conditioncond 61.1% 22 / 36 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 94.1% 16 / 17 Podpod 100% 8 / 8 totaltotal 79.7% 165 / 207 CC 33 SCAR 16.8
CC
33
Cov
84%
remove
9.1
_convert_item
7.9
item
7.0
CRAP 5.4
« Builder/TypeConstraints.pm ↑ Summary Builder/Image.pm »
1 # ============================================================================
2 package Mail::Builder::List;
3 # ============================================================================
4  
5 8 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Mail::Builder::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 our $VERSION = $Mail::Builder::VERSION;
12  
13 8 has 'type' => (
14 is => 'ro',
15 isa => 'Mail::Builder::Type::Class',
16 required => 1,
17 );
18  
19 has 'list' => (
20 is => 'rw',
21 isa => 'ArrayRef[Object]',
22 29 ▶ default => sub { return [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 8 trigger => \&_check_list,
24 traits => ['Array'],
25 handles => {
26 length => 'count',
27 #all => 'elements',
28 },
29 );
30  
31 sub _check_list {
32 12 ▶ my ($self,$value) = @_;
Subroutine _check_list: called CC=4 SCAR=15.6
33
34 12 my $type = $self->type;
35
36 12 foreach my $element (@$value) {
37 18 ▶ unless (blessed $element
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($element) and $element->isa($type)) 0 18 18
Condition33%1/3
builtin::blessed($element) and $element->isa($type)
!ll&&!rl&&r
0018
MC/DC0%0/2
builtin::blessed($element) and $element->isa($type)
builtin::blessed($element) $element->isa($type)
Truth table33%1/3
A and B
A= builtin::blessed($element)B= $element->isa($type)
ABresult
0X0
100
111
38 && $element->isa($type)) {
39 0 croak("'$value' is not a '$type'");
40 }
41 }
42 12 return;
43 }
44  
45 around 'list' => sub {
46 89 ▶ my $orig = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
47 89 my $self = shift;
48
49 89 my $result = $self->$orig(@_);
50
51 89 ▶ return wantarray ? @{$result} : $result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 30 59 89
52 8 };
53  
54 around BUILDARGS => sub {
55 38 ▶ my $orig = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
56 38 my $class = shift;
57  
58 38 ▶ if (scalar @_ == 1
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and ref $_[0] eq '') { } 1 37 38
Condition100%3/3
scalar @_ == 1 and ref $_[0] eq ""
!ll&&!rl&&r
3611
MC/DC100%2/2
scalar @_ == 1 and ref $_[0] eq ""
scalar @_ == 1 ref $_[0] eq ""
Truth table100%3/3
A and B
A= scalar @_ == 1B= ref $_[0] eq ""
ABresult
0X0
100
111
59 && ref($_[0]) eq '') {
60 1 return $class->$orig({ type => $_[0] });
61 } else {
62 37 return $class->$orig(@_);
63 }
64 8 };
65  
66  
67 8 __PACKAGE__->meta->make_immutable;
68  
69 sub _convert_item {
70 12 ▶ my ($self) = shift;
Subroutine _convert_item: called CC=7 SCAR=20.6
71
72 12 ▶ croak(qq[Params missing])
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 0 12 12
73 unless scalar @_;
74
75 12 my $type = $self->type;
76
77 12 ▶ if (blessed($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0])) { } 4 8 12
78 4 ▶ croak(qq[Invalid item added to list: Must be of $type])
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->isa($type) 1 3 4
79 unless ($_[0]->isa($type));
80 3 return $_[0];
81 } else {
82 8 my $object = $type->new(@_);
83 8 ▶ croak(qq[Could not create $type object])
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object and builtin::blessed($object) and $object->isa($type) 0 8 8
Condition33%2/6
defined $object and builtin::blessed($object) and $object->isa($type)
!ll&&!rl&&r
008
defined $object and builtin::blessed($object)
!ll&&!rl&&r
008
MC/DC0%0/3
defined $object and builtin::blessed($object) and $object->isa($type)
defined $object builtin::blessed($object) $object->isa($type)
Truth table25%1/4
A and B and C
A= defined $objectB= builtin::blessed($object)C= $object->isa($type)
ABCresult
0XX0
10X0
1100
1111
84 unless (defined $object
85 && blessed $object
86 && $object->isa($type));
87
88 8 return $object;
89 }
90 }
91  
92 sub convert {
93 1 ▶ my ($class,@elements) = @_;
Subroutine convert: called CC=3 SCAR=12.5
Pod documented
94
95 1 ▶ my $elements_ref = (scalar @elements == 1 && ref $elements[0] eq 'ARRAY') ?
Branch: 1 branch
BranchTrueFalseTotal
scalar @elements == 1 && ref $elements[0] eq 'ARRAY' ? : 1 0 1
Condition33%1/3
scalar @elements == 1 and ref $elements[0] eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
scalar @elements == 1 and ref $elements[0] eq "ARRAY"
scalar @elements == 1 ref $elements[0] eq "ARRAY"
Truth table33%1/3
A and B
A= scalar @elements == 1B= ref $elements[0] eq "ARRAY"
ABresult
0X0
100
111
96 $elements[0] : \@elements;
97
98 1 return $class->new(
99 type => ref($elements_ref->[0]),
100 list => $elements_ref,
101 );
102 }
103  
104 sub join {
105 13 ▶ my ($self,$join_string) = @_;
Subroutine join: called CC=1 SCAR=0.0
Pod documented
106
107 return CORE::join $join_string,
108 14 grep { $_ }
109 13 map { $_->serialize }
110 $self->list;
111 }
112  
113 sub contains {
114 16 ▶ my ($self,$compare) = @_;
Subroutine contains: called CC=6 SCAR=17.9
Pod documented
115
116 16 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $compare 0 16 16
117 unless (defined $compare);
118
119 16 foreach my $item ($self->list) {
120 24 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($compare) and $item == $compare 1 23 24
Condition100%3/3
builtin::blessed($compare) and $item == $compare
!ll&&!rl&&r
10131
MC/DC100%2/2
builtin::blessed($compare) and $item == $compare
builtin::blessed($compare) $item == $compare
Truth table100%3/3
A and B
A= builtin::blessed($compare)B= $item == $compare
ABresult
0X0
100
111
121 if (blessed($compare) && $item == $compare);
122 23 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $item->compare($compare) 2 21 23
123 if ($item->compare($compare));
124 }
125 13 return 0;
126 }
127  
128 sub reset {
129 1 ▶ my ($self) = @_;
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
130
131 1 $self->list([]);
132
133 1 return 1;
134 }
135  
136 sub push {
137 0 ▶ my ($self) = @_;
Subroutine push: not called CC=1 SCAR=6.9
Pod documented
138 0 return $self->add(@_);
139 }
140  
141 sub remove {
142 3 ▶ my ($self,$remove) = @_;
Subroutine remove: called CC=9 SCAR=22.1
Pod documented
143
144 3 my $list = $self->list;
145
146 # No params: take last param
147 3 ▶ unless (defined $remove) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $remove) { } 2 1 3
148 1 return pop @{$list};
149 # Element
150 } else {
151 2 my $new_list = [];
152 2 my $old_value;
153 2 my $index = 0;
154 2 foreach my $item (@{$list}) {
155 7 ▶ if (blessed($remove) && $item == $remove
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($remove) and $item == $remove or $remove =~ /^\d+$/ and $index == $remove or $item->compare($remove)) { } 2 5 7
Condition83%10/12
builtin::blessed($remove) and $item == $remove or $remove =~ /^\d+$/ and $index == $remove or $item->compare($remove)
l!l&&r!l&&!r
115
builtin::blessed($remove) and $item == $remove or $remove =~ /^\d+$/ and $index == $remove
l!l&&r!l&&!r
016
builtin::blessed($remove) and $item == $remove
!ll&&!rl&&r
340
$remove =~ /^\d+$/ and $index == $remove
!ll&&!rl&&r
511
MC/DC20%1/5
builtin::blessed($remove) and $item == $remove or $remove =~ /^\d+$/ and $index == $remove or $item->compare($remove)
builtin::blessed($remove) $item == $remove $remove =~ /^\d+$/ $index == $remove $item->compare($remove)
Truth table36%4/11
A and B or C and D or E
A= builtin::blessed($remove)B= $item == $removeC= $remove =~ /^\d+$/D= $index == $removeE= $item->compare($remove)
ABCDEresult
11XXX1
0X11X1
1011X1
0X0X11
0X1011
100X11
101011
0X0X00
0X1000
100X00
101000
156 || ($remove =~ /^\d+$/ && $index == $remove)
157 || $item->compare($remove)) {
158 2 $remove = $item;
159 } else {
160 5 CORE::push(@{$new_list},$item);
161 }
162 7 $index ++;
163 }
164 2 $self->list($new_list);
165
166 # Return old value
167 2 ▶ return $remove
Branch: 1 branch
BranchTrueFalseTotal
if defined $remove 2 0 2
168 if defined $remove;
169 }
170 0 return;
171 }
172  
173 sub add {
174 12 ▶ my ($self) = shift;
Subroutine add: called CC=2 SCAR=6.9
Pod documented
175
176 12 my $item = $self->_convert_item(@_);
177
178 11 ▶ unless ($self->contains($item)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->contains($item)) 10 1 11
179 10 CORE::push(@{$self->list}, $item);
180 }
181
182 11 return $item;
183 }
184  
185  
186 sub item {
187 21 ▶ my ($self,$index) = @_;
Subroutine item: called CC=5 SCAR=19.4
Pod documented
188
189 21 ▶ $index = 0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index and $index =~ /^\d+$/ 0 21 21
Condition33%1/3
defined $index and $index =~ /^\d+$/
!ll&&!rl&&r
0021
MC/DC0%0/2
defined $index and $index =~ /^\d+$/
defined $index $index =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $indexB= $index =~ /^\d+$/
ABresult
0X0
100
111
190 unless defined $index
191 && $index =~ m/^\d+$/;
192
193 return
194 21 ▶ unless ($index =~ m/^\d+$/
Branch: 1 branch
BranchTrueFalseTotal
unless $index =~ /^\d+$/ and defined $self->list->[$index] 0 21 21
Condition33%1/3
$index =~ /^\d+$/ and defined $self->list->[$index]
!ll&&!rl&&r
0021
MC/DC0%0/2
$index =~ /^\d+$/ and defined $self->list->[$index]
$index =~ /^\d+$/ defined $self->list->[$index]
Truth table33%1/3
A and B
A= $index =~ /^\d+$/B= defined $self->list->[$index]
ABresult
0X0
100
111
195 && defined $self->list->[$index]);
196
197 21 return $self->list->[$index];
198 }
199  
200 8 __PACKAGE__->meta->make_immutable;
201  
202 8 1;
203  
204 =encoding utf8
205  
206 =head1 NAME
207  
208 Mail::Builder::List - Helper module for handling various lists
209  
210 =head1 SYNOPSIS
211  
212 use Mail::Builder;
213
214 # Create a list that accepts Mail::Builder::Address objects
215 my $list = Mail::Builder::List->new('Mail::Builder::Address');
216
217 # Add aMail::Builder::Address object
218 $list->add($address_object);
219
220 # Add an email (Unrecognized values will be passed to the constructor of
221 # the type class - Mail::Builder::Address)
222 $list->add('sasha.nein@psychonauts.org');
223
224 # Add one more email (Unrecognized values will be passed to the constructor of
225 # the type class - Mail::Builder::Address)
226 $list->add({ email => 'raz.aquato@psychonauts.org', name => 'Razputin'} );
227
228 # Remove email from list
229 $list->remove('raz.aquato@psychonauts.org');
230
231 # Remove first element in list
232 $list->remove(1);
233
234 # Reset list
235 $list->reset;
236
237 # Add email
238 $list->add('milla.vodello@psychonauts.org','Milla Vodello');
239
240 # Serialize email list
241 print $list->join(',');
242  
243 =head1 DESCRIPTION
244  
245 This is a helper module for handling various lists (e.g. recipient, attachment
246 lists). The class contains convinient array/list handling functions.
247  
248 =head1 METHODS
249  
250 =head2 Constructor
251  
252 =head3 new
253  
254 my $list = Mail::Builder::List->new(Class name);
255 OR
256 my $list = Mail::Builder::List->new({
257 type => Class name,
258 [ list => ArrayRef, ]
259 });
260  
261 This constructor takes the class name of the objects it should hold. It is
262 only possible to add objects of the given type. It is not possible to change
263 the assigned type later.
264  
265 =head3 convert
266  
267 my $list = Mail::Builder::List->convert(ArrayRef);
268  
269 Constructor that converts an array reference into a Mail::Builder::List
270 object. The list type is defined by the first element of the array.
271  
272 =head2 Public Methods
273  
274 =head3 length
275  
276 Returns the number of items in the list.
277  
278 =head3 add
279  
280 $obj->add(Object);
281 OR
282 $obj->add(Anything)
283  
284 Pushes a new item into the list. The methods either accepts an object or
285 any values. Values will be passed to the C<new> method in the
286 list type class.
287  
288 =head3 push
289  
290 Synonym for L<add>
291  
292 =head3 remove
293  
294 $obj->remove(Object)
295 OR
296 $obj->remove(Index)
297 OR
298 $obj->remove(Anything)
299 OR
300 $obj->remove()
301  
302 Removes the given element from the list. If no parameter is passed to the
303 method the last element from the list will be removed instead.
304  
305 =head3 reset
306  
307 Removes all elements from the list, leaving an empty list.
308  
309 =head3 item
310  
311 my $list_item = $obj->item(Index)
312  
313 Returns the list item with the given index.
314  
315 =head3 join
316  
317 my $list = $obj->join(String)
318  
319 Serializes all items in the list and joins them using the given string.
320  
321 =head3 contains
322  
323 $obj->contains(Object)
324 or
325 $obj->contains(Anything)
326  
327 Returns true if the given object is in the list. You can either pass an
328 object or scalar value. Uses the L<compare> method from the list type class.
329  
330 =head2 Accessors
331  
332 =head3 type
333  
334 Returns the class name which was initially passed to the constructor.
335  
336 =head3 list
337  
338 Raw list as list or array reference.
339  
340 =head1 AUTHOR
341  
342 Maroš Kollár
343 CPAN ID: MAROS
344 maros [at] k-1.com
345 http://www.k-1.com
346  
347 =cut
« Builder/TypeConstraints.pm ↑ Summary Builder/Image.pm »