blib/lib/Array/To/Moose.pm

filter: Statementstmt 100% 153 / 153 Branchbran 95.3% 103 / 108 Conditioncond 95.2% 20 / 21 MC/DCmcdc 92.8% 13 / 14 Subroutinesub 100% 20 / 20 Podpod 100% 5 / 5 totaltotal 97.8% 314 / 321 CC 64 SCAR 24.4
CC
64
Cov
98%
_check_descriptor
23.0
_check_subobj
12.0
array_to_moose
12.0
CRAP 11.5
↑ Summary
1 package Array::To::Moose;
2  
3 # Copyright (c) Stanford University. June 6th, 2010.
4 # All rights reserved.
5 # Author: Sam Brain <samb@stanford.edu>
6 # This library is free software; you can redistribute it and/or modify
7 # it under the same terms as Perl itself, either Perl version 5.8.8 or,
8 # at your option, any later version of Perl 5 you may have available.
9 #
10  
11 22 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 22 require Exporter;
16 22 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 22 our %EXPORT_TAGS = (
19 'ALL' => [ qw( array_to_moose
20 throw_nonunique_keys throw_multiple_rows
21 set_class_ind set_key_ind ) ],
22 'TESTING' => [ qw( _check_descriptor _check_subobj
23 _check_ref_attribs _check_non_ref_attribs ) ],
24 );
25  
26 22 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'ALL'} }, @{ $EXPORT_TAGS{'TESTING'} } );
27  
28 22 our @EXPORT = qw( array_to_moose
29  
30 );
31  
32 22 ▶use version; our $VERSION = qv('0.0.9');
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # BEGIN { $Exporter::Verbose=1 };
35  
36 #BEGIN { print "Got Array::To:Moose Module\n" }
37  
38 22 ▶use Params::Validate::Array qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 22 ▶use Array::GroupBy qw(igroup_by str_row_equal);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 22 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 22 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 22 $Carp::Verbose = 1;
44  
45 22 $Data::Dumper::Terse = 1;
46 22 $Data::Dumper::Indent = 1;
47  
48 # strings for "key => ..." and "class => ..." indicators
49 22 my ($KEY, $CLASS);
50  
51 22 ▶BEGIN { $KEY = 'key' ; $CLASS = 'class' }
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 # throw error if a HashRef[] key found to be non-unique
54 22 my $throw_nonunique_keys;
55  
56 # throw error if there are multiple candidate rows for an attribute
57 # which is a single object, "isa => 'MyObject'"
58 22 my $throw_multiple_rows;
59  
60 ############################################
61 # Set the indicators for "key => ..." and "class => ..."
62 # If there is no arg, reset them back to the default 'key' and 'class'
63 ############################################
64 6 ▶sub set_key_ind {
Subroutine set_key_ind: called CC=4 SCAR=13.9
Pod documented
65 6 ▶ croak "set_key_ind('$_[0]') not a legal identifier"
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and not $_[0] =~ /^\w+$/ 1 5 6
Condition100%3/3
defined $_[0] and not $_[0] =~ /^\w+$/
!ll&&!rl&&r
231
MC/DC100%2/2
defined $_[0] and not $_[0] =~ /^\w+$/
defined $_[0] not $_[0] =~ /^\w+$/
Truth table100%3/3
A and not(B)
A= defined $_[0]B= not $_[0] =~ /^\w+$/
ABresult
0X0
100
111
66 if defined $_[0] and $_[0] !~ /^\w+$/;
67  
68 5 ▶ $KEY = defined $_[0] ? $_[0] : 'key';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 3 2 5
69 }
70  
71 ############################################
72 9 ▶sub set_class_ind {
Subroutine set_class_ind: called CC=4 SCAR=13.9
Pod documented
73 9 ▶ croak "set_class_ind('$_[0]') not a legal identifier"
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and not $_[0] =~ /^\w+$/ 1 8 9
Condition100%3/3
defined $_[0] and not $_[0] =~ /^\w+$/
!ll&&!rl&&r
441
MC/DC100%2/2
defined $_[0] and not $_[0] =~ /^\w+$/
defined $_[0] not $_[0] =~ /^\w+$/
Truth table100%3/3
A and not(B)
A= defined $_[0]B= not $_[0] =~ /^\w+$/
ABresult
0X0
100
111
74 if defined $_[0] and $_[0] !~ /^\w+$/;
75  
76 8 ▶ $CLASS = defined $_[0] ? $_[0] : 'class';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 4 4 8
77 }
78  
79 ########################################
80 # throw error if non-unique keys in a HashRef['] is causing already-constructed
81 # Moose objects to be overwritten
82 # throw_nonunique_keys() to set, throw_nonunique_keys(0) to unset
83 ########################################
84 4 ▶sub throw_nonunique_keys { $throw_nonunique_keys = defined $_[0] ? $_[0] : 1 }
Subroutine throw_nonunique_keys: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 2 2 4
85  
86 ########################################
87 # throw error if a single object attribute has multiple data rows
88 # throw_multiple_rows() to set throw_multiple_rows(0) to unset
89 ########################################
90 6 ▶sub throw_multiple_rows { $throw_multiple_rows = defined $_[0] ? $_[0] : 1 }
Subroutine throw_multiple_rows: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 3 3 6
91  
92 ##########
93 # Usage
94 # my $moose_object_ref = array_to_moose( data => $array_ref,
95 # desc => { ... },
96 # );
97 ############################################
98 149 ▶sub array_to_moose {
Subroutine array_to_moose: called CC=12 SCAR=24.8
Pod documented
99 149 my ($data, $desc) = validate(@_,
100 [ data => { type => ARRAYREF },
101 desc => { type => HASHREF },
102 ]
103 );
104  
105 149 ▶ croak "'data => ...' isn't a 2D array (AoA)"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->[0] 2 147 149
106 unless ref($data->[0]);
107  
108 147 ▶ croak 'empty descriptor'
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$desc 1 146 147
109 unless keys %$desc;
110  
111 #print "data ", Dumper($data), "\ndesc ", Dumper($desc);
112  
113  
114 146 my $result = []; # returned result is either an array or a hash of objects
115  
116 # extract column of possible hash key
117 146 my $keycol;
118  
119 146 ▶ if (exists $desc->{$KEY}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $desc->{$KEY}) 37 109 146
120 37 $keycol = $desc->{$KEY};
121  
122 37 $result = {}; # returning a hashref
123  
124 }
125  
126 # _check_descriptor returns:
127 # $class, the class of the object
128 # $attribs, a hashref (attrib => column_number) of "simple" attributes
129 # (column numbers only)
130 # $ref_attribs, a hashref of attribute/column number values for
131 # non-simple attributes, currently limited to "ArrayRef[`a]",
132 # where `a is e.g 'Str', etc (i.e. `a is not a class)
133 # $sub_desc, a hashref of sub-objects.
134 # the keys are the attrib. names, the values the
135 # descriptors of the next level down
136  
137 146 my ($class, $attribs, $ref_attribs, $sub_obj_desc) =
138 _check_descriptor($data, $desc);
139  
140 #print "data ", Dumper($data), "\nattrib = ", Dumper($attribs),
141 # "\nargs = ", Dumper([ values %$attribs ]);
142  
143 #print "\$ref_attribs ", Dumper($ref_attribs); exit;
144  
145 134 my $iter = igroup_by(
146 data => $data,
147 compare => \&str_row_equal,
148 args => [ values %$attribs ],
149 );
150  
151 134 ▶ while (my $subset = $iter->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subset = &$iter()) 265 114 379
152  
153 #print "subset: ", Dumper($subset), "\n";
154  
155 #print "before 1: attrib ", Dumper($attribs), "\ndata ", Dumper($subset);
156  
157 # change attribs from col numbers to values:
158 # from: { name => 1, sex => 2, ... }
159 # to { name => 'Smith, J.', sex => 'male', ... }
160 265 my %attribs = map { $_ => $subset->[0]->[$attribs->{$_}] } keys %$attribs;
161
162  
163 # print "after 1: attrib ", Dumper(\%attribs), "\n";
164  
165 # add the 'simple ArrayRef' sub-objects
166 # (there should really be only one of these - test for it?)
167 265 ▶ while (my($attr_name, $col) = each %$ref_attribs) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$ref_attribs) 3 265 268
168 3 my @col = map { $_->[$col] } @$subset;
169 3 $attribs{$attr_name} = \@col;
170  
171 # ... or ...
172 #$attribs{$attr_name} = [ map { $_->[$col] } @$subset ];
173 }
174  
175 # print "after 2: attrib ", Dumper(\%attribs), "\n";
176  
177 # sub-objects - recursive call to array_to_moose()
178 265 ▶ while( my($attr_name, $desc) = each %$sub_obj_desc) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$sub_obj_desc) 85 248 333
179  
180 85 ▶ my $type = $class->meta->find_attribute_by_name($attr_name)->type_constraint
Branch: 1 branch
BranchTrueFalseTotal
my $type = $class->meta->find_attribute_by_name($attr_name)->type_constraint or croak("Moose attribute '${attr_name}' has no type") 84 1 85
181 or croak "Moose attribute '$attr_name' has no type";
182  
183 #print "'$attr_name' has type '$type'";
184  
185 84 my $sub_obj = array_to_moose( data => $subset,
186 desc => $desc,
187 );
188  
189 82 $sub_obj = _check_subobj($class, $attr_name, $type, $sub_obj);
190  
191 #print "type $type\n";
192  
193 68 $attribs{$attr_name} = $sub_obj;
194 }
195  
196 # print "after 2: attrib ", Dumper(\%attribs), "\n";
197  
198 248 my $obj;
199 248 eval { $obj = $class->meta->new_object(%attribs) };
200 248 ▶ croak "Can't make a new '$class' object:\n$@\n"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 247 248
201 if $@;
202  
203 247 ▶ if (defined $keycol) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $keycol) { } 74 173 247
204 74 my $key_name = $subset->[0]->[$keycol];
205  
206 # optionally croak if we are overwriting an existing hash entry
207 croak "Non-unique key '$key_name' in '", $desc->{$CLASS}, "' class"
208 74 ▶ if exists $result->{$key_name} and $throw_nonunique_keys;
Branch: 1 branch
BranchTrueFalseTotal
if exists $result->{$key_name} and $throw_nonunique_keys 2 72 74
Condition100%3/3
exists $result->{$key_name} and $throw_nonunique_keys
!ll&&!rl&&r
6842
MC/DC100%2/2
exists $result->{$key_name} and $throw_nonunique_keys
exists $result->{$key_name} $throw_nonunique_keys
Truth table100%3/3
A and B
A= exists $result->{$key_name}B= $throw_nonunique_keys
ABresult
0X0
100
111
209  
210 72 $result->{$key_name} = $obj;
211 } else {
212 173 push @{$result}, $obj;
213 }
214 }
215 114 return $result;
216 }
217  
218 ############################################
219 # Usage: my ($class, $attribs, $ref_attribs, $sub_desc)
220 # = _check_descriptor($data, $desc)
221 #
222 # Check the correctness of the descriptor hashref, $desc.
223 #
224 # Checks of descriptor $desc include:
225 # 1. "class => 'MyClass'" line exists, and that class "MyClass" has
226 # been defined
227 # 2. for "attrib => N"
228 # or "key => N" lines, N, the column number, is an integer, and that
229 # the column numbers is within limits of the data
230 # 3. For "attrib => [N]", (note square brackets), N, the columnn number,
231 # is within limits of the data
232 #
233 # Returns:
234 # $class, the class name,
235 # $attribs, hashref (name => column_index) of "simple" attributes
236 # $ref_attribs hashref (name => column_index) of attribs which are
237 # ArrayRef[']s of simple types (i.e. not a Class)
238 # (HashRef[']s not implemented)
239 # $sub_desc hashref (name => desc) of sub-object descriptors
240 ############################################
241 177 ▶sub _check_descriptor {
Subroutine _check_descriptor: called CC=23 SCAR=31.4
242 177 my ($data, $desc) = @_;
243  
244 # remove from production!
245 177 ▶ croak "_check_descriptor() needs two arguments"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 177 177
246 unless @_ == 2;
247  
248 177 ▶ my $class = $desc->{$CLASS}
Branch: 1 branch
BranchTrueFalseTotal
my $class = $desc->{$CLASS} or croak("No class descriptor '$CLASS => ...' in descriptor:\n", Dumper($desc)) 172 5 177
249 or croak "No class descriptor '$CLASS => ...' in descriptor:\n",
250 Dumper($desc);
251  
252 172 my $meta;
253  
254 # see other example of getting meta in Moose::Manual::???
255 172 eval{ $meta = $class->meta };
256 172 ▶ croak "Class '$class' not defined: $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 171 172
257 if $@;
258  
259 171 my $ncols = @{ $data->[0] };
260  
261 # separate out simple (i.e. non-reference) attributes, reference
262 # attributes, and sub-objects
263 171 my ($attrib, $ref_attrib, $sub_desc);
264  
265 171 ▶ while ( my ($name, $value) = each %$desc) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$desc) 607 150 757
266  
267 # check lines which have 'simple' column numbers ( attrib or key => N)
268 607 ▶ unless (ref($value) or $name eq $CLASS) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value or $name eq $CLASS) 382 225 607
Condition100%3/3
ref $value or $name eq $CLASS
l!l&&r!l&&!r
64161382
MC/DC100%2/2
ref $value or $name eq $CLASS
ref $value $name eq $CLASS
Truth table100%3/3
A or B
A= ref $valueB= $name eq $CLASS
ABresult
1X1
011
000
269  
270 382 my $msg = "attribute '$name => $value'";
271  
272 382 ▶ croak "$msg must be a (non-negative) integer"
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^\d+$/ 3 379 382
273 unless $value =~ /^\d+$/;
274  
275 379 ▶ croak "$msg greater than # cols in the data ($ncols)"
Branch: 1 branch
BranchTrueFalseTotal
if $value > $ncols - 1 3 376 379
276 if $value > $ncols - 1;
277 }
278  
279 # check to see if there are attributes called 'class' or 'key'
280 601 ▶ if ($name eq $CLASS or $name eq $KEY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq $CLASS or $name eq $KEY) 200 401 601
Condition100%3/3
$name eq $CLASS or $name eq $KEY
l!l&&r!l&&!r
16139401
MC/DC100%2/2
$name eq $CLASS or $name eq $KEY
$name eq $CLASS $name eq $KEY
Truth table100%3/3
A or B
A= $name eq $CLASSB= $name eq $KEY
ABresult
1X1
011
000
281 200 ▶ croak "The '$class' object has an attribute called '$name'"
Branch: 1 branch
BranchTrueFalseTotal
if $meta->find_attribute_by_name($name) 7 193 200
282 if $meta->find_attribute_by_name($name);
283  
284 193 next;
285 }
286  
287 401 ▶ croak "Attribute '$name' not in '$class' object"
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->find_attribute_by_name($name) 1 400 401
288 unless $meta->find_attribute_by_name($name);
289  
290 400 ▶ if ((my $ref = ref($value)) eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if ((my $ref = ref $value) eq 'HASH') { } 49 351 400
elsif ($ref eq 'ARRAY') { } 12 339 351
elsif ($ref) { } 3 336 339
291 49 $sub_desc->{$name} = $value;
292  
293 } elsif ($ref eq 'ARRAY') {
294 # descr entry looks like, e.g.:
295 # attrib => [6],
296 #
297 # ( or attrib => [key => 6, value => 7], in future... ?)
298  
299 12 ▶ croak "attribute must be of form, e.g.: '$name => [N], "
Branch: 1 branch
BranchTrueFalseTotal
unless @$value == 1 1 11 12
300 . "where N is a single integer'"
301 unless @$value == 1;
302  
303 11 my $msg = "attribute '$name => [ " . $value->[0] . " ]'. '" .
304 $value->[0] . "'";
305  
306 11 ▶ croak "$msg must be a (non-negative) integer"
Branch: 1 branch
BranchTrueFalseTotal
unless $value->[0] =~ /^\d+$/ 2 9 11
307 unless $value->[0] =~ /^\d+$/;
308  
309 9 ▶ croak "$msg greater than # cols in the data ($ncols)"
Branch: 1 branch
BranchTrueFalseTotal
if $value->[0] > $ncols - 1 1 8 9
310 if $value->[0] > $ncols - 1;
311  
312 8 $ref_attrib->{$name} = $value->[0];
313  
314 } elsif ($ref) {
315 3 croak "attribute '$name' can't be a '$ref' reference";
316  
317 } else {
318 # "simple" attribute
319 336 $attrib->{$name} = $value;
320 }
321 }
322  
323  
324 # check ref- and ...
325 150 ▶ _check_ref_attribs($class, $ref_attrib)
Branch: 1 branch
BranchTrueFalseTotal
if $ref_attrib 8 142 150
326 if $ref_attrib;
327  
328 # ... non-ref attributes from the descriptor against the Moose object
329 146 ▶ _check_non_ref_attribs($class, $attrib)
Branch: 1 branch
BranchTrueFalseTotal
if $attrib 145 1 146
330 if $attrib;
331  
332 144 ▶ croak "no attributes with column numbers in descriptor:\n", Dumper($desc)
Branch: 1 branch
BranchTrueFalseTotal
unless $attrib and %$attrib 1 143 144
Condition66%2/3
$attrib and %$attrib
!ll&&!rl&&r
10143
MC/DC50%1/2
$attrib and %$attrib
$attrib %$attrib
Truth table66%2/3
A and B
A= $attribB= %$attrib
ABresult
0X0
100
111
333 unless $attrib and %$attrib;
334  
335 143 return ($class, $attrib, $ref_attrib, $sub_desc);
336 }
337  
338 ########################################
339 # Usage: $sub_obj = _check_subobj($class, $attr_name, $type, $sub_obj);
340 #
341 # $class is the name of the current class
342 # $attr_name is the name of the attribute in the descriptor, e.g.
343 # MyObjs => { ... } (used only diagnostic messages)
344 # $type is the expected Moose type of the sub-object
345 # i.e. 'HashRef[MyObj]', 'ArrayRef[MyObj]', or 'MyObj'
346 # $sub_obj_ref Reference to the data (just returned from a recursive call to
347 # array_to_moose() ) to be stored in the sub-object,
348 # i.e. isa => 'HashRef[MyObj]', isa => 'ArrayRef[MyObj]',
349 # or isa => 'MyObj'
350 #
351 #
352 # Checks that the data in $sub_obj_ref agrees with the type of the object to
353 # contain it
354 # if $type is a ref to an object (isa => 'MyObj'), _check_subobj() converts
355 # $sub_obj_ref from an arrayref to sub-object to ref to a subobj
356 # (see notes in code below)
357 #
358 # Throws error is it finds a type mis-match
359 ########################################
360 82 ▶sub _check_subobj {
Subroutine _check_subobj: called CC=12 SCAR=24.9
361 82 my ($class, $attr_name, $type, $sub_obj) = @_;
362  
363 # for now...
364 82 ▶ croak "_check_subobj() should have 4 args" unless @_ == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 4 0 82 82
365  
366 #my $type = $class->meta->find_attribute_by_name($attr_name)->type_constraint
367 # or croak "Moose class '$class' attribute '$attr_name' has no type";
368  
369 82 ▶ if ( $type =~ /^HashRef\[([^]]*)\]/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /^HashRef\[([^]]*)\]/) { } 28 54 82
elsif ($type =~ /^ArrayRef\[([^]]*)\]/) { } 27 27 54
370  
371 #print "subobj is of type ", ref($sub_obj), "\n";
372 #print "subobj ", Dumper($sub_obj);
373  
374 28 ▶ croak "Moose attribute '$attr_name' has type '$type' "
Branch: 1 branch
BranchTrueFalseTotal
if ref $sub_obj ne "HASH" 2 26 28
375 . "but your descriptor produced an object "
376 . "of type '" . ref($sub_obj) . "'\n"
377 if ref($sub_obj) ne 'HASH';
378  
379 #print "\$1 '$1', value: ", ref( ( values %{$sub_obj} )[0] ), "\n";
380  
381 croak("Moose attribute '$attr_name' has type '$type' "
382 . "but your descriptor produced an object "
383 2 . "of type 'HashRef[" . ref( ( values %{$sub_obj} )[0] )
384 . "]'\n")
385 26 ▶ if ref( ( values %{$sub_obj} )[0] ) ne $1;
Branch: 1 branch
BranchTrueFalseTotal
if ref((values %{$sub_obj;})[0]) ne $1 2 24 26
386  
387 } elsif ( $type =~ /^ArrayRef\[([^]]*)\]/ ) {
388  
389 27 ▶ croak "Moose attribute '$attr_name' has type '$type' "
Branch: 1 branch
BranchTrueFalseTotal
if ref $sub_obj ne "ARRAY" 2 25 27
390 . "but your descriptor produced an object "
391 . "of type '" . ref($sub_obj) . "'\n"
392 if ref($sub_obj) ne 'ARRAY';
393  
394 25 ▶ croak "Moose attribute '$attr_name' has type '$type' "
Branch: 1 branch
BranchTrueFalseTotal
if ref $sub_obj->[0] ne $1 2 23 25
395 . "but your descriptor produced an object "
396 . "of type 'ArrayRef[" . ref( $sub_obj->[0] ) . "]'\n"
397 if ref( $sub_obj->[0] ) ne $1;
398  
399 } else {
400  
401 # not isa => 'ArrayRef[MyObj]' or 'HashRef[MyObj]' but isa => 'MyObj',
402 # *but* since array_to_moose() can return only a hash- or arrayref of Moose
403 # objects, $sub_obj will be an arrayref of Moose objects, which we convert to a
404 # ref to an object
405  
406 27 ▶ croak "Moose attribute '$attr_name' has type '$type' "
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sub_obj eq "ARRAY" 1 26 27
407 . "but your descriptor generated a '"
408 . ref($sub_obj)
409 . "' object and not the expected ARRAY"
410 unless ref $sub_obj eq 'ARRAY';
411  
412 # optionally give error if we got more than one row
413 26 ▶ croak "Expected a single '$type' object, but got ",
Branch: 1 branch
BranchTrueFalseTotal
if @$sub_obj != 1 and $throw_multiple_rows 3 23 26
Condition100%3/3
@$sub_obj != 1 and $throw_multiple_rows
!ll&&!rl&&r
7163
MC/DC100%2/2
@$sub_obj != 1 and $throw_multiple_rows
@$sub_obj != 1 $throw_multiple_rows
Truth table100%3/3
A and B
A= @$sub_obj != 1B= $throw_multiple_rows
ABresult
0X0
100
111
414 scalar @$sub_obj, " of them"
415 if @$sub_obj != 1 and $throw_multiple_rows;
416  
417 # convert from arrayref of objects to ref to object
418 23 $sub_obj = $sub_obj->[0];
419  
420 # print "\$sub_obj type is ", ref($sub_obj), "\n";
421  
422 23 ▶ croak "Moose attribute '$attr_name' has type '$type' "
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sub_obj eq $type 2 21 23
423 . "but your descriptor produced an object "
424 . "of type '" . ref( $sub_obj ) . "'"
425 unless ref( $sub_obj ) eq $type;
426 }
427 68 return $sub_obj;
428 }
429  
430 {
431  
432 # The Moose type hierarchy (from Moose::Manual::Types) is:
433 # Any
434 # Item
435 # Bool
436 # Maybe[`a]
437 # Undef
438 # Defined
439 # Value
440 # Str
441 # Num
442 # Int
443 # ClassName
444 # RoleName
445 # Ref
446 # ScalarRef[`a]
447 # ArrayRef[`a]
448 # HashRef[`a]
449 # CodeRef
450 # RegexpRef
451 # GlobRef
452 # FileHandle
453 # Object
454  
455 # So the test for
456  
457 22 my %simple_types;
458  
459 BEGIN
460 22 ▶ {
Subroutine BEGIN: called CC=1 SCAR=0.0
461 22 %simple_types = map { $_ => 1 }
462 qw ( Any Item Bool Undef Defined Value Str Num Int __ANON__ );
463 }
464  
465 ########################################
466 # Usage:
467 # _check_ref_attribs($class, $ref_attribs);
468 # Checks that "reference" attributes from the descriptor (e.g., attr => [N])
469 # are ArrayRef[]'s of simple attributes in the Moose object
470 # (e.g., isa => ArrayRef['Str'])
471 # Throws an exception if check fails
472 #
473 # where:
474 # $class is the current Moose class
475 # $ref_attribs an hashref of Moose attributes which are "ref
476 # attributes", e.g., " has 'hobbies' (isa => 'ArrayRef[Str]'); "
477 #
478 ########################################
479 8 ▶sub _check_ref_attribs {
Subroutine _check_ref_attribs: called CC=6 SCAR=17.9
480 8 my ($class, $ref_attribs) = @_;
481  
482 8 ▶ my $meta = $class->meta
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $class->meta or croak("No meta for class '${class}'?") 8 0 8
483 or croak "No meta for class '$class'?";
484  
485 8 foreach my $attrib ( keys %{ $ref_attribs } ) {
486 8 my $msg = "Moose class '$class' ref attrib '$attrib'";
487  
488 8 ▶ my $constraint = $meta->find_attribute_by_name($attrib)->type_constraint
Branch: 1 branch
BranchTrueFalseTotal
my $constraint = $meta->find_attribute_by_name($attrib)->type_constraint or croak("$msg has no type constraint") 7 1 8
489 or croak "$msg has no type constraint";
490  
491 #print "_check_ref_attribs(): $attrib $constraint\n";
492  
493 7 ▶ if ($constraint =~ /^ArrayRef\[([^]]*)\]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constraint =~ /^ArrayRef\[([^]]*)\]/) 6 1 7
494  
495 croak "$msg has bad type '$constraint' ('$1' is not a simple type)"
496 6 ▶ unless $simple_types{$1};
Branch: 1 branch
BranchTrueFalseTotal
unless $simple_types{$1} 2 4 6
497  
498 4 return;
499 }
500 1 croak "$msg must be an ArrayRef[`a] and not a '$constraint'";
501 }
502 }
503  
504  
505 ########################################
506 # Usage:
507 # _check_non_ref_attribs($class, $non_ref_attribs);
508 # Checks that non-ref attributes from the descriptor (e.g., attr => N)
509 # are indeed simple attributes in the Moose object (e.g., isa => 'Str')
510 # Throws an exception if check fails
511 #
512 #
513 # where:
514 # $class is the current Moose class
515 # $non_ref_attribs an hashref of Moose attributes which are
516 # non-reference, or "simple" attributes like 'Str', 'Int', etc.
517 # The key is the attribute name, the value the type
518 #
519 ########################################
520 145 ▶sub _check_non_ref_attribs {
Subroutine _check_non_ref_attribs: called CC=7 SCAR=19.5
521 145 my ($class, $attribs) = @_;
522  
523 145 ▶ my $meta = $class->meta
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $class->meta or croak("No meta for class '${class}'?") 145 0 145
524 or croak "No meta for class '$class'?";
525  
526 145 foreach my $attrib ( keys %{ $attribs } ) {
527 324 my $msg = "Moose class '$class', attrib '$attrib'";
528  
529 324 ▶ my $constraint = $meta->find_attribute_by_name($attrib)->type_constraint
Branch: 1 branch
BranchTrueFalseTotal
my $constraint = $meta->find_attribute_by_name($attrib)->type_constraint or croak("$msg has no type (isa => ...)") 323 1 324
530 or croak "$msg has no type (isa => ...)";
531  
532 #print "_check_non_ref_attribs(): $attrib '$constraint'\n";
533  
534 # kludge for Maybe[`]
535 323 $constraint =~ /^Maybe\[([^]]+)\]/;
536 323 ▶ $constraint = $1 if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 1 322 323
537  
538 #print " after: $attrib '$constraint'\n";
539  
540 323 ▶ next if $simple_types{$constraint};
Branch: 1 branch
BranchTrueFalseTotal
if $simple_types{$constraint} 322 1 323
541  
542 $msg = "$msg has type '$constraint', but your descriptor had '$attrib => "
543 1 . $attribs->{$attrib} . "'.";
544  
545 1 ▶ $msg .= " (Did you forget the '[]' brackets?)"
Branch: 1 branch
BranchTrueFalseTotal
if $constraint =~ /^ArrayRef/ 1 0 1
546 if $constraint =~ /^ArrayRef/;
547
548 1 croak $msg;
549 }
550 }
551
552 } # end of local block
553  
554  
555 22 1;
556  
557 __END__
↑ Summary