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 12 ▶sub _check_list {
Subroutine _check_list: called CC=4 SCAR=15.6
32 12 my ($self,$value) = @_;
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 89 ▶around 'list' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
46 89 my $orig = shift;
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 38 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
55 38 my $orig = shift;
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 12 ▶sub _convert_item { ## no critic(RequireArgUnpacking)
Subroutine _convert_item: called CC=7 SCAR=20.6
70 12 my ($self) = shift;
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 1 ▶sub convert {
Subroutine convert: called CC=3 SCAR=12.5
Pod documented
93 1 my ($class,@elements) = @_;
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 13 ▶sub join { ## no critic(ProhibitBuiltinHomonyms)
Subroutine join: called CC=1 SCAR=0.0
Pod documented
105 13 my ($self,$join_string) = @_;
106  
107 return CORE::join $join_string,
108 14 grep { $_ }
109 13 map { $_->serialize }
110 $self->list;
111 }
112  
113 16 ▶sub contains {
Subroutine contains: called CC=6 SCAR=17.9
Pod documented
114 16 my ($self,$compare) = @_;
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 1 ▶sub reset { ## no critic(ProhibitBuiltinHomonyms)
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
129 1 my ($self) = @_;
130  
131 1 $self->list([]);
132 1 return 1;
133 }
134  
135 0 ▶sub push { ## no critic(RequireArgUnpacking,ProhibitBuiltinHomonyms)
Subroutine push: not called CC=1 SCAR=6.9
Pod documented
136 0 my ($self) = @_;
137 0 return $self->add(@_);
138 }
139  
140 3 ▶sub remove {
Subroutine remove: called CC=9 SCAR=22.1
Pod documented
141 3 my ($self,$remove) = @_;
142  
143 3 my $list = $self->list;
144  
145 # No params: take last param
146 3 ▶ unless (defined $remove) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $remove) { } 2 1 3
147 1 return pop @{$list};
148 # Element
149 } else {
150 2 my $new_list = [];
151 2 my $old_value;
152 2 my $index = 0;
153 2 foreach my $item (@{$list}) {
154 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
155 || ($remove =~ /^\d+$/ && $index == $remove)
156 || $item->compare($remove)) {
157 2 $remove = $item;
158 } else {
159 5 CORE::push(@{$new_list},$item);
160 }
161 7 $index ++;
162 }
163 2 $self->list($new_list);
164  
165 # Return old value
166 2 ▶ return $remove
Branch: 1 branch
BranchTrueFalseTotal
if defined $remove 2 0 2
167 if defined $remove;
168 }
169 0 return;
170 }
171  
172 12 ▶sub add { ## no critic(RequireArgUnpacking)
Subroutine add: called CC=2 SCAR=6.9
Pod documented
173 12 my ($self) = shift;
174  
175 12 my $item = $self->_convert_item(@_);
176  
177 11 ▶ unless ($self->contains($item)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->contains($item)) 10 1 11
178 10 CORE::push(@{$self->list}, $item);
179 }
180  
181 11 return $item;
182 }
183  
184  
185 21 ▶sub item {
Subroutine item: called CC=5 SCAR=19.4
Pod documented
186 21 my ($self,$index) = @_;
187  
188 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
189 unless defined $index
190 && $index =~ m/^\d+$/;
191  
192 return
193 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
194 && defined $self->list->[$index]);
195  
196 21 return $self->list->[$index];
197 }
198  
199 8 __PACKAGE__->meta->make_immutable;
200  
201 8 1;
202  
203 =encoding utf8
204  
205 =head1 NAME
206  
207 Mail::Builder::List - Helper module for handling various lists
208  
209 =head1 SYNOPSIS
210  
211 use Mail::Builder;
212
213 # Create a list that accepts Mail::Builder::Address objects
214 my $list = Mail::Builder::List->new('Mail::Builder::Address');
215
216 # Add aMail::Builder::Address object
217 $list->add($address_object);
218
219 # Add an email (Unrecognized values will be passed to the constructor of
220 # the type class - Mail::Builder::Address)
221 $list->add('sasha.nein@psychonauts.org');
222
223 # Add one more email (Unrecognized values will be passed to the constructor of
224 # the type class - Mail::Builder::Address)
225 $list->add({ email => 'raz.aquato@psychonauts.org', name => 'Razputin'} );
226
227 # Remove email from list
228 $list->remove('raz.aquato@psychonauts.org');
229
230 # Remove first element in list
231 $list->remove(1);
232
233 # Reset list
234 $list->reset;
235
236 # Add email
237 $list->add('milla.vodello@psychonauts.org','Milla Vodello');
238
239 # Serialize email list
240 print $list->join(',');
241  
242 =head1 DESCRIPTION
243  
244 This is a helper module for handling various lists (e.g. recipient, attachment
245 lists). The class contains convinient array/list handling functions.
246  
247 =head1 METHODS
248  
249 =head2 Constructor
250  
251 =head3 new
252  
253 my $list = Mail::Builder::List->new(Class name);
254 OR
255 my $list = Mail::Builder::List->new({
256 type => Class name,
257 [ list => ArrayRef, ]
258 });
259  
260 This constructor takes the class name of the objects it should hold. It is
261 only possible to add objects of the given type. It is not possible to change
262 the assigned type later.
263  
264 =head3 convert
265  
266 my $list = Mail::Builder::List->convert(ArrayRef);
267  
268 Constructor that converts an array reference into a Mail::Builder::List
269 object. The list type is defined by the first element of the array.
270  
271 =head2 Public Methods
272  
273 =head3 length
274  
275 Returns the number of items in the list.
276  
277 =head3 add
278  
279 $obj->add(Object);
280 OR
281 $obj->add(Anything)
282  
283 Pushes a new item into the list. The methods either accepts an object or
284 any values. Values will be passed to the C<new> method in the
285 list type class.
286  
287 =head3 push
288  
289 Synonym for L<add>
290  
291 =head3 remove
292  
293 $obj->remove(Object)
294 OR
295 $obj->remove(Index)
296 OR
297 $obj->remove(Anything)
298 OR
299 $obj->remove()
300  
301 Removes the given element from the list. If no parameter is passed to the
302 method the last element from the list will be removed instead.
303  
304 =head3 reset
305  
306 Removes all elements from the list, leaving an empty list.
307  
308 =head3 item
309  
310 my $list_item = $obj->item(Index)
311  
312 Returns the list item with the given index.
313  
314 =head3 join
315  
316 my $list = $obj->join(String)
317  
318 Serializes all items in the list and joins them using the given string.
319  
320 =head3 contains
321  
322 $obj->contains(Object)
323 or
324 $obj->contains(Anything)
325  
326 Returns true if the given object is in the list. You can either pass an
327 object or scalar value. Uses the L<compare> method from the list type class.
328  
329 =head2 Accessors
330  
331 =head3 type
332  
333 Returns the class name which was initially passed to the constructor.
334  
335 =head3 list
336  
337 Raw list as list or array reference.
338  
339 =head1 AUTHOR
340  
341 Maroš Kollár
342 CPAN ID: MAROS
343 maros [at] k-1.com
344 http://www.k-1.com
345  
346 =cut
« Builder/TypeConstraints.pm ↑ Summary Builder/Image.pm »