Tangram.pm

filter: Statementstmt 68.4% 665 / 972 Branchbran 55.2% 308 / 558 Conditioncond 51.4% 141 / 274 MC/DCmcdc 22.2% 36 / 162 Subroutinesub 62.7% 79 / 126 Podpod 94.2% 33 / 35 totaltotal 59.3% 1262 / 2127 CC 391 SCAR 32.7
CC
391
Cov
62%
_set_X_hash
85.3
_set_X_array
79.4
get
72.0
CRAP 26.2
« Tangram/Generator.pm ↑ Summary Tangram/Generator/Stub.pm »
1 package Class::Tangram;
2  
3 # Copyright (c) 2001 - 2005, Sam Vilain. All right reserved. This
4 # file is licensed under the terms of the Perl Artistic license.
5  
6 =head1 NAME
7  
8 Class::Tangram - Tangram-friendly classes, DWIM attributes
9  
10 =head1 SYNOPSIS
11  
12 package MyObject;
13  
14 use base qw(Class::Tangram);
15  
16 our $fields = { int => [ qw(foo bar) ],
17 string => [ qw(baz quux) ] };
18  
19 package main;
20  
21 my $object = MyObject->new(foo => 2, baz => "hello");
22  
23 print $object->baz(); # prints "hello"
24  
25 $object->set_quux("Something");
26  
27 $object->set_foo("Something"); # dies - not an integer
28  
29 =head1 DESCRIPTION
30  
31 Class::Tangram is a tool for defining objects attributes. Simply
32 define your object's fields/attributes using the same data structure
33 introduced in _A Guided Tour of Tangram_ (see L<SEE ALSO>) and
34 detailed in L<Tangram::Schema>, and you get objects that work As You'd
35 Expect(tm).
36  
37 Class::Tangram has no dependancy upon Tangram, and vice versa.
38 Neither requires anything special of your objects, nor do they insert
39 any special fields into your objects. This is a very important
40 feature with innumerable benefits, and few (if any) other object
41 persistence tools have this feature.
42  
43 So, fluff aside, let's run through how you use Class::Tangram to make
44 objects.
45  
46 First, you decide upon the attributes your object is going to have.
47 You might do this using UML, or you might pick an existing database
48 table and declare each column to be an attribute (you can leave out
49 "id"; that one is implicit; also, leave out foreign keys until later).
50  
51 Your object should use Class::Tangram as a base class;
52  
53 use base qw(Class::Tangram)
54  
55 or for older versions of perl:
56  
57 use Class::Tangram;
58 use vars qw(@ISA);
59 @ISA = qw(Class::Tangram)
60  
61 You should then define a C<$fields> variable in the scope of the
62 package, that is a B<hash> from attribute B<types> (see
63 L<Tangram::Type>) to either an B<array> of B<attribute names>, or
64 another B<hash> from B<attribute names> to B<options hashes> (or
65 C<undef>). The layout of this structure coincides exactly with the
66 C<fields> portion of a tangram schema (see L<Tangram::Schema>), though
67 there are some extra options available.
68  
69 This will hereon in be referred to as the `object schema' or just
70 `schema'.
71  
72 For example,
73  
74 package Orange;
75 use base qw(Class::Tangram);
76  
77 our $fields = {
78 int => {
79 juiciness => undef,
80 segments => {
81 # this code reference is called when this
82 # attribute is set, to check the value is
83 # OK - note, no object is passed, this is for
84 # simple marshalling only.
85 check_func => sub {
86 die "too many segments"
87 if (${(shift)} > 30);
88 },
89 # the default for this attribute.
90 init_default => 7,
91 },
92 },
93 ref => {
94 grower => {
95 },
96 },
97  
98 # 'required' attributes - insist that these fields are
99 # set, both with constructor and set()/set_X methods
100 string => {
101 # true: 'type' must have non-empty value (for
102 # strings) or be logically true (for other types)
103 type => { required => 1 },
104  
105 # false: 'tag' must be defined but may be empty
106 tag => { required => '' },
107 },
108  
109 # fields allowed by Class::Tangram but not ever
110 # stored by Tangram - no type checking by default
111 transient => [ qw(_tangible) ],
112 };
113  
114 It is of critical importance to your sanity that you understand how
115 anonymous hashes and anonymous arrays work in Perl. Some additional
116 features are used above that have not yet been introduced, but you
117 should be able to look at the above data structure and see that it
118 satisfies the conditions stated in the paragraph before it. If it is
119 hazy, I recommend reading L<perlref> or L<perlreftut>.
120  
121 When the schema for the object is first imported (see L<Schema
122 import>), Class::Tangram defines accessor functions for each of the
123 attributes defined in the schema. These accessor functions are then
124 available as C<$object-E<gt>function> on created objects. By virtue
125 of inheritance, various other methods are available.
126  
127 From Class::Tangram 1.12 onwards, perl's C<AUTOLOAD> feature is not
128 used to implement accessors; closures are compiled when the class is
129 first used.
130  
131 =cut
132  
133 4 ▶use strict 'vars', 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
134 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
135  
136 4 ▶use vars qw($VERSION %defaults @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
137  
138 4 $VERSION = "1.57";
139  
140 4 ▶use Set::Object qw(blessed reftype refaddr ish_int is_int is_double is_key);
Subroutine BEGIN: called CC=1 SCAR=0.0
141  
142 #---------------------------------------------------------------------
143 # run-time globals
144  
145 # $types{$class}->{$attribute} is the run-time discovered tangram type
146 # of each attribute
147 4 our (%types);
148  
149 # $attribute_options{$class}->{$attribute} is the hash passed to tangram
150 # for the given attribute (ie T2::Class.attribute(foo).options)
151 4 our (%attribute_options);
152  
153 # $check{$class}->{$attribute}->($value) is a function that will die
154 # if $value is not alright, see check_X functions
155 4 our (%check);
156  
157 # Destructors for each attribute. They are called as
158 # $cleaners{$class}->{$attribute}->($self, $attribute);
159 4 our (%cleaners);
160  
161 # init_default values for each attribute. These could be hash refs,
162 # array refs, code refs, or simple scalars. They will be stored as
163 # $init_defaults{$class}->{$attribute}
164 4 our (%init_defaults);
165  
166 # $required_attributes{$class}->{$attribute} records which attributes
167 # are required... used only by new() at present.
168 4 our (%required_attributes);
169  
170 # companion association registry.
171 #
172 # $companions{$class}->{$attribute} = $rem_attribute
173 #
174 # The inserted/deleted object has;
175 # $object->"${rem_attribute}_insert"($self)
176 # $object->"${rem_attribute}_remove"($self)
177 # The sub is called as $coderef->($attribute, "insert", @objs);
178 # or $coderef->($attribute, "remove", @objs);
179 4 our (%companions);
180  
181 # if a class is abstract, complain if one is constructed.
182 4 our (%abstract);
183  
184 # Set when it is detected that Tangram is not installed
185 4 my $no_tangram;
186  
187 =head1 METHODS
188  
189 The following methods are available for all Class::Tangram objects
190  
191 =head2 Constructor
192  
193 A Constructor is a method that returns a new instance of an object.
194  
195 =over 4
196  
197 =item Class-E<gt>new (attribute1 =E<gt> value, attribute2 =E<gt> value)
198  
199 Sets up a new object of type C<Class>, with attributes set to the
200 values supplied.
201  
202 Can also be used as an object method (normal use is as a "class
203 method"), in which case it returns a B<copy> of the object, without
204 any deep copying.
205  
206 =cut
207  
208 sub new
209 82 ▶{
Subroutine new: called CC=5 SCAR=16.1
Pod documented
210 82 my $invocant = shift;
211 82 ▶ my $class = ref $invocant || $invocant;
Condition66%2/3
ref $invocant || $invocant
l!l&&r!l&&!r
3790
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table66%2/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
212  
213 # Setup the object
214 82 my $self = { };
215 82 bless $self, $class;
216  
217 # auto-load schema as necessary
218 82 ▶ exists $types{$class} or import_schema($class);
Branch: 1 branch
BranchTrueFalseTotal
exists $Class::Tangram::types{$class} or import_schema($class) 63 19 82
219  
220 croak "Attempt to instantiate an abstract type $class"
221 82 ▶ if ($abstract{$class});
Branch: 1 branch
BranchTrueFalseTotal
if $Class::Tangram::abstract{$class} 0 82 82
222  
223 82 ▶ if (ref $invocant)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $invocant) { } 3 79 82
224 {
225 # The copy constructor; this could be better :)
226 # this has the side effect of much auto-vivification.
227 3 $self->set( $invocant->_copy(@_) ); # override with @values
228 }
229 else
230 {
231 79 $self->set (@_); # start with @values
232 }
233  
234 76 $self->_fill_init_default();
235 76 $self->_check_required();
236  
237 70 return $self;
238  
239 }
240  
241 76 ▶sub _fill_init_default {
Subroutine _fill_init_default: called CC=7 SCAR=20.9
242 76 my $self = shift;
243 76 ▶ my $class = ref $self or confess "_fill_init_default usage error";
Branch: 1 branch
BranchTrueFalseTotal
my $class = ref $self or confess("_fill_init_default usage error") 76 0 76
244  
245 # fill in fields that have defaults
246 76 ▶ while ( my ($attribute, $default) =
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$Class::Tangram::init_defaults{$class};}) 90 76 166
247 166 each %{$init_defaults{$class}} ) {
248  
249 90 ▶ next if (exists $self->{$attribute});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$attribute} 30 60 90
250  
251 60 my $setter = "set_$attribute";
252 60 ▶ if (ref $default eq "CODE") {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $default eq 'CODE') { } 37 23 60
elsif (ref $default eq 'HASH') { } 0 23 23
elsif (ref $default eq 'ARRAY') { } 0 23 23
253 # sub { }, attribute gets return value
254 37 $self->$setter( $default->($self) );
255  
256 } elsif (ref $default eq "HASH") {
257 # hash ref, copy hash
258 0 $self->$setter( { %{ $default } } );
259  
260 } elsif (ref $default eq "ARRAY") {
261 # array ref, copy array
262 0 $self->$setter( [ @{ $default } ] );
263  
264 } else {
265 # something else, an object or a scalar
266 23 $self->$setter($default);
267 }
268 }
269 }
270  
271 76 ▶sub _check_required {
Subroutine _check_required: called CC=8 SCAR=20.8
272 76 my $self = shift;
273 76 my $class = ref $self;
274  
275 # make sure field is not undef if 'required' option is set
276 76 ▶ if (my $required = $required_attributes{$class}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $required = $Class::Tangram::required_attributes{$class}) 21 55 76
277  
278 # find the immediate caller outside of this package
279 21 my $i = 0;
280 21 ▶ $i++ while UNIVERSAL::isa($self, scalar(caller($i))||";->");
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($self, scalar caller $i || ";->") and do { ++$i } 26 21 47
Condition50%1/2
scalar caller $i || ';->'
l!l
470
MC/DC0%0/1
scalar caller $i || ';->'
scalar caller $i
Truth table50%1/2
A
A= scalar caller $i
Aresult
11
00
281  
282 # give Tangram some lenience - it is exempt from the effects
283 # of the "required" option
284 21 ▶ unless ( caller($i) =~ m/^Tangram::/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller($i) =~ /^Tangram::/) 18 3 21
285 18 my @missing;
286 18 ▶ while ( my ($attribute, $value) = each %$required ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$required) 42 18 60
287 push(@missing, $attribute)
288 42 ▶ if ! exists $self->{$attribute};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$attribute} 8 34 42
289 }
290 18 ▶ croak("object missing required attribute(s): "
Branch: 1 branch
BranchTrueFalseTotal
if @missing 6 12 18
291 .join(', ',@missing).'.') if @missing;
292 }
293 }
294 }
295  
296 # $obj->_copy($target): copy self into the first arg
297 3 ▶sub _copy {
Subroutine _copy: called CC=4 SCAR=14.4
298 3 my $self = shift;
299 3 my $class = ref $self;
300 0 ▶ my $types = $types{$class} || do { import_schema($class);
Condition33%1/3
$Class::Tangram::types{$class} || do { import_schema($class); $Class::Tangram::types{$class} }
l!l&&r!l&&!r
300
MC/DC0%0/2
$Class::Tangram::types{$class} || do { import_schema($class); $Class::Tangram::types{$class} }
$Class::Tangram::types{$class} do { import_schema($class); $Class::Tangram::types{$class} }
Truth table33%1/3
A || B
A= $Class::Tangram::types{$class}B= do { import_schema($class); $Class::Tangram::types{$class} }
ABresult
1X1
011
000
301 0 $types{$class}; };
302 3 my %passed = (@_);
303  
304 # This will pretty much autovivify everything nearby.
305 # c'est la vie
306 3 my @rv;
307 3 for my $field ( sort keys %$types ) {
308 21 ▶ next if exists $passed{$field};
Branch: 1 branch
BranchTrueFalseTotal
if exists $passed{$field} 1 20 21
309 20 my $func = "get_$field";
310 20 push @rv, ($field => scalar($self->$func()));
311 }
312 3 return @rv, %passed;
313 }
314  
315  
316 =back
317  
318 =head2 Accessing & Setting Attributes
319  
320 =over
321  
322 =item $instance->set(attribute => $value, ...)
323  
324 Sets the attributes of the given instance to the given values. croaks
325 if there is a problem with the values.
326  
327 This function simply calls C<$instance-E<gt>set_attribute($value)> for
328 each of the C<attribute =E<gt> $value> pairs passed to it.
329  
330 =cut
331  
332 83 ▶sub set {
Subroutine set: called CC=7 SCAR=19.9
Pod documented
333 83 my $self = shift;
334  
335 # yes, this is a lot to do. yes, it's slow. But I'm fairly
336 # certain that this could be handled efficiently if it were to be
337 # moved inside the Perl interpreter or an XS module
338 83 ▶ UNIVERSAL::isa($self, "Class::Tangram") or croak "type mismatch";
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($self, "Class::Tangram") or croak("type mismatch") 83 0 83
339 83 my $class = ref $self;
340 83 ▶ exists $check{$class} or import_schema($class);
Branch: 1 branch
BranchTrueFalseTotal
exists $Class::Tangram::check{$class} or import_schema($class) 83 0 83
341 83 ▶ croak "set must be called with an even number of arguments"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ & 1 0 83 83
342 if (scalar(@_) & 1);
343  
344 83 ▶ while (my ($name, $value) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 145 76 221
345  
346 145 my $setter = "set_".$name;
347  
348 145 ▶ croak "attempt to set an illegal field $name in a $class"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($setter) or $self->can("AUTOLOAD") 1 144 145
Condition66%2/3
$self->can($setter) or $self->can("AUTOLOAD")
l!l&&r!l&&!r
14401
MC/DC50%1/2
$self->can($setter) or $self->can("AUTOLOAD")
$self->can($setter) $self->can("AUTOLOAD")
Truth table66%2/3
A or B
A= $self->can($setter)B= $self->can("AUTOLOAD")
ABresult
1X1
011
000
349 unless $self->can($setter) or $self->can("AUTOLOAD");
350  
351 144 $self->$setter($value);
352 }
353 }
354  
355 =item $instance->get("attribute")
356  
357 Gets the value of C<$attribute>. This simply calls
358 C<$instance-E<gt>get_attribute>. If multiple attributes are listed,
359 then a list of the attribute values is returned in order. Note that
360 you get back the results of the scalar context C<get_attribute> call
361 in this case.
362  
363 =cut
364  
365 0 ▶sub get {
Subroutine get: not called CC=8 SCAR=42.8
Pod documented
366 0 my $self = shift;
367 0 ▶ croak "get what?" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
368 0 ▶ UNIVERSAL::isa($self, "Class::Tangram") or croak "type mismatch";
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($self, "Class::Tangram") or croak("type mismatch") 0 0 0
369  
370 0 my $class = ref $self;
371 0 ▶ exists $check{$class} or import_schema($class);
Branch: 1 branch
BranchTrueFalseTotal
exists $Class::Tangram::check{$class} or import_schema($class) 0 0 0
372  
373 0 my $multiget = (scalar(@_) != 1);
374  
375 0 my @return;
376 0 ▶ while ( my $field = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $field = shift()) 0 0 0
377 0 my $getter = "get_".$field;
378 0 ▶ croak "attempt to read an illegal field $field in a $class"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($getter) or $self->can("AUTOLOAD") 0 0 0
Condition0%0/3
$self->can($getter) or $self->can("AUTOLOAD")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->can($getter) or $self->can("AUTOLOAD")
$self->can($getter) $self->can("AUTOLOAD")
Truth table0%0/3
A or B
A= $self->can($getter)B= $self->can("AUTOLOAD")
ABresult
1X1
011
000
379 unless $self->can($getter) or $self->can("AUTOLOAD");
380  
381 0 ▶ if ( $multiget ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multiget) { } 0 0 0
382 0 push @return, scalar($self->$getter());
383 } else {
384 0 return $self->$getter();
385 }
386 }
387  
388 0 return @return;
389 }
390  
391 =item $instance->attribute($value)
392  
393 For DWIM's sake, the behaviour of this function depends on the type of
394 the attribute.
395  
396 =over
397  
398 =for the keen eye
399  
400 This function, along with the get_attribute and set_attribute
401 functions, are actually written inside a loop of the import_schema()
402 function. The rationale for this is that a single closure is faster
403 than two functions.
404  
405 =item scalar attributes
406  
407 If C<$value> is not given, then
408 this is equivalent to C<$instance-E<gt>get_attribute>
409  
410 If C<$value> is given, then this is equivalent to
411 C<$instance-E<gt>set_attribute($value)>. This usage issues a warning
412 if warnings are on; you should change your code to use the
413 set_attribute syntax for better readability. OO veterans will tell
414 you that for maintainability object method names should always be a
415 verb.
416  
417 =item associations
418  
419 With attributes that are associations, the default action when a
420 parameter is given depends on what the argument list looks like. If
421 it appears to be a series of C<(key =E<gt> value)> pairs (with or
422 without the keys), then it is translated into call to C<set>.
423 Containers (or C<undef>) are also allowed in place of values.
424  
425 If the argument list contains only keys (ie, scalars) then it is
426 assumed you mean to `get' attributes.
427  
428 If you pass this method an ambiguous argument list (eg, Key Key Value
429 or Value Key) then you get an exception.
430  
431 =back
432  
433 =item $instance->get_attribute([@keys])
434  
435 =over
436  
437 =item scalar attributes
438  
439 Returns the value of the attribute. This may be a normal scalar, for
440 C<int>, C<string>, and the C<datetime> related types, or an ARRAY or
441 HASH REF, in the case of C<flat_array> or C<flat_hash> types.
442  
443 =item associations
444  
445 The association types - C<ref>, C<set>, C<array> and C<hash> return
446 different results depending upon the context and presence of keys in
447 the method's parameter list.
448  
449 In list context with no parameters, always returns the entire contents
450 of the container, as a list, without keys. No sorting is applied,
451 unless there is an implicit order due to the type of container the
452 association uses (ie, arrays).
453  
454 In scalar context with no parameters, always returns the container - a
455 Set::Object, Array or Hash (or, for single element containers, the
456 single element or C<undef> if it is empty).
457  
458 In list context with parameters, the parameters are assumed to be a
459 list of keys to look up. The container does its best to look up items
460 corresponding to the keys given, and then returns them in the same
461 order as the keys.
462  
463 In scalar context with one parameter, the function returns that
464 element best described by that key, or C<undef> if it is not present
465 in the container.
466  
467 =back
468  
469 =cut
470  
471 2 ▶sub looks_like_KVKV {
Subroutine looks_like_KVKV: called CC=2 SCAR=7.1
Pod undocumented
472 2 ▶ my $input = join("", map { is_key($_) ? "K" : "V" } @_);
Branch: 1 branch
BranchTrueFalseTotal
is_key($_) ? : 2 0 2
473 2 return ($input =~ m/^(K?V)+$/g);
474 }
475  
476 2 ▶sub looks_like_KK {
Subroutine looks_like_KK: called CC=2 SCAR=7.1
Pod undocumented
477 2 ▶ my $input = join("", map { is_key($_) ? "K" : "V" } @_);
Branch: 1 branch
BranchTrueFalseTotal
is_key($_) ? : 2 0 2
478 2 return ($input =~ m/^K+$/g);
479 }
480  
481 =over 4
482  
483 =item `ref' attributes get
484  
485 `ref' attributes are modelled as a container with a single element.
486  
487 The accessor always returns the single element.
488  
489 =cut
490  
491 13 ▶sub _get_X_ref {
Subroutine _get_X_ref: called CC=3 SCAR=11.6
492 13 my $self = shift;
493 13 my $X = shift;
494 13 my $rv = $self->{$X};
495 # work around perl 5.8.0 tie() bug
496 13 my $t = tied $self->{$X};
497 13 ▶ untie($self->{$X}) if ($t and $t =~ m/^Tangram/);
Branch: 1 branch
BranchTrueFalseTotal
if $t and $t =~ /^Tangram/ 0 13 13
Condition33%1/3
$t and $t =~ /^Tangram/
!ll&&!rl&&r
1300
MC/DC0%0/2
$t and $t =~ /^Tangram/
$t $t =~ /^Tangram/
Truth table33%1/3
A and B
A= $tB= $t =~ /^Tangram/
ABresult
0X0
100
111
498 13 return $self->{$X};
499 }
500  
501 =item `array' attributes get
502  
503 =cut
504  
505 15 ▶sub _get_X_array {
Subroutine _get_X_array: called CC=12 SCAR=38.7
506 15 my $self = shift;
507 15 my $X = shift;
508 15 ▶ my $a = ($self->{$X} ||= [ ]);
Condition100%2/2
$self->{$X} ||= []
l!l
96
MC/DC100%1/1
$self->{$X} ||= []
$self->{$X}
Truth table100%2/2
A
A= $self->{$X}
Aresult
11
00
509 # work around perl 5.8.0 tie() bug
510 15 my $t = tied $self->{$X};
511 15 ▶ untie($self->{$X}) if ($t and $t =~ m/^Tangram/);
Branch: 1 branch
BranchTrueFalseTotal
if $t and $t =~ /^Tangram/ 0 15 15
Condition33%1/3
$t and $t =~ /^Tangram/
!ll&&!rl&&r
1500
MC/DC0%0/2
$t and $t =~ /^Tangram/
$t $t =~ /^Tangram/
Truth table33%1/3
A and B
A= $tB= $t =~ /^Tangram/
ABresult
0X0
100
111
512  
513 15 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 15 15
514 0 my @rv;
515 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
516 0 my $key = shift;
517 0 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 0 0 0
518 0 ▶ if (defined(my $n = ish_int($key))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $n = ish_int($key))) { } 0 0 0
519 0 push @rv, $a->[$n];
520 } else {
521 0 ▶ carp("Keyed lookup to array container "
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
522 .ref($self)."->$X($key), returning last "
523 ."member of array")
524 if $^W;
525 0 push @rv, $a->[$#{$a}];
526 }
527 }
528 }
529 0 ▶ if (wantarray or @rv > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray or @rv > 1) { } 0 0 0
Condition0%0/3
wantarray or @rv > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
wantarray or @rv > 1
wantarray @rv > 1
Truth table0%0/3
A or B
A= wantarrayB= @rv > 1
ABresult
1X1
011
000
530 0 return @rv;
531 } else {
532 0 return $rv[0];
533 }
534 } else {
535 15 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 7 8 15
536 7 return @{$a};
537 } else {
538 8 return $a;
539 }
540 }
541 }
542  
543 =item `set' attributes get
544  
545 =cut
546  
547 112 ▶sub _get_X_set {
Subroutine _get_X_set: called CC=11 SCAR=24.7
548 112 my $self = shift;
549 112 my $X = shift;
550 112 ▶ my $a = ($self->{$X} ||= Set::Object->new());
Condition66%2/3
$self->{$X} ||= "Set::Object"->new
l!l&&r!l&&!r
10930
MC/DC0%0/2
$self->{$X} ||= "Set::Object"->new
$self->{$X} "Set::Object"->new
Truth table66%2/3
A ||= B
A= $self->{$X}B= "Set::Object"->new
ABresult
1X1
011
000
551 # work around perl 5.8.0 tie() bug
552 112 my $t = tied $self->{$X};
553 112 ▶ untie($self->{$X}) if ($t and $t =~ m/^Tangram/);
Branch: 1 branch
BranchTrueFalseTotal
if $t and $t =~ /^Tangram/ 0 112 112
Condition33%1/3
$t and $t =~ /^Tangram/
!ll&&!rl&&r
11200
MC/DC0%0/2
$t and $t =~ /^Tangram/
$t $t =~ /^Tangram/
Truth table33%1/3
A and B
A= $tB= $t =~ /^Tangram/
ABresult
0X0
100
111
554  
555 112 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 110 112
556 # uh-oh, asking a set for keyed values. hmm.
557 2 my @members = $a->members(); # maybe should shuffle
558 2 my @rv;
559 2 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 2 5
560 3 my $key = shift;
561 3 ▶ if (defined $key and @members) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and @members) 3 0 3
Condition33%1/3
defined $key and @members
!ll&&!rl&&r
003
MC/DC0%0/2
defined $key and @members
defined $key @members
Truth table33%1/3
A and B
A= defined $keyB= @members
ABresult
0X0
100
111
562 3 push @rv, (shift @members);
563 }
564 }
565 2 ▶ if (wantarray or @rv > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray or @rv > 1) { } 1 1 2
Condition66%2/3
wantarray or @rv > 1
l!l&&r!l&&!r
101
MC/DC50%1/2
wantarray or @rv > 1
wantarray @rv > 1
Truth table66%2/3
A or B
A= wantarrayB= @rv > 1
ABresult
1X1
011
000
566 1 return @rv;
567 } else {
568 1 return $rv[0];
569 }
570 } else {
571 110 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 30 80 110
572 30 return $a->members();
573 } else {
574 80 return $a;
575 }
576 }
577 }
578  
579 =item `hash' attributes get
580  
581 =cut
582  
583 11 ▶sub _get_X_hash {
Subroutine _get_X_hash: called CC=10 SCAR=24.8
584 11 my $self = shift;
585 11 my $X = shift;
586 11 ▶ my $a = ($self->{$X} ||= {});
Condition100%2/2
$self->{$X} ||= {}
l!l
83
MC/DC100%1/1
$self->{$X} ||= {}
$self->{$X}
Truth table100%2/2
A
A= $self->{$X}
Aresult
11
00
587 # work around perl 5.8.0 tie() bug
588 11 my $t = tied $self->{$X};
589 11 ▶ untie($self->{$X}) if ($t and $t =~ m/^Tangram/);
Branch: 1 branch
BranchTrueFalseTotal
if $t and $t =~ /^Tangram/ 0 11 11
Condition33%1/3
$t and $t =~ /^Tangram/
!ll&&!rl&&r
1100
MC/DC0%0/2
$t and $t =~ /^Tangram/
$t $t =~ /^Tangram/
Truth table33%1/3
A and B
A= $tB= $t =~ /^Tangram/
ABresult
0X0
100
111
590  
591 11 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 9 11
592 2 my @rv;
593 2 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 2 4
594 2 my $key = shift;
595 2 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 2 0 2
596 2 push @rv, $a->{$key};
597 }
598 }
599 2 ▶ if (wantarray or @rv > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray or @rv > 1) { } 0 2 2
Condition33%1/3
wantarray or @rv > 1
l!l&&r!l&&!r
002
MC/DC0%0/2
wantarray or @rv > 1
wantarray @rv > 1
Truth table33%1/3
A or B
A= wantarrayB= @rv > 1
ABresult
1X1
011
000
600 0 return @rv;
601 } else {
602 2 return $rv[0];
603 }
604 } else {
605 9 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 9 9
606 0 return values %$a;
607 } else {
608 9 return $a;
609 }
610 }
611  
612 }
613  
614 =back
615  
616 =item $instance->set_attribute($value)
617  
618 The normative way of setting attributes. If you wish to override the
619 behaviour of an object when getting or setting an attribute, override
620 these functions. They will be called when you use
621 C<$instance-E<gt>attribute>, C<$instance-E<gt>get()>, constructors,
622 etc.
623  
624 When attributes that are associations are changed via other functions,
625 a new container with the new contents is built, and then passed to
626 this function.
627  
628 =over
629  
630 =item `ref' attributes set
631  
632 Like all other container set methods, this method may be passed a Set,
633 Array or Hash, and all the members are added in order to (single
634 element) container. If the resultant container has more than one
635 item, it raises a run-time warning.
636  
637 =cut
638  
639 21 ▶sub _set_X_ref {
Subroutine _set_X_ref: called CC=22 SCAR=35.8
640 21 my $self = shift;
641 21 my $base_type = shift;
642 21 my $companion = shift;
643 21 my $X = shift;
644 21 my $class = ref $self;
645  
646 21 my @ncc;
647 21 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 20 21 41
648 20 my $value = shift;
649 20 ▶ if (blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&blessed($value)) { } 19 1 20
650 19 ▶ if ($value->isa("Set::Object")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->isa('Set::Object')) { } 0 19 19
651 0 push @ncc, $value->members();
652 } else {
653 19 push @ncc, $value;
654 }
655 } else {
656 1 my $ref = ref $value;
657 1 ▶ if ($ref eq "ARRAY") {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 0 1 1
elsif ($ref eq 'HASH') { } 0 1 1
elsif (defined $value) { } 1 0 1
658 0 @ncc = @$value;
659 } elsif ($ref eq "HASH") {
660 0 @ncc = values %$value;
661 } elsif (defined $value) {
662 1 push @ncc, $value;
663 }
664 }
665 }
666  
667 21 ▶ if (@ncc) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ncc) 20 1 21
668 20 ▶ if (my $checkit = \$check{$class}->{$X}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $checkit = \$Class::Tangram::check{$class}{$X}) { } 20 0 20
669 # There's a check function! Use it!
670 20 $ {$checkit}->(\$ncc[0]);
671 } else {
672 0 ▶ if (@ncc > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ncc > 1) 0 0 0
673 0 ▶ carp ("container ".ref($self)."->$X overflowed! "
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
674 ."Rejecting members at end!")
675 if $^W;
676 0 @ncc = $ncc[0];
677 }
678 0 ▶ croak("Tried to place `$ncc[0]' in a ref container")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ncc[0] 0 0 0
679 unless (ref $ncc[0]);
680 }
681 }
682  
683 20 my $old = $self->{$X};
684 20 my $chosen = $self->{$X} = $ncc[0];
685  
686 20 ▶ if ($companion and refaddr($self->{$X}) != refaddr($old)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($companion and &refaddr($self->{$X}) != &refaddr($old)) 6 14 20
Condition66%2/3
$companion and &refaddr($self->{$X}) != &refaddr($old)
!ll&&!rl&&r
1406
MC/DC50%1/2
$companion and &refaddr($self->{$X}) != &refaddr($old)
$companion &refaddr($self->{$X}) != &refaddr($old)
Truth table66%2/3
A and B
A= $companionB= &refaddr($self->{$X}) != &refaddr($old)
ABresult
0X0
100
111
687 6 my $remove = $companion."_remove";
688 6 my $insert = $companion."_insert";
689 6 my $includes = $companion."_includes";
690  
691 6 ▶ $old->$remove($self)
Branch: 1 branch
BranchTrueFalseTotal
if $old and $old->can($remove) and $old->can($includes) and $old->$includes($self) 0 6 6
Condition66%6/9
$old and $old->can($remove) and $old->can($includes) and $old->$includes($self)
!ll&&!rl&&r
510
$old and $old->can($remove) and $old->can($includes)
!ll&&!rl&&r
501
$old and $old->can($remove)
!ll&&!rl&&r
501
MC/DC0%0/4
$old and $old->can($remove) and $old->can($includes) and $old->$includes($self)
$old $old->can($remove) $old->can($includes) $old->$includes($self)
Truth table40%2/5
A and B and C and D
A= $oldB= $old->can($remove)C= $old->can($includes)D= $old->$includes($self)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
692 if ($old and $old->can($remove)
693 and $old->can($includes)
694 and $old->$includes($self));
695  
696 6 ▶ $chosen->$insert($self)
Branch: 1 branch
BranchTrueFalseTotal
if $chosen and $chosen->can($insert) and $chosen->can($includes) and not $chosen->$includes($self) 2 4 6
Condition77%7/9
$chosen and $chosen->can($insert) and $chosen->can($includes) and not $chosen->$includes($self)
!ll&&!rl&&r
132
$chosen and $chosen->can($insert) and $chosen->can($includes)
!ll&&!rl&&r
105
$chosen and $chosen->can($insert)
!ll&&!rl&&r
105
MC/DC50%2/4
$chosen and $chosen->can($insert) and $chosen->can($includes) and not $chosen->$includes($self)
$chosen $chosen->can($insert) $chosen->can($includes) not $chosen->$includes($self)
Truth table60%3/5
A and B and C and not(D)
A= $chosenB= $chosen->can($insert)C= $chosen->can($includes)D= not $chosen->$includes($self)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
697 if ($chosen and $chosen->can($insert)
698 and $chosen->can($includes)
699 and !$chosen->$includes($self));
700 }
701  
702 }
703  
704 =item `set' attributes set
705  
706 =cut
707  
708 89 ▶sub _set_X_set {
Subroutine _set_X_set: called CC=25 SCAR=32.3
709 89 my $self = shift;
710 89 my $base_type = shift;
711 89 my $companion = shift;
712 89 my $X = shift;
713 89 my $class = ref $self;
714  
715 # Shortcut to avoid penalty when simply setting to a new container
716 89 ▶ if (@_ == 1 and !$companion and
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not $companion and UNIVERSAL::isa($_[0], "Set::Object")) 21 68 89
Condition100%6/6
@_ == 1 and not $companion and UNIVERSAL::isa($_[0], "Set::Object")
!ll&&!rl&&r
66221
@_ == 1 and not $companion
!ll&&!rl&&r
323423
MC/DC100%3/3
@_ == 1 and not $companion and UNIVERSAL::isa($_[0], "Set::Object")
@_ == 1 not $companion UNIVERSAL::isa($_[0], "Set::Object")
Truth table100%4/4
A and not(B) and C
A= @_ == 1B= not $companionC= UNIVERSAL::isa($_[0], "Set::Object")
ABCresult
0XX0
10X0
1100
1111
717 UNIVERSAL::isa($_[0], "Set::Object")) {
718 21 ▶ if (my $checkit = \$check{$class}->{$X}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $checkit = \$Class::Tangram::check{$class}{$X}) 21 0 21
719 # There's a check function! Use it!
720 21 $ {$checkit}->(\($_[0]));
721 }
722 21 delete $self->{$X}; # make sure it's not tied - 5.8.0 bug
723 21 return $self->{$X} = $_[0];
724 }
725  
726 68 my @ncc;
727 68 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 84 68 152
728 84 my $value = (shift @_);
729 84 ▶ if (blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&blessed($value)) { } 81 3 84
730 81 ▶ if ($value->isa("Set::Object")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->isa('Set::Object')) { } 46 35 81
731 46 push @ncc, $value->members();
732 } else {
733 35 push @ncc, $value;
734 }
735 } else {
736 3 my $ref = ref $value;
737 3 ▶ if ($ref eq "ARRAY") {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 2 1 3
elsif ($ref eq 'HASH') { } 1 0 1
elsif (defined ish_int($value)) { } 0 0 0
738 2 push @ncc, @$value;
739 } elsif ($ref eq "HASH") {
740 1 push @ncc, values %$value;
741 } elsif (defined(ish_int($value))) {
742 0 $ncc[$value] = (shift @_);
743 } else {
744 # some other type of key, ignore it
745 }
746 }
747 }
748  
749 68 my ($old, $new);
750  
751 68 ▶ if ($companion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($companion) 59 9 68
752 # ordering is ignored for arrays when it comes to
753 # companions
754 59 ▶ $old = Set::Object->new( $self->{$X} ? $self->{$X}->members
Branch: 1 branch
BranchTrueFalseTotal
$self->{$X} ? : 31 28 59
755 : () );
756 }
757 68 $new = Set::Object->new(@ncc);
758  
759 68 ▶ if (my $checkit = \$check{$class}->{$X}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $checkit = \$Class::Tangram::check{$class}{$X}) 68 0 68
760 # There's a check function! Use it!
761 68 $ {$checkit}->(\$new);
762 }
763 68 $self->{$X} = $new;
764  
765 68 ▶ if ($companion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($companion) 59 9 68
766  
767 # I love Set::Object, it should be a builtin data type :-)
768 59 my $gone = $old - $new;
769 59 my $added = $new - $old;
770  
771 59 my $includes_func = $companion."_includes";
772  
773 59 ▶ if ($gone->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gone->size) 9 50 59
774 9 my $remove_func = $companion."_remove";
775 9 for my $gonner ($gone->members) {
776 9 ▶ if ($gonner->can($remove_func) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($gonner->can($remove_func) and $gonner->can($includes_func) and $gonner->$includes_func($self)) 5 4 9
Condition83%5/6
$gonner->can($remove_func) and $gonner->can($includes_func) and $gonner->$includes_func($self)
!ll&&!rl&&r
225
$gonner->can($remove_func) and $gonner->can($includes_func)
!ll&&!rl&&r
207
MC/DC66%2/3
$gonner->can($remove_func) and $gonner->can($includes_func) and $gonner->$includes_func($self)
$gonner->can($remove_func) $gonner->can($includes_func) $gonner->$includes_func($self)
Truth table75%3/4
A and B and C
A= $gonner->can($remove_func)B= $gonner->can($includes_func)C= $gonner->$includes_func($self)
ABCresult
0XX0
10X0
1100
1111
777 $gonner->can($includes_func) &&
778 $gonner->$includes_func($self)) {
779 5 $gonner->$remove_func($self);
780 }
781 }
782 }
783  
784 59 ▶ if ($added->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($added->size) 23 36 59
785 23 my $insert_func = $companion."_insert";
786 23 for my $new_mate ($added->members) {
787 24 ▶ if ($new_mate->can($insert_func) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($new_mate->can($insert_func) and $new_mate->can($includes_func) and not $new_mate->$includes_func($self)) 15 9 24
Condition83%5/6
$new_mate->can($insert_func) and $new_mate->can($includes_func) and not $new_mate->$includes_func($self)
!ll&&!rl&&r
2715
$new_mate->can($insert_func) and $new_mate->can($includes_func)
!ll&&!rl&&r
2022
MC/DC66%2/3
$new_mate->can($insert_func) and $new_mate->can($includes_func) and not $new_mate->$includes_func($self)
$new_mate->can($insert_func) $new_mate->can($includes_func) not $new_mate->$includes_func($self)
Truth table75%3/4
A and B and not(C)
A= $new_mate->can($insert_func)B= $new_mate->can($includes_func)C= not $new_mate->$includes_func($self)
ABCresult
0XX0
10X0
1100
1111
788 $new_mate->can($includes_func) &&
789 !$new_mate->$includes_func($self) ) {
790 15 $new_mate->$insert_func($self);
791 }
792 }
793 }
794 }
795 }
796  
797 =item `array' attributes set
798  
799 =cut
800  
801 7 ▶sub _set_X_array {
Subroutine _set_X_array: called CC=25 SCAR=43.8
802 7 my $self = shift;
803 7 my $base_type = shift;
804 7 my $companion = shift;
805 7 my $X = shift;
806 7 my $class = ref $self;
807  
808 # Shortcut to avoid penalty when simply setting to a new container
809 7 ▶ if (@_ == 1 and !$companion and ref $_[0] eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not $companion and ref $_[0] eq "ARRAY") 0 7 7
Condition50%3/6
@_ == 1 and not $companion and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
700
@_ == 1 and not $companion
!ll&&!rl&&r
160
MC/DC0%0/3
@_ == 1 and not $companion and ref $_[0] eq "ARRAY"
@_ == 1 not $companion ref $_[0] eq "ARRAY"
Truth table50%2/4
A and not(B) and C
A= @_ == 1B= not $companionC= ref $_[0] eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
810 0 delete $self->{$X}; # make sure it's not tied - 5.8.0 bug
811 0 ▶ if (my $checkit = \$check{$class}->{$X}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $checkit = \$Class::Tangram::check{$class}{$X}) 0 0 0
812 # There's a check function! Use it!
813 0 $ {$checkit}->(\($_[0]));
814 }
815 0 return $self->{$X} = $_[0];
816 }
817  
818 7 my @ncc;
819 7 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 8 7 15
820 8 my ($value) = (shift @_);
821 8 ▶ if (blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&blessed($value)) { } 7 1 8
822 7 ▶ if ($value->isa("Set::Object")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->isa('Set::Object')) { } 0 7 7
823 0 push @ncc, $value->members();
824 } else {
825 7 push @ncc, $value;
826 }
827 } else {
828 1 my $ref = ref $value;
829 1 ▶ if ($ref eq "ARRAY") {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 1 0 1
elsif ($ref eq 'HASH') { } 0 0 0
elsif (defined ish_int($value)) { } 0 0 0
830 1 push @ncc, @$value;
831 } elsif ($ref eq "HASH") {
832 0 push @ncc, values %$value;
833 } elsif (defined(ish_int($value))) {
834 0 $ncc[$value] = (shift @_);
835 } else {
836 # some other type of key, ignore it
837 }
838 }
839 }
840  
841 7 my ($set, $ncc);
842  
843 7 ▶ if ($companion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($companion) 7 0 7
844 # ordering is ignored for arrays when it comes to
845 # companions
846 $set = Set::Object->new( blessed($self->{$X})
847 0 ▶ ? (grep { ref $_ } $self->{$X}->members)
Branch: 1 branch
BranchTrueFalseTotal
&blessed($self->{$X}) ? : 0 7 7
848 : () );
849 7 $ncc = Set::Object->new(grep { ref $_ } @ncc);
850 }
851  
852 7 ▶ if (my $checkit = $check{$class}->{$X}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $checkit = $Class::Tangram::check{$class}{$X}) { } 7 0 7
853 # There's a check function! Use it!
854 7 $checkit->(\\@ncc);
855 } else {
856 0 confess "no checkit for $self - $class, X is $X, checkit is $$checkit\n";
857 }
858  
859 7 $self->{$X} = \@ncc;
860  
861 7 ▶ if ($companion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($companion) 7 0 7
862  
863 # I love Set::Object, it should be a builtin data type :-)
864 7 my $gone = $set - $ncc;
865 7 my $new = $ncc - $set;
866  
867 7 my $includes_func = $companion."_includes";
868  
869 7 ▶ if ($gone->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gone->size) 0 7 7
870 0 my $remove_func = $companion."_remove";
871 0 for my $gonner ($gone->members) {
872 0 ▶ if ($gonner->can($remove_func) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($gonner->can($remove_func) and $gonner->can($includes_func) and $gonner->$includes_func($self)) 0 0 0
Condition0%0/6
$gonner->can($remove_func) and $gonner->can($includes_func) and $gonner->$includes_func($self)
!ll&&!rl&&r
000
$gonner->can($remove_func) and $gonner->can($includes_func)
!ll&&!rl&&r
000
MC/DC0%0/3
$gonner->can($remove_func) and $gonner->can($includes_func) and $gonner->$includes_func($self)
$gonner->can($remove_func) $gonner->can($includes_func) $gonner->$includes_func($self)
Truth table0%0/4
A and B and C
A= $gonner->can($remove_func)B= $gonner->can($includes_func)C= $gonner->$includes_func($self)
ABCresult
0XX0
10X0
1100
1111
873 $gonner->can($includes_func) &&
874 $gonner->$includes_func($self)) {
875 0 $gonner->$remove_func($self);
876 }
877 }
878 }
879  
880 7 ▶ if ($new->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new->size) 7 0 7
881 7 my $insert_func = $companion."_insert";
882 7 for my $new_mate ($new->members) {
883 7 ▶ if ($new_mate->can($insert_func) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($new_mate->can($insert_func) and $new_mate->can($includes_func) and not $new_mate->$includes_func($self)) 1 6 7
Condition50%3/6
$new_mate->can($insert_func) and $new_mate->can($includes_func) and not $new_mate->$includes_func($self)
!ll&&!rl&&r
061
$new_mate->can($insert_func) and $new_mate->can($includes_func)
!ll&&!rl&&r
007
MC/DC33%1/3
$new_mate->can($insert_func) and $new_mate->can($includes_func) and not $new_mate->$includes_func($self)
$new_mate->can($insert_func) $new_mate->can($includes_func) not $new_mate->$includes_func($self)
Truth table50%2/4
A and B and not(C)
A= $new_mate->can($insert_func)B= $new_mate->can($includes_func)C= not $new_mate->$includes_func($self)
ABCresult
0XX0
10X0
1100
1111
884 $new_mate->can($includes_func) &&
885 !$new_mate->$includes_func($self) ) {
886 1 $new_mate->$insert_func($self);
887 }
888 }
889 }
890 }
891 }
892  
893 =item `hash' attributes set
894  
895 =cut
896  
897 3 ▶sub _set_X_hash {
Subroutine _set_X_hash: called CC=28 SCAR=44.5
898 3 my $self = shift;
899 3 my $base_type = shift;
900 3 my $companion = shift;
901 3 my $X = shift;
902 3 my $class = ref $self;
903  
904 # Shortcut to avoid penalty when simply setting to a new container
905 3 ▶ if (@_ == 1 and !$companion and ref $_[0] eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not $companion and ref $_[0] eq "HASH") 0 3 3
Condition66%4/6
@_ == 1 and not $companion and ref $_[0] eq "HASH"
!ll&&!rl&&r
210
@_ == 1 and not $companion
!ll&&!rl&&r
201
MC/DC0%0/3
@_ == 1 and not $companion and ref $_[0] eq "HASH"
@_ == 1 not $companion ref $_[0] eq "HASH"
Truth table50%2/4
A and not(B) and C
A= @_ == 1B= not $companionC= ref $_[0] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
906 0 delete $self->{$X}; # make sure it's not tied - 5.8.0 bug
907 0 ▶ if (my $checkit = \$check{$class}->{$X}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $checkit = \$Class::Tangram::check{$class}{$X}) 0 0 0
908 # There's a check function! Use it!
909 0 $ {$checkit}->(\($_[0]));
910 }
911 0 return $self->{$X} = $_[0];
912 }
913  
914 3 my %ncc;
915 3 my $n = 0;
916 4 ▶ my $ins = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
917 4 my $item = shift;
918 4 ▶ if (blessed $item and
Branch: 1 branch
BranchTrueFalseTotal
if (&blessed($item) and $item->can(my $meth = "${X}_hek")) { } 3 1 4
Condition66%2/3
&blessed($item) and $item->can(my $meth = "${X}_hek")
!ll&&!rl&&r
013
MC/DC50%1/2
&blessed($item) and $item->can(my $meth = "${X}_hek")
&blessed($item) $item->can(my $meth = "${X}_hek")
Truth table66%2/3
A and B
A= &blessed($item)B= $item->can(my $meth = "${X}_hek")
ABresult
0X0
100
111
919 $item->can(my $meth = "${X}_hek")) {
920 3 $ncc{$item->$meth} = $item;
921 } else {
922 1 $ncc{"".$n++} = $item;
923 }
924 3 };
925  
926 3 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 3 10
927 7 my ($value) = (shift @_);
928 7 ▶ if (blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&blessed($value)) { } 4 3 7
929 4 ▶ if ($value->isa("Set::Object")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->isa('Set::Object')) { } 0 4 4
930 0 $ins->($_) foreach $value->members();
931 } else {
932 4 $ins->($value);
933 }
934 } else {
935 3 my $ref = ref $value;
936 3 ▶ if ($ref) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref) { } 0 3 3
elsif (defined ish_int($value)) { } 0 3 3
937 0 ▶ if ($ref eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 0 0 0
elsif ($ref eq 'HASH') { } 0 0 0
938 0 $ins->($_) foreach @$value;
939 } elsif ($ref eq "HASH") {
940 0 ▶ while (my ($k, $v) = each %$value) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$value) 0 0 0
941 0 $ncc{$k} = $v;
942 }
943 }
944 } elsif (defined(ish_int($value))) {
945 # hmmf. A number? Well, just put it on the end.
946 # exact convention to be determined later
947 0 $ins->(shift @_);
948 } else {
949 # a plain hash key
950 3 $ncc{$value} = (shift @_);
951 }
952 }
953 }
954  
955 3 ▶ my $old = $self->{$X} || {};
Condition50%1/2
$self->{$X} || {}
l!l
30
MC/DC0%0/1
$self->{$X} || {}
$self->{$X}
Truth table50%1/2
A
A= $self->{$X}
Aresult
11
00
956 3 ▶ if (my $checkit = \$check{$class}->{$X}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $checkit = \$Class::Tangram::check{$class}{$X}) 3 0 3
957 # There's a check function! Use it!
958 3 $ {$checkit}->(\\%ncc);
959 }
960 3 $self->{$X} = \%ncc;
961  
962 3 ▶ if ($companion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($companion) 2 1 3
963 # ordering is ignored for arrays when it comes to
964 # companions
965 2 my $set = Set::Object->new(values %$old);
966 2 my $ncc = Set::Object->new(values %ncc);
967  
968 # I love Set::Object, it should be a builtin data type :-)
969 2 my $gone = $set - $ncc;
970 2 my $new = $ncc - $set;
971  
972 2 my $includes_func = $companion."_includes";
973  
974 2 ▶ if ($gone->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gone->size) 0 2 2
975 0 my $remove_func = $companion."_remove";
976 0 for my $gonner ($gone->members) {
977 0 ▶ if ($gonner->can($remove_func) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($gonner->can($remove_func) and $gonner->can($includes_func) and $gonner->$includes_func($self)) 0 0 0
Condition0%0/6
$gonner->can($remove_func) and $gonner->can($includes_func) and $gonner->$includes_func($self)
!ll&&!rl&&r
000
$gonner->can($remove_func) and $gonner->can($includes_func)
!ll&&!rl&&r
000
MC/DC0%0/3
$gonner->can($remove_func) and $gonner->can($includes_func) and $gonner->$includes_func($self)
$gonner->can($remove_func) $gonner->can($includes_func) $gonner->$includes_func($self)
Truth table0%0/4
A and B and C
A= $gonner->can($remove_func)B= $gonner->can($includes_func)C= $gonner->$includes_func($self)
ABCresult
0XX0
10X0
1100
1111
978 $gonner->can($includes_func) &&
979 $gonner->$includes_func($self)) {
980 0 $gonner->$remove_func($self);
981 }
982 }
983 }
984  
985 2 ▶ if ($new->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new->size) 1 1 2
986 1 my $insert_func = $companion."_insert";
987 1 for my $new_mate ($new->members) {
988 3 ▶ if ($new_mate->can($insert_func) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($new_mate->can($insert_func) and $new_mate->can($includes_func) and not $new_mate->$includes_func($self)) 3 0 3
Condition33%2/6
$new_mate->can($insert_func) and $new_mate->can($includes_func) and not $new_mate->$includes_func($self)
!ll&&!rl&&r
003
$new_mate->can($insert_func) and $new_mate->can($includes_func)
!ll&&!rl&&r
003
MC/DC0%0/3
$new_mate->can($insert_func) and $new_mate->can($includes_func) and not $new_mate->$includes_func($self)
$new_mate->can($insert_func) $new_mate->can($includes_func) not $new_mate->$includes_func($self)
Truth table25%1/4
A and B and not(C)
A= $new_mate->can($insert_func)B= $new_mate->can($includes_func)C= not $new_mate->$includes_func($self)
ABCresult
0XX0
10X0
1100
1111
989 $new_mate->can($includes_func) &&
990 !$new_mate->$includes_func($self) ) {
991 3 $new_mate->$insert_func($self);
992 }
993 }
994 }
995 }
996 }
997  
998 =back
999  
1000 =item $instance->attribute_includes(@objects)
1001  
1002 Returns true if all of the objects, or object => value pairs, are
1003 present in the container.
1004  
1005 =cut
1006  
1007 42 ▶sub _includes_X_set {
Subroutine _includes_X_set: called CC=9 SCAR=25.4
1008 42 my $self = shift;
1009 42 my $X = shift;
1010 42 my $getter = "get_$X";
1011 42 ▶ my $a = $self->$getter || Set::Object->new();
Condition33%1/3
$self->$getter || 'Set::Object'->new
l!l&&r!l&&!r
4200
MC/DC0%0/2
$self->$getter || 'Set::Object'->new
$self->$getter 'Set::Object'->new
Truth table33%1/3
A || B
A= $self->$getterB= 'Set::Object'->new
ABresult
1X1
011
000
1012  
1013 42 my $all_there = 1;
1014 42 my $item;
1015 42 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 42 23 65
1016 42 ▶ if (blessed($item = shift) or reftype($item)) {
Branch: 2 branches
BranchTrueFalseTotal
if (&blessed($item = shift()) or &reftype($item)) { } 42 0 42
elsif (defined(my $x = ish_int($item))) { } 0 0 0
Condition33%1/3
&blessed($item = shift()) or &reftype($item)
l!l&&r!l&&!r
4200
MC/DC0%0/2
&blessed($item = shift()) or &reftype($item)
&blessed($item = shift()) &reftype($item)
Truth table33%1/3
A or B
A= &blessed($item = shift())B= &reftype($item)
ABresult
1X1
011
000
1017 42 ▶ $all_there = 0 unless $a->includes($item);
Branch: 1 branch
BranchTrueFalseTotal
unless $a->includes($item) 19 23 42
1018 } elsif (defined(my $x = ish_int($item))) {
1019 0 ▶ $all_there = 0 if $x > $a->size;
Branch: 1 branch
BranchTrueFalseTotal
if $x > $a->size 0 0 0
1020 } else {
1021 0 carp("Searched for non-reference `$item' in set");
1022 }
1023 42 ▶ last unless $all_there;
Branch: 1 branch
BranchTrueFalseTotal
unless $all_there 19 23 42
1024 }
1025 42 return $all_there;
1026 }
1027  
1028 5 ▶sub _includes_X_ref {
Subroutine _includes_X_ref: called CC=7 SCAR=20.8
1029 5 my $self = shift;
1030 5 my $X = shift;
1031 5 my $getter = "get_$X";
1032  
1033 5 my $all_there = 1;
1034 5 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 2 7
1035 5 ▶ if (blessed(my $item = shift)) {
Branch: 2 branches
BranchTrueFalseTotal
if (&blessed(my $item = shift())) { } 5 0 5
elsif (defined(my $x = ish_int($item))) { } 0 0 0
1036 5 ▶ $all_there = 0
Branch: 1 branch
BranchTrueFalseTotal
unless &refaddr(scalar $self->$getter) == &refaddr($item) 3 2 5
1037 unless (refaddr($self->$getter) == refaddr($item));
1038 } elsif (defined(my $x = ish_int($item))) {
1039 0 ▶ $all_there = 0 if $x;
Branch: 1 branch
BranchTrueFalseTotal
if $x 0 0 0
1040 }
1041 5 ▶ last unless $all_there;
Branch: 1 branch
BranchTrueFalseTotal
unless $all_there 3 2 5
1042 }
1043 5 return $all_there;
1044 }
1045  
1046 7 ▶sub _includes_X_array {
Subroutine _includes_X_array: called CC=12 SCAR=33.1
1047 7 my $self = shift;
1048 7 my $X = shift;
1049 7 my $getter = "get_$X";
1050 7 ▶ my $a = $self->$getter || [];
Condition50%1/2
$self->$getter || []
l!l
70
MC/DC0%0/1
$self->$getter || []
$self->$getter
Truth table50%1/2
A
A= $self->$getter
Aresult
11
00
1051  
1052 7 my $all_there = 1;
1053 7 my $members;
1054 7 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 2 9
1055 7 ▶ if (blessed(my $item = shift)) {
Branch: 2 branches
BranchTrueFalseTotal
if (&blessed(my $item = shift())) { } 7 0 7
elsif (defined(my $x = ish_int($item))) { } 0 0 0
1056 # includes without a key, d'oh! convert to set
1057 7 ▶ $members ||= Set::Object->new(@$a);
Condition33%1/3
$members ||= "Set::Object"->new(@$a)
l!l&&r!l&&!r
070
MC/DC0%0/2
$members ||= "Set::Object"->new(@$a)
$members "Set::Object"->new(@$a)
Truth table33%1/3
A ||= B
A= $membersB= "Set::Object"->new(@$a)
ABresult
1X1
011
000
1058 7 ▶ $all_there = 0 unless $members->includes($item);
Branch: 1 branch
BranchTrueFalseTotal
unless $members->includes($item) 5 2 7
1059 } elsif (defined(my $x = ish_int($item))) {
1060 0 ▶ $all_there = 0, last unless ($x >= 0 && $x < @$a);
Branch: 1 branch
BranchTrueFalseTotal
unless $x >= 0 and $x < @$a 0 0 0
Condition0%0/3
$x >= 0 and $x < @$a
!ll&&!rl&&r
000
MC/DC0%0/2
$x >= 0 and $x < @$a
$x >= 0 $x < @$a
Truth table0%0/3
A and B
A= $x >= 0B= $x < @$a
ABresult
0X0
100
111
1061 0 ▶ if (blessed($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (&blessed($_[0])) 0 0 0
1062 0 $item = shift;
1063 0 ▶ $all_there = 0 unless (refaddr($a->[$x]) == refaddr($item));
Branch: 1 branch
BranchTrueFalseTotal
unless &refaddr($a->[$x]) == &refaddr($item) 0 0 0
1064 }
1065 }
1066 7 ▶ last unless $all_there;
Branch: 1 branch
BranchTrueFalseTotal
unless $all_there 5 2 7
1067 }
1068 7 return $all_there;
1069 }
1070  
1071 4 ▶sub _includes_X_hash {
Subroutine _includes_X_hash: called CC=12 SCAR=35.3
1072 4 my $self = shift;
1073 4 my $X = shift;
1074 4 my $getter = "get_$X";
1075 4 ▶ my $a = $self->$getter || {};
Condition50%1/2
$self->$getter || {}
l!l
40
MC/DC0%0/1
$self->$getter || {}
$self->$getter
Truth table50%1/2
A
A= $self->$getter
Aresult
11
00
1076  
1077 4 my $all_there = 1;
1078 4 my $members;
1079 4 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 4 8
1080 4 ▶ if (blessed(my $item = shift)) {
Branch: 3 branches
BranchTrueFalseTotal
if (&blessed(my $item = shift())) { } 4 0 4
elsif (defined(my $x = ish_int($item))) { } 0 0 0
elsif (not ref $item) { } 0 0 0
1081 # includes without a key, d'oh! convert to set
1082 4 ▶ $members ||= Set::Object->new(values %$a);
Condition33%1/3
$members ||= "Set::Object"->new(values %$a)
l!l&&r!l&&!r
040
MC/DC0%0/2
$members ||= "Set::Object"->new(values %$a)
$members "Set::Object"->new(values %$a)
Truth table33%1/3
A ||= B
A= $membersB= "Set::Object"->new(values %$a)
ABresult
1X1
011
000
1083 4 ▶ $all_there = 0 unless $members->includes($item);
Branch: 1 branch
BranchTrueFalseTotal
unless $members->includes($item) 0 4 4
1084 } elsif (defined(my $x = ish_int($item))) {
1085 # lookup by index, ignore key for now
1086 next
1087 0 } elsif (!ref($item)) {
1088 # lookup by hash key
1089 0 ▶ $all_there = 0, last unless exists $a->{$item};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $a->{$item} 0 0 0
1090 0 ▶ if (blessed($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (&blessed($_[0])) 0 0 0
1091 0 my $key;
1092 0 ($key, $item) = ($item, shift);
1093 0 ▶ $all_there = 0 unless refaddr($a->{$key}) == refaddr($item);
Branch: 1 branch
BranchTrueFalseTotal
unless &refaddr($a->{$key}) == &refaddr($item) 0 0 0
1094 }
1095 }
1096 4 ▶ last unless $all_there;
Branch: 1 branch
BranchTrueFalseTotal
unless $all_there 0 4 4
1097 }
1098 4 return $all_there;
1099 }
1100  
1101  
1102 =item $instance->attribute_insert([key] => $object, [...])
1103  
1104 Inserts all of the items into the collection.
1105  
1106 Where possible, if the collection type can avoid a collision (perhaps
1107 by duplicating an entry for a key or inserting a slot into an ordered
1108 list), then such action is taken.
1109  
1110 If you're inserting a list of objects into an array by number, ensure
1111 that you list the keys in order, unless you know what you're doing.
1112  
1113 eg
1114  
1115 $obj->myarray_insert( 1 => $obj1, 2 => $obj2, 1 => $obj3 )
1116  
1117 will yield
1118  
1119 $obj->myarray() == ( $obj3, $obj1, $obj2 );
1120  
1121 Empty slots are shifted along with the rest of them.
1122  
1123 =cut
1124  
1125 3 ▶sub _insert_X_ref {
Subroutine _insert_X_ref: called CC=2 SCAR=7.0
1126 3 my $self = shift;
1127 3 my $X = shift;
1128 3 my $setter = "set_$X";
1129 3 my $getter = "get_$X";
1130 3 ▶ return $self->$setter($_[0] || scalar($self->$getter));
Condition50%1/2
$_[0] || scalar $self->$getter
l!l
30
MC/DC0%0/1
$_[0] || scalar $self->$getter
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
1131 }
1132 22 ▶sub _insert_X_set {
Subroutine _insert_X_set: called CC=1 SCAR=0.0
1133 22 my $self = shift;
1134 22 my $X = shift;
1135 22 my $setter = "set_$X";
1136 22 my $getter = "get_$X";
1137 22 my @new = (scalar($self->$getter), @_);
1138 22 return $self->$setter(@new);
1139 }
1140 6 ▶sub _insert_X_array {
Subroutine _insert_X_array: called CC=7 SCAR=25.3
1141 6 my $self = shift;
1142 6 my $X = shift;
1143 6 my $setter = "set_$X";
1144 6 my $getter = "get_$X";
1145  
1146 6 my @ncc = $self->$getter();
1147 6 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 6 12
1148 6 my ($value) = (shift @_);
1149 6 ▶ if (blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&blessed($value)) { } 6 0 6
1150 6 ▶ if ($value->isa("Set::Object")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->isa('Set::Object')) { } 0 6 6
1151 0 push @ncc, $value->members();
1152 } else {
1153 6 push @ncc, $value;
1154 }
1155 } else {
1156 0 my $ref = ref $value;
1157 0 ▶ if ($ref eq "ARRAY") {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 0 0 0
elsif ($ref eq 'HASH') { } 0 0 0
elsif (defined ish_int($value)) { } 0 0 0
1158 0 push @ncc, @$value;
1159 } elsif ($ref eq "HASH") {
1160 0 push @ncc, values %$value;
1161 } elsif (defined(ish_int($value))) {
1162 # FIXME - what about $object->insert(7 => \@obj) ?
1163 0 @ncc = (@ncc[0..$value-1], (shift @_),
1164 @ncc[$value..$#ncc]);
1165 } else {
1166 # some other type of key, ignore it
1167 }
1168 }
1169 }
1170 6 return $self->$setter(@ncc);
1171 }
1172 2 ▶sub _insert_X_hash {
Subroutine _insert_X_hash: called CC=1 SCAR=0.0
1173 2 my $self = shift;
1174 2 my $X = shift;
1175 2 my $setter = "set_$X";
1176 2 my $getter = "${X}_pairs";
1177 2 return $self->$setter($self->$getter, @_);
1178 }
1179  
1180  
1181 =item $instance->attribute_replace([key] => $object, [...])
1182  
1183 "Replace" is, for the most part, identical to "insert". However, if
1184 collisions occur (whatever that means for the collection type you are
1185 inserting to), then the target will be replaced, no duplications of
1186 elements will occur in collection types supporting duplicates.
1187  
1188 =cut
1189  
1190 0 ▶sub _replace_X_ref {
Subroutine _replace_X_ref: not called CC=1 SCAR=6.9
1191 0 my $self = shift;
1192 0 my $X = shift;
1193 0 my $getter = "get_$X";
1194 0 my $setter = "set_$X";
1195 0 return $self->$setter((@_, scalar($self->$getter))[0]);
1196 }
1197 0 ▶sub _replace_X_set {
Subroutine _replace_X_set: not called CC=1 SCAR=6.9
1198 0 my $self = shift;
1199 0 my $X = shift;
1200 0 my $setter = "set_$X";
1201 0 my $getter = "get_$X";
1202 0 return $self->$setter(scalar($self->$getter), @_);
1203 }
1204 0 ▶sub _replace_X_array {
Subroutine _replace_X_array: not called CC=1 SCAR=6.9
1205 0 my $self = shift;
1206 0 my $X = shift;
1207 0 my $setter = "set_$X";
1208 0 my $getter = "get_$X";
1209 0 return $self->$setter(scalar($self->$getter), @_);
1210 }
1211 0 ▶sub _replace_X_hash {
Subroutine _replace_X_hash: not called CC=1 SCAR=6.9
1212 0 my $self = shift;
1213 0 my $X = shift;
1214 0 my $setter = "set_$X";
1215 0 my $getter = "${X}_pairs";
1216 0 return $self->$setter(scalar($self->$getter), @_);
1217 }
1218  
1219  
1220 =item $instance->attribute_pairs
1221  
1222 =cut
1223  
1224 0 ▶sub _pairs_X_ref {
Subroutine _pairs_X_ref: not called CC=1 SCAR=6.9
1225 0 my $self = shift;
1226 0 my $X = shift;
1227 0 my $getter = "get_$X";
1228 0 return map { ("" => $_) } $self->$getter(@_);
1229 }
1230 0 ▶sub _pairs_X_set {
Subroutine _pairs_X_set: not called CC=1 SCAR=6.9
1231 0 my $self = shift;
1232 0 my $X = shift;
1233 0 my $getter = "get_$X";
1234 0 return map { ("" => $_) } $self->$getter(@_);
1235 }
1236 0 ▶sub _pairs_X_array {
Subroutine _pairs_X_array: not called CC=1 SCAR=6.9
1237 0 my $self = shift;
1238 0 my $X = shift;
1239 0 my $setter = "set_$X";
1240 0 my $getter = "get_$X";
1241 0 my $n = 0;
1242 0 return map { ($n++ => $_) } $self->$getter(@_);
1243 }
1244 4 ▶sub _pairs_X_hash {
Subroutine _pairs_X_hash: called CC=1 SCAR=0.0
1245 4 my $self = shift;
1246 4 my $X = shift;
1247 4 my $getter = "get_$X";
1248 4 return %{$self->$getter}
1249 }
1250  
1251 =item $instance->attribute_size
1252  
1253 FETCHSIZE
1254  
1255 =cut
1256  
1257 0 ▶sub _size_X_ref {
Subroutine _size_X_ref: not called CC=2 SCAR=17.9
1258 0 my $self = shift;
1259 0 my $X = shift;
1260 0 my $getter = "get_$X";
1261 0 ▶ return ($self->$getter ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->$getter ? : 0 0 0
1262 }
1263 9 ▶sub _size_X_set {
Subroutine _size_X_set: called CC=1 SCAR=0.0
1264 9 my $self = shift;
1265 9 my $X = shift;
1266 9 my $getter = "get_$X";
1267 9 return $self->$getter->size();
1268 }
1269 0 ▶sub _size_X_array {
Subroutine _size_X_array: not called CC=1 SCAR=6.9
1270 0 my $self = shift;
1271 0 my $X = shift;
1272 0 my $getter = "get_$X";
1273 0 return scalar(@{$self->$getter});
1274 }
1275 0 ▶sub _size_X_hash {
Subroutine _size_X_hash: not called CC=1 SCAR=6.9
1276 0 my $self = shift;
1277 0 my $X = shift;
1278 0 my $getter = "get_$X";
1279 0 return scalar(keys %{$self->$getter});
1280 }
1281  
1282  
1283 =item $instance->attribute_clear
1284  
1285 Empties a collection
1286  
1287 =cut
1288  
1289 3 ▶sub _clear_X_ref {
Subroutine _clear_X_ref: called CC=1 SCAR=0.0
1290 3 my $self = shift;
1291 3 my $X = shift;
1292 3 my $setter = "set_$X";
1293 3 return ($self->$setter());
1294 }
1295 3 ▶sub _clear_X_set { _clear_X_ref(@_) }
Subroutine _clear_X_set: called CC=1 SCAR=0.0
1296 0 ▶sub _clear_X_array { _clear_X_ref(@_) }
Subroutine _clear_X_array: not called CC=1 SCAR=6.9
1297 0 ▶sub _clear_X_hash { _clear_X_ref(@_) }
Subroutine _clear_X_hash: not called CC=1 SCAR=6.9
1298  
1299 =item $instance->attribute_push
1300  
1301 Place an element on the end of a collection; identical to foo_insert
1302 without an index.
1303  
1304 =cut
1305  
1306 0 ▶sub _push_X_ref { _insert_X_ref(@_) }
Subroutine _push_X_ref: not called CC=1 SCAR=6.9
1307 1 ▶sub _push_X_set { _insert_X_set(@_) }
Subroutine _push_X_set: called CC=1 SCAR=0.0
1308 1 ▶sub _push_X_array { _insert_X_array(@_) }
Subroutine _push_X_array: called CC=1 SCAR=0.0
1309 0 ▶sub _push_X_hash { _insert_X_hash(@_) }
Subroutine _push_X_hash: not called CC=1 SCAR=6.9
1310  
1311 =item $instance->attribute_unshift
1312  
1313 Place an element on the end of a collection; identical to foo_insert
1314 without an index.
1315  
1316 =cut
1317  
1318 0 ▶sub _unshift_X_ref { _insert_X_ref(@_) }
Subroutine _unshift_X_ref: not called CC=1 SCAR=6.9
1319 0 ▶sub _unshift_X_set { _insert_X_set(@_) }
Subroutine _unshift_X_set: not called CC=1 SCAR=6.9
1320 0 ▶sub _unshift_X_array {
Subroutine _unshift_X_array: not called CC=1 SCAR=6.9
1321 0 my $self = shift;
1322 0 my $X = shift;
1323 0 my $getter = "get_$X";
1324 0 my @ncc = $self->$getter();
1325 0 my $setter = "set_$X";
1326 0 return $self->$setter(@_, @ncc);
1327 }
1328 0 ▶sub _unshift_X_hash { _insert_X_hash(@_) }
Subroutine _unshift_X_hash: not called CC=1 SCAR=6.9
1329  
1330 =item $instance->attribute_pop
1331  
1332 Returns the last element in a collection, and deletes that item from
1333 the collection, but not necessarily in that order. No parameters are
1334 accepted.
1335  
1336 =cut
1337  
1338 0 ▶sub _pop_X_ref {
Subroutine _pop_X_ref: not called CC=2 SCAR=17.9
1339 0 my $self = shift;
1340 0 my $X = shift;
1341 0 my $setter = "set_$X";
1342 0 my $getter = "get_$X";
1343 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
1344 0 my @rv = ($self->$getter());
1345 0 $self->$setter();
1346 0 return @rv;
1347 } else {
1348 0 my $rv = $self->$getter();
1349 0 $self->$setter();
1350 0 return $rv;
1351 }
1352 }
1353  
1354 0 ▶sub _pop_X_set {
Subroutine _pop_X_set: not called CC=3 SCAR=24.8
1355 0 my $self = shift;
1356 0 my $X = shift;
1357 0 my $getter = "get_$X";
1358 # sets don't have an order, so just delete any member
1359 0 ▶ if (my $val = $self->$getter(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = $self->$getter(0)) { } 0 0 0
1360 0 my $toaster = "${X}_remove";
1361 0 $self->$toaster($val);
1362 0 return $val;
1363 } else {
1364 0 ▶ return (wantarray ? () : undef);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1365 }
1366 }
1367 0 ▶sub _pop_X_array {
Subroutine _pop_X_array: not called CC=1 SCAR=6.9
1368 0 my $self = shift;
1369 0 my $X = shift;
1370 0 my $getter = "get_$X";
1371 0 my @ncc = $self->$getter();
1372 0 my $rv = pop @ncc;
1373 0 my $setter = "set_$X";
1374 0 $self->$setter(@ncc);
1375 0 return $rv;
1376 }
1377 0 ▶sub _pop_X_hash {
Subroutine _pop_X_hash: not called CC=2 SCAR=17.9
1378 0 my $self = shift;
1379 0 my $X = shift;
1380 0 my $getter = "get_$X";
1381 0 my $killer = "${X}_remove";
1382 0 my $hashref = $self->$getter();
1383 0 my ($key, $gonner) = (each %$hashref);
1384 0 ▶ $self->$killer($key => $gonner) if defined $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key 0 0 0
1385 0 return $gonner;
1386 }
1387  
1388 =item $instance->attribute_shift
1389  
1390 Remove an element on the beginning of a collection, and return it
1391  
1392 =cut
1393  
1394 0 ▶sub _shift_X_ref { _pop_X_ref(@_) }
Subroutine _shift_X_ref: not called CC=1 SCAR=6.9
1395 0 ▶sub _shift_X_set { _pop_X_set(@_) }
Subroutine _shift_X_set: not called CC=1 SCAR=6.9
1396 0 ▶sub _shift_X_array {
Subroutine _shift_X_array: not called CC=1 SCAR=6.9
1397 0 my $self = shift;
1398 0 my $X = shift;
1399 0 my $getter = "get_$X";
1400 0 my @ncc = $self->$getter();
1401 0 my $rv = shift @ncc;
1402 0 my $setter = "set_$X";
1403 0 $self->$setter(@ncc);
1404 0 return $rv;
1405 }
1406 0 ▶sub _shift_X_hash { _pop_X_hash(@_) }
Subroutine _shift_X_hash: not called CC=1 SCAR=6.9
1407  
1408  
1409 =item $instance->attribute_splice($offset, $length, @objects)
1410  
1411 Pretends that the collection is an array and splices it.
1412  
1413 =cut
1414  
1415 0 ▶sub _splice_X_ref { _splice_X_array(@_) }
Subroutine _splice_X_ref: not called CC=1 SCAR=6.9
1416 0 ▶sub _splice_X_set { _splice_X_array(@_) }
Subroutine _splice_X_set: not called CC=1 SCAR=6.9
1417 0 ▶sub _splice_X_array {
Subroutine _splice_X_array: not called CC=2 SCAR=17.9
1418 0 my $self = shift;
1419 0 my $X = shift;
1420 0 my $getter = "get_${X}";
1421 0 my $setter = "set_${X}";
1422 0 my @list = $self->$getter();
1423 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
1424 0 my @rv = splice @list, @_;
1425 0 $self->$setter(@list);
1426 0 return @rv;
1427 } else {
1428 0 my $rv = splice @list, @_;
1429 0 $self->$setter(@list);
1430 0 return $rv;
1431 }
1432 }
1433 0 ▶sub _splice_X_hash {
Subroutine _splice_X_hash: not called CC=2 SCAR=17.9
1434 0 my $self = shift;
1435 0 my $X = shift;
1436 0 my $getter = "${X}_pairs";
1437 0 my $setter = "set_${X}";
1438 0 my @list = $self->$getter();
1439 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
1440 0 my @rv = splice @list, @_;
1441 0 $self->$setter(@list);
1442 0 return @rv;
1443 } else {
1444 0 my $rv = splice @list, @_;
1445 0 $self->$setter(@list);
1446 0 return $rv;
1447 }
1448 }
1449  
1450 =item $instance->attribute_remove(@objects)
1451  
1452 translates logically to a search for that item or index, followed by a
1453 delete
1454  
1455 This suite of functions applies to attributes that are sets (C<iset>
1456 or C<set>). It could in theory also apply generally to all
1457 collections - ie also arrays (C<iarray> or C<array>), and hashes
1458 (C<hash>, C<ihash>).
1459  
1460 All of these modifications build a new container, then call
1461 $object->set_attribute($container)
1462  
1463 It is up to the set_attribute() function to update all related
1464 classes.
1465  
1466 =cut
1467  
1468 16 ▶sub _listify {
Subroutine _listify: called CC=5 SCAR=23.4
1469 16 ▶ map { (blessed($_)
Branch: 4 branches
BranchTrueFalseTotal
&blessed($_) ? : 16 0 16
$_->isa('Set::Object') ? : 0 16 16
ref $_ eq 'HASH' ? : 0 0 0
ref $_ eq 'ARRAY' ? : 0 0 0
1470 ? (
1471 $_->isa("Set::Object")
1472 ? $_->members()
1473 : $_
1474 )
1475 : (ref $_ eq "HASH"
1476 ? (keys %$_)
1477 : (ref $_ eq "ARRAY"
1478 ? @$_
1479 : ()))) } @_
1480 }
1481  
1482 1 ▶sub _remove_X_ref {
Subroutine _remove_X_ref: called CC=1 SCAR=0.0
1483 1 my $self = shift;
1484 1 my $X = shift;
1485 1 my $setter = "set_$X";
1486 1 my $getter = "get_$X";
1487 1 my $remove = Set::Object->new(_listify(@_));
1488 1 return $self->$setter(grep { !$remove->includes($_) }
1489 $self->$getter);
1490 }
1491 15 ▶sub _remove_X_set {
Subroutine _remove_X_set: called CC=1 SCAR=0.0
1492 15 my $self = shift;
1493 15 my $X = shift;
1494 15 my $setter = "set_$X";
1495 15 my $getter = "get_$X";
1496 15 my $remove = Set::Object->new(_listify(@_));
1497 15 return $self->$setter(grep { !$remove->includes($_) }
1498 $self->$getter);
1499 }
1500  
1501 0 ▶sub _remove_X_array {
Subroutine _remove_X_array: not called CC=8 SCAR=42.8
1502 0 my $self = shift;
1503 0 my $X = shift;
1504 0 my $setter = "set_$X";
1505 0 my $getter = "get_$X";
1506 0 my @new = ($self->$getter);
1507 0 my %gone;
1508 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
1509 0 my $item = shift;
1510 0 ▶ if (blessed($item)) {
Branch: 2 branches
BranchTrueFalseTotal
if (&blessed($item)) { } 0 0 0
elsif (defined ish_int($item)) { } 0 0 0
1511 0 ▶ for (my $i = 0; $i < @new; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @new) 0 0 0
1512 0 ▶ $gone{$i} = 1, last
Branch: 1 branch
BranchTrueFalseTotal
if &refaddr($item) == &refaddr($new[$i]) 0 0 0
1513 if (refaddr($item) == refaddr($new[$i]));
1514 }
1515 } elsif (defined(ish_int($item))) {
1516 0 $gone{$item} = 1;
1517 } else {
1518 0 ▶ for (my $i = 0; $i < @new; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @new) 0 0 0
1519 0 ▶ $gone{$i} = 1, last unless $gone{$i};
Branch: 1 branch
BranchTrueFalseTotal
unless $gone{$i} 0 0 0
1520 }
1521 }
1522 }
1523 0 delete @new[keys %gone];
1524 0 return $self->$setter(@new);
1525 }
1526  
1527 0 ▶sub _remove_X_hash {
Subroutine _remove_X_hash: not called CC=7 SCAR=40.3
1528 0 my $self = shift;
1529 0 my $X = shift;
1530 0 my $getter = "${X}_pairs";
1531 0 my $setter = "set_$X";
1532 0 my %new = $self->$getter();
1533 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
1534 0 my $item = shift;
1535  
1536 0 ▶ if (blessed($item)) {
Branch: 2 branches
BranchTrueFalseTotal
if (&blessed($item)) { } 0 0 0
elsif (&blessed($_[0])) { } 0 0 0
1537 0 ▶ while (my ($k, $v) = each %new) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %new) 0 0 0
1538 0 ▶ $item = $k if (refaddr($item) == refaddr($v));
Branch: 1 branch
BranchTrueFalseTotal
if &refaddr($item) == &refaddr($v) 0 0 0
1539 }
1540 } elsif (blessed($_[0])) {
1541 # FIXME - only delete if the values match, perhaps?
1542 }
1543  
1544 0 ▶ ($item) = next %new if (defined(ish_int($item)));
Branch: 1 branch
BranchTrueFalseTotal
if defined ish_int($item) 0 0 0
1545  
1546 0 delete $new{$item};
1547 }
1548 0 return $self->$setter(%new);
1549 }
1550  
1551  
1552  
1553  
1554 =back
1555  
1556 B<Note:> The above functions can be overridden, but they may not be
1557 called with the C<$self-E<gt>SUPER::> superclass chaining method.
1558 This is because they are not defined within the scope of
1559 Class::Tangram, only your package.
1560  
1561 =cut
1562  
1563 =head1 ATTRIBUTE TYPE CHECKING
1564  
1565 Class::Tangram provides type checking of attributes when attributes
1566 are set - either using the default C<set_attribute> functions, or
1567 created via the C<new> constructor.
1568  
1569 The checking has default behaviour for each type of attribute (see
1570 L<Default Type Checking>), and can be extended arbitrarily via a
1571 per-attribute C<check_func>, described below. Critical attributes can
1572 be marked as such with the C<required> flag.
1573  
1574 The specification of this type checking is placed in the class schema,
1575 in the per-attribute B<options hash>. This is a Class::Tangram
1576 extension to the Tangram schema structure.
1577  
1578 =over
1579  
1580 =item check_func
1581  
1582 A function that is called with a B<reference> to the new value in
1583 C<$_[0]>. It should call C<die()> if the value is bad. Note that
1584 this check_func will never be passed an undefined value; this is
1585 covered by the "required" option, below.
1586  
1587 In the example schema (above), the attribute C<segments> has a
1588 C<check_func> that prevents setting the value to anything greater than
1589 30. Note that it does not prevent you from setting the value to
1590 something that is not an integer; if you define a C<check_func>, it
1591 replaces the default.
1592  
1593 =item required
1594  
1595 If this option is set to a true value, then the attribute must be set
1596 to a true value to pass type checking. For string attributes, this
1597 means that the string must be defined and non-empty (so "0" is true).
1598 For other attribute types, the normal Perl definition of logical truth
1599 is used.
1600  
1601 If the required option is defined but logically false, (ie "" or 0),
1602 then the attribute must also be defined, but may be set to a logically
1603 false value.
1604  
1605 If the required option is undefined, then the attribute may be set to
1606 an undefined value.
1607  
1608 For integration with tangram, the C<new()> function has a special
1609 hack; if it is being invoked from within Tangram, then the required
1610 test is skipped.
1611  
1612 =back
1613  
1614 =head2 Other per-attribute options
1615  
1616 Any of the following options may be inserted into the per-attribute
1617 B<options hash>:
1618  
1619 =over
1620  
1621 =item init_default
1622  
1623 This value specifies the default value of the attribute when
1624 it is created with C<new()>. It is a scalar value, it is
1625 copied to the fresh object. If it is a code reference, that
1626 code reference is called and its return value inserted into
1627 the attribute. If it is an ARRAY or HASH reference, then
1628 that array or hash is COPIED into the attribute.
1629  
1630 =item destroy_func
1631  
1632 If anything special needs to happen to this attribute before the
1633 object is destroyed (or when someone calls
1634 C<$object-E<gt>clear_refs()>), then define this. It is called as
1635 C<$sub-E<gt>($object, "attribute")>.
1636  
1637 =back
1638  
1639 =head2 Default Type Checking
1640  
1641 Default type checking s
1642  
1643 =cut
1644  
1645  
1646 =over
1647  
1648 =item check_X (\$value)
1649  
1650 This series of internal functions are built-in C<check_func> functions
1651 defined for all of the standard Tangram attribute types.
1652  
1653 =over
1654  
1655 =item check_string
1656  
1657 checks that the supplied value is less than 255 characters long.
1658  
1659 =cut
1660  
1661 82 ▶sub check_string {
Subroutine check_string: called CC=2 SCAR=6.9
Pod documented
1662 1 croak "string too long (${$_[0]})"
1663 82 ▶ if (length ${$_[0]} > 255);
Branch: 1 branch
BranchTrueFalseTotal
if length ${$_[0];} > 255 1 81 82
1664 }
1665  
1666 =item check_int
1667  
1668 checks that the value is a (possibly signed) integer
1669  
1670 =cut
1671  
1672 8 ▶sub check_int {
Subroutine check_int: called CC=3 SCAR=11.0
Pod documented
1673 4 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1674 1 croak "not an integer (${$_[0]})"
1675 8 ▶ unless (is_int ${$_[0]} or ${$_[0]}+0 eq ${$_[0]});
Branch: 1 branch
BranchTrueFalseTotal
unless &is_int(${$_[0];}) or ${$_[0];} + 0 eq ${$_[0];} 1 7 8
Condition100%3/3
&is_int(${$_[0];}) or ${$_[0];} + 0 eq ${$_[0];}
l!l&&r!l&&!r
611
MC/DC100%2/2
&is_int(${$_[0];}) or ${$_[0];} + 0 eq ${$_[0];}
&is_int(${$_[0];}) ${$_[0];} + 0 eq ${$_[0];}
Truth table100%3/3
A or B
A= &is_int(${$_[0];})B= ${$_[0];} + 0 eq ${$_[0];}
ABresult
1X1
011
000
1676 }
1677  
1678 =item check_real
1679  
1680 checks that the value is a real number, by stringifying it and
1681 matching it against (C<m/^-?\d*(\.\d*)?(e-?\d*)?$/>). Inefficient?
1682 Yes. Patches welcome.
1683  
1684 With my cries for help, where are the user-submitted patches?! Well,
1685 this function now checks the scalar flags that indicate that it
1686 contains a number, which isn't flawless, but a lot faster :)
1687  
1688 =cut
1689  
1690 4 my $real_re = qr/^-?\d*(\.\d*)?(e-?\d*)?$/;
1691 7 ▶sub check_real {
Subroutine check_real: called CC=4 SCAR=14.3
Pod documented
1692 1 croak "not a real number (${$_[0]})"
1693 7 unless (is_double(${$_[0]}) or is_int(${$_[0]})
1694 7 ▶ or ${$_[0]} =~ m/$real_re/);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_double(${$_[0];}) or &is_int(${$_[0];}) or ${$_[0];} =~ /$real_re/ 1 6 7
Condition50%3/6
&is_double(${$_[0];}) or &is_int(${$_[0];}) or ${$_[0];} =~ /$real_re/
l!l&&r!l&&!r
061
&is_double(${$_[0];}) or &is_int(${$_[0];})
l!l&&r!l&&!r
007
MC/DC33%1/3
&is_double(${$_[0];}) or &is_int(${$_[0];}) or ${$_[0];} =~ /$real_re/
&is_double(${$_[0];}) &is_int(${$_[0];}) ${$_[0];} =~ /$real_re/
Truth table50%2/4
A or B or C
A= &is_double(${$_[0];})B= &is_int(${$_[0];})C= ${$_[0];} =~ /$real_re/
ABCresult
1XX1
01X1
0011
0000
1695 }
1696  
1697 =item check_obj
1698  
1699 checks that the supplied variable is a reference to a blessed object
1700  
1701 =cut
1702  
1703 # this pattern matches a regular reference
1704 20 ▶sub check_obj {
Subroutine check_obj: called CC=3 SCAR=11.0
Pod documented
1705 1 croak "${$_[0]} is not an object reference"
1706 20 unless (blessed ${ $_[0] }
1707 20 ▶ or !${ $_[0] });
Branch: 1 branch
BranchTrueFalseTotal
unless &blessed(${$_[0];}) or not ${$_[0];} 1 19 20
Condition66%2/3
&blessed(${$_[0];}) or not ${$_[0];}
l!l&&r!l&&!r
1901
MC/DC50%1/2
&blessed(${$_[0];}) or not ${$_[0];}
&blessed(${$_[0];}) not ${$_[0];}
Truth table66%2/3
A or not(B)
A= &blessed(${$_[0];})B= not ${$_[0];}
ABresult
1X1
011
000
1708 }
1709  
1710 =item check_flat_array
1711  
1712 checks that $value is a ref ARRAY and that all elements are unblessed
1713 scalars. Does NOT currently check that all values are of the correct
1714 type (int vs real vs string, etc)
1715  
1716 =cut
1717  
1718 3 ▶sub check_flat_array {
Subroutine check_flat_array: called CC=4 SCAR=18.7
Pod documented
1719 0 croak "${$_[0]} is not a flat array"
1720 3 ▶ if (ref ${$_[0]} ne "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
if ref ${$_[0];} ne "ARRAY" 0 3 3
1721 0 croak "flat array ${$_[0]} may not contain references"
1722 0 ▶ if (map { (ref $_ ? "1" : ()) } @${$_[0]});
Branch: 2 branches
BranchTrueFalseTotal
if map {ref $_ ? 1 : ();} @${$_[0];} 0 3 3
ref $_ ? : 0 0 0
1723 }
1724  
1725 =item check_rawdate
1726  
1727 checks that $value is of the form YYYY-MM-DD, or YYYYMMDD, or YYMMDD.
1728  
1729 =cut
1730  
1731 # YYYY-MM-DD HH:MM:SS
1732 4 my $rawdate_re = qr/^(?: \d{4}-\d{2}-\d{2}
1733 | (?:\d\d){3,4}
1734 )$/x;
1735 2 ▶sub check_rawdate {
Subroutine check_rawdate: called CC=2 SCAR=6.9
Pod documented
1736 1 croak "invalid SQL rawdate `${$_[0]}'"
1737 2 ▶ unless (${$_[0]} =~ m/$rawdate_re/o);
Branch: 1 branch
BranchTrueFalseTotal
unless ${$_[0];} =~ /$rawdate_re/o 1 1 2
1738 }
1739  
1740 =item check_rawtime
1741  
1742 checks that $value is of the form HH:MM(:SS)?
1743  
1744 =cut
1745  
1746 # YYYY-MM-DD HH:MM:SS
1747 4 my $rawtime_re = qr/^\d{1,2}:\d{2}(?::\d{2})?$/;
1748 2 ▶sub check_rawtime {
Subroutine check_rawtime: called CC=2 SCAR=6.9
Pod documented
1749 1 croak "invalid SQL rawtime `${$_[0]}'"
1750 2 ▶ unless (${$_[0]} =~ m/$rawtime_re/o);
Branch: 1 branch
BranchTrueFalseTotal
unless ${$_[0];} =~ /$rawtime_re/o 1 1 2
1751 }
1752  
1753 =item check_rawdatetime
1754  
1755 checks that $value is of the form YYYY-MM-DD HH:MM(:SS)? (the time
1756 and/or the date can be missing), or a string of numbers between 6 and
1757 14 numbers long.
1758  
1759 =cut
1760  
1761 4 my $rawdatetime_re = qr/^(?:
1762 # YYYY-MM-DD HH:MM:SS
1763 (?: (?:\d{4}-\d{2}-\d{2}\s+)?
1764 \d{1,2}:\d{2}(?::\d{2})?
1765 | \d{4}-\d{2}-\d{2}
1766 )
1767 | # YYMMDD, etc
1768 (?:\d\d){3,7}
1769 )$/x;
1770 9 ▶sub check_rawdatetime {
Subroutine check_rawdatetime: called CC=2 SCAR=6.9
Pod documented
1771 2 croak "invalid SQL rawdatetime `${$_[0]}'"
1772 9 ▶ unless (${$_[0]} =~ m/$rawdatetime_re/o);
Branch: 1 branch
BranchTrueFalseTotal
unless ${$_[0];} =~ /$rawdatetime_re/o 2 7 9
1773 }
1774  
1775 =item check_dmdatetime
1776  
1777 checks that $value is of the form YYYYMMDDHH:MM:SS, or those allowed
1778 for rawdatetime.
1779  
1780 =cut
1781  
1782 2 ▶sub check_dmdatetime {
Subroutine check_dmdatetime: called CC=3 SCAR=11.0
Pod documented
1783 1 croak "invalid dmdatetime `${$_[0]}'"
1784 2 unless (${$_[0]} =~ m/^\d{10}:\d\d:\d\d$|$rawdatetime_re/o
1785 2 ▶ or Date::Manip::ParseDate(${$_[0]}));
Branch: 1 branch
BranchTrueFalseTotal
unless ${$_[0];} =~ /^\d{10}:\d\d:\d\d$|$rawdatetime_re/o or Date::Manip::ParseDate(${$_[0];}) 1 1 2
Condition66%2/3
${$_[0];} =~ /^\d{10}:\d\d:\d\d$|$rawdatetime_re/o or Date::Manip::ParseDate(${$_[0];})
l!l&&r!l&&!r
101
MC/DC50%1/2
${$_[0];} =~ /^\d{10}:\d\d:\d\d$|$rawdatetime_re/o or Date::Manip::ParseDate(${$_[0];})
${$_[0];} =~ /^\d{10}:\d\d:\d\d$|$rawdatetime_re/o Date::Manip::ParseDate(${$_[0];})
Truth table66%2/3
A or B
A= ${$_[0];} =~ /^\d{10}:\d\d:\d\d$|$rawdatetime_re/oB= Date::Manip::ParseDate(${$_[0];})
ABresult
1X1
011
000
1786 }
1787  
1788 =item check_flat_hash
1789  
1790 checks that $value is a ref HASH and all values are scalars. Does NOT
1791 currently check that all values are of the correct type (int vs real
1792 vs string, etc)
1793  
1794 =cut
1795  
1796 3 ▶sub check_flat_hash {
Subroutine check_flat_hash: called CC=5 SCAR=23.4
Pod documented
1797 0 croak "${$_[0]} is not a hash"
1798 3 ▶ unless (ref ${$_[0]} eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
unless ref ${$_[0];} eq "HASH" 0 3 3
1799 3 ▶ while (my ($k, $v) = each %${$_[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %${$_[0];}) 0 3 3
1800 0 ▶ croak "hash not flat"
Branch: 1 branch
BranchTrueFalseTotal
if ref $k or ref $v 0 0 0
Condition0%0/3
ref $k or ref $v
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $k or ref $v
ref $k ref $v
Truth table0%0/3
A or B
A= ref $kB= ref $v
ABresult
1X1
011
000
1801 if (ref $k or ref $v);
1802 }
1803 }
1804  
1805 =item check_set
1806  
1807 Checks that the passed value is a Set::Object
1808  
1809 =cut
1810  
1811 89 ▶sub check_set {
Subroutine check_set: called CC=2 SCAR=8.1
Pod documented
1812 0 confess "${$_[0]} is not a set"
1813 89 ▶ unless (UNIVERSAL::isa(${$_[0]}, "Set::Object"));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa(${$_[0];}, "Set::Object") 0 89 89
1814 }
1815  
1816 =item check_hash
1817  
1818 Checks that the passed value is a perl HV
1819  
1820 =cut
1821  
1822 3 ▶sub check_hash {
Subroutine check_hash: called CC=2 SCAR=8.1
Pod documented
1823 0 confess "${$_[0]} is not a hash"
1824 3 ▶ unless (reftype(${$_[0]}) eq "HASH");
Branch: 1 branch
BranchTrueFalseTotal
unless &reftype(${$_[0];}) eq "HASH" 0 3 3
1825 }
1826  
1827 =item check_array
1828  
1829 Checks that the passed value is a perl AV
1830  
1831 =cut
1832  
1833 7 ▶sub check_array {
Subroutine check_array: called CC=2 SCAR=8.1
Pod documented
1834 0 confess "${$_[0]} is not an array"
1835 7 ▶ unless (reftype(${$_[0]}) eq "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
unless &reftype(${$_[0];}) eq "ARRAY" 0 7 7
1836 }
1837  
1838 =item check_nothing
1839  
1840 checks whether Australians like sport
1841  
1842 =cut
1843  
1844 1 ▶sub check_nothing { }
Subroutine check_nothing: called CC=1 SCAR=0.0
Pod documented
1845  
1846 =back
1847  
1848 =item destroy_X ($instance, $attr)
1849  
1850 Similar story with the check_X series of functions, these are called
1851 during object destruction on every attribute that has a reference that
1852 might need breaking. Note: B<these functions all assume that
1853 attributes belonging to an object that is being destroyed may be
1854 destroyed also>. In other words, do not allow distinct objects to
1855 share Set::Object containers or hash references in their attributes,
1856 otherwise when one gets destroyed the others will lose their data.
1857  
1858 Available functions:
1859  
1860 =over
1861  
1862 =item destroy_array
1863  
1864 empties an array
1865  
1866 =cut
1867  
1868 1 ▶sub destroy_array {
Subroutine destroy_array: called CC=3 SCAR=11.6
Pod documented
1869 1 my $self = shift;
1870 1 my $attr = shift;
1871 1 my $t = tied $self->{$attr};
1872 1 ▶ @{$self->{$attr}} = ()
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t and $t =~ /Tangram::CollOnDemand/ 1 0 1
Condition33%1/3
defined $t and $t =~ /Tangram::CollOnDemand/
!ll&&!rl&&r
100
MC/DC0%0/2
defined $t and $t =~ /Tangram::CollOnDemand/
defined $t $t =~ /Tangram::CollOnDemand/
Truth table33%1/3
A and B
A= defined $tB= $t =~ /Tangram::CollOnDemand/
ABresult
0X0
100
111
1873 unless (defined $t and $t =~ m,Tangram::CollOnDemand,);
1874 1 delete $self->{$attr};
1875 }
1876  
1877 =item destroy_set
1878  
1879 Calls Set::Object::clear to clear the set
1880  
1881 =cut
1882  
1883 30 ▶sub destroy_set {
Subroutine destroy_set: called CC=4 SCAR=14.2
Pod documented
1884 30 my $self = shift;
1885 30 my $attr = shift;
1886  
1887 #return if (reftype $self ne "HASH");
1888 30 my $t = tied $self->{$attr};
1889 30 ▶ return if (defined $t and $t =~ m,Tangram::CollOnDemand,);
Branch: 1 branch
BranchTrueFalseTotal
if defined $t and $t =~ /Tangram::CollOnDemand/ 0 30 30
Condition33%1/3
defined $t and $t =~ /Tangram::CollOnDemand/
!ll&&!rl&&r
3000
MC/DC0%0/2
defined $t and $t =~ /Tangram::CollOnDemand/
defined $t $t =~ /Tangram::CollOnDemand/
Truth table33%1/3
A and B
A= defined $tB= $t =~ /Tangram::CollOnDemand/
ABresult
0X0
100
111
1890 30 ▶ if (ref $self->{$attr} eq "Set::Object") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{$attr} eq "Set::Object") 29 1 30
1891 29 $self->{$attr}->clear;
1892 }
1893 30 delete $self->{$attr};
1894 }
1895  
1896 =item destroy_hash
1897  
1898 empties a hash
1899  
1900 =cut
1901  
1902 2 ▶sub destroy_hash {
Subroutine destroy_hash: called CC=3 SCAR=11.6
Pod documented
1903 2 my $self = shift;
1904 2 my $attr = shift;
1905 2 my $t = tied $self->{$attr};
1906 2 ▶ %{$self->{$attr}} = ()
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t and $t =~ /Tangram::CollOnDemand/ 2 0 2
Condition33%1/3
defined $t and $t =~ /Tangram::CollOnDemand/
!ll&&!rl&&r
200
MC/DC0%0/2
defined $t and $t =~ /Tangram::CollOnDemand/
defined $t $t =~ /Tangram::CollOnDemand/
Truth table33%1/3
A and B
A= defined $tB= $t =~ /Tangram::CollOnDemand/
ABresult
0X0
100
111
1907 unless (defined $t and $t =~ m,Tangram::CollOnDemand,);
1908 2 delete $self->{$attr};
1909 }
1910  
1911 =item destroy_ref
1912  
1913 destroys a reference.
1914  
1915 =cut
1916  
1917 12 ▶sub destroy_ref {
Subroutine destroy_ref: called CC=1 SCAR=0.0
Pod documented
1918 12 my $self = shift;
1919 12 delete $self->{(shift)};
1920 }
1921  
1922 =back
1923  
1924 =item parse_X ($attribute, { schema option })
1925  
1926 Parses the schema option field, and returns one or two closures that
1927 act as a check_X and a destroy_X function for the attribute.
1928  
1929 This is currently a very ugly hack, parsing the SQL type definition of
1930 an object. But it was bloody handy in my case for hacking this in
1931 quickly. This is probably unmanagably unportable across databases;
1932 but send me bug reports on it anyway, and I'll try and make the
1933 parsers work for as many databases as possible.
1934  
1935 This perhaps should be replaced by primitives that go the other way,
1936 building the SQL type definition from a more abstract definition of
1937 the type.
1938  
1939 Available functions:
1940  
1941 =over
1942  
1943 =item parse_string
1944  
1945 parses SQL types of:
1946  
1947 =over
1948  
1949 =cut
1950  
1951 4 ▶use vars qw($quoted_part $sql_list);
Subroutine BEGIN: called CC=1 SCAR=0.0
1952  
1953 4 $quoted_part = qr/(?: \"([^\"]+)\" | \'([^\']+)\' )/x;
1954 4 $sql_list = qr/\(\s*
1955 (
1956 $quoted_part
1957 (?:\s*,\s* $quoted_part )*
1958 ) \s*\)/x;
1959  
1960 22 ▶sub parse_string {
Subroutine parse_string: called CC=12 SCAR=30.5
Pod documented
1961  
1962 22 my $attribute = shift;
1963 22 my $option = shift;
1964  
1965 # simple case; return the check_string function. We don't
1966 # need a destructor for a string so don't return one.
1967 22 ▶ if (!$option->{sql}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($option->{"sql"}) 18 4 22
1968 18 return \&check_string;
1969 }
1970  
1971 4 my $sql = $option->{sql};
1972  
1973 # remove some common suffixes
1974 4 $sql =~ s{\s+default\s+\S+}{}si;
1975 4 $sql =~ s{(\s+not)?\s+null}{}si;
1976  
1977 =item CHAR(N), VARCHAR(N)
1978  
1979 closure checks length of string is less than N characters
1980  
1981 =cut
1982  
1983 4 ▶ if ($option->{sql} =~ m/^\s*(?:var)?char\s*\(\s*(\d+)\s*\)/ix) {
Branch: 4 branches
BranchTrueFalseTotal
if ($option->{'sql'} =~ /^\s*(?:var)?char\s*\(\s*(\d+)\s*\)/xi) { } 0 4 4
elsif ($option->{'sql'} =~ /^\s*(?:tiny|long|medium)? (?:blob|text)/xi) { } 0 4 4
elsif (my($members) = $option->{'sql'} =~ /^\s*set\s*$Class::Tangram::sql_list/io) { } 2 2 4
elsif (my($values) = $option->{'sql'} =~ /^\s*enum\s*$Class::Tangram::sql_list/io) { } 2 0 2
1984 0 my $max_length = $1;
1985 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1986 croak "string too long for $attribute"
1987 0 ▶ if (length ${$_[0]} > $max_length);
Branch: 1 branch
BranchTrueFalseTotal
if length ${$_[0];} > $max_length 0 0 0
1988 0 };
1989  
1990 =item TINYBLOB, BLOB, LONGBLOB
1991  
1992 checks max. length of string to be 255, 65535 or 16777215 chars
1993 respectively. Also works with "TEXT" instead of "BLOB"
1994  
1995 =cut
1996  
1997 } elsif ($option->{sql} =~ m/^\s*(?:tiny|long|medium)?
1998 (?:blob|text)/ix) {
1999 0 ▶ my $max_length = ($1 ? ($1 eq "tiny"?255:2**24 - 1)
Branch: 2 branches
BranchTrueFalseTotal
$1 ? : 0 0 0
$1 eq 'tiny' ? : 0 0 0
2000 : 2**16 - 1);
2001 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
2002 croak "string too long for $attribute"
2003 0 ▶ if (${$_[0]} and length ${$_[0]} > $max_length);
Branch: 1 branch
BranchTrueFalseTotal
if ${$_[0];} and length ${$_[0];} > $max_length 0 0 0
Condition0%0/3
${$_[0];} and length ${$_[0];} > $max_length
!ll&&!rl&&r
000
MC/DC0%0/2
${$_[0];} and length ${$_[0];} > $max_length
${$_[0];} length ${$_[0];} > $max_length
Truth table0%0/3
A and B
A= ${$_[0];}B= length ${$_[0];} > $max_length
ABresult
0X0
100
111
2004 0 };
2005  
2006 =item SET("members", "of", "set")
2007  
2008 checks that the value passed is valid as a SQL set type, and that all
2009 of the passed values are allowed to be a member of that set.
2010  
2011 =cut
2012  
2013 } elsif (my ($members) = $option->{sql} =~
2014 m/^\s*set\s*$sql_list/oi) {
2015  
2016 2 my %members;
2017 2 ▶ $members{lc($1 || $2)} = 1
Branch: 1 branch
BranchTrueFalseTotal
$members =~ /\G[,\s]*$Class::Tangram::quoted_part/cog and do { $members{lc($1 || $2)} = 1 } 9 2 11
Condition66%2/3
$1 || $2
l!l&&r!l&&!r
630
MC/DC0%0/2
$1 || $2
$1 $2
Truth table66%2/3
A || B
A= $1B= $2
ABresult
1X1
011
000
2018 while ( $members =~ m/\G[,\s]*$quoted_part/cog );
2019  
2020 4 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
2021 4 for my $x (split /\s*,\s*/, ${$_[0]}) {
2022 croak ("SQL set badly formed or invalid member $x "
2023 ." (SET" . join(",", keys %members). ")")
2024 10 ▶ if (not exists $members{lc($x)});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $members{lc $x} 2 8 10
2025 }
2026 2 };
2027  
2028 =item ENUM("possible", "values")
2029  
2030 checks that the value passed is one of the allowed values.
2031  
2032 =cut
2033  
2034 } elsif (my ($values) = $option->{sql} =~
2035 m/^\s*enum\s*$sql_list/oi ) {
2036  
2037 2 my %values;
2038 2 ▶ $values{lc($1 || $2)} = 1
Branch: 1 branch
BranchTrueFalseTotal
$values =~ /\G[,\s]*$Class::Tangram::quoted_part/cg and do { $values{lc($1 || $2)} = 1 } 9 2 11
Condition66%2/3
$1 || $2
l!l&&r!l&&!r
630
MC/DC0%0/2
$1 || $2
$1 $2
Truth table66%2/3
A || B
A= $1B= $2
ABresult
1X1
011
000
2039 while ( $values =~ m/\G[,\s]*$quoted_part/gc);
2040  
2041 6 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
2042 2 croak ("invalid enum value ${$_[0]} must be ("
2043 . join(",", keys %values). ")")
2044 6 ▶ if (not exists $values{lc(${$_[0]})});
Branch: 1 branch
BranchTrueFalseTotal
if not exists $values{lc ${$_[0];}} 2 4 6
2045 }
2046  
2047  
2048 2 } else {
2049 croak ("Please build support for your string SQL type in "
2050 0 ."Class::Tangram (".$option->{sql}.")");
2051 }
2052 }
2053  
2054 =back
2055  
2056 =back
2057  
2058 =back
2059  
2060 =head2 Quick Object Dumping and Destruction
2061  
2062 =over
2063  
2064 =item $instance->quickdump
2065  
2066 Quickly show the blessed hash of an object, without descending into
2067 it. Primarily useful when you have a large interconnected graph of
2068 objects so don't want to use the B<x> command within the debugger.
2069 It also doesn't have the side effect of auto-vivifying members.
2070  
2071 This function returns a string, suitable for print()ing. It does not
2072 currently escape unprintable characters.
2073  
2074 =cut
2075  
2076 0 ▶sub quickdump {
Subroutine quickdump: not called CC=6 SCAR=37.4
Pod documented
2077 0 my $self = shift;
2078  
2079 0 my $r = "REF ". (ref $self). "\n";
2080 0 for my $k (sort keys %$self) {
2081 0 eval {
2082 $r .= (" $k => "
2083 . (
2084 tied $self->{$k}
2085 || ( ref $self->{$k}
2086 ? $self->{$k}
2087 : ( defined ($self->{$k})
2088 0 ▶ ? "'".$self->{$k}."'"
Branch: 2 branches
BranchTrueFalseTotal
ref $self->{$k} ? : 0 0 0
defined $self->{$k} ? : 0 0 0
Condition0%0/3
tied $self->{$k} || (ref $self->{$k} ? $self->{$k} : (defined $self->{$k} ? q['] . $self->{$k} . q['] : 'undef'))
l!l&&r!l&&!r
000
MC/DC0%0/2
tied $self->{$k} || (ref $self->{$k} ? $self->{$k} : (defined $self->{$k} ? q['] . $self->{$k} . q['] : 'undef'))
tied $self->{$k} (ref $self->{$k} ? $self->{$k} : (defined $self->{$k} ? q['] . $self->{$k} . q['] : 'undef'))
Truth table0%0/3
A || B
A= tied $self->{$k}B= (ref $self->{$k} ? $self->{$k} : (defined $self->{$k} ? q['] . $self->{$k} . q['] : 'undef'))
ABresult
1X1
011
000
2089 : "undef" )
2090 )
2091 )
2092 . "\n");
2093 };
2094 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
2095 0 $r .= " $k => Error('$@')\n";
2096 }
2097 }
2098 0 return $r;
2099 }
2100  
2101  
2102 =item $instance->DESTROY
2103  
2104 This function ensures that all of your attributes have their
2105 destructors called. It calls the destroy_X function for attributes
2106 that have it defined, if that attribute exists in the instance that we
2107 are destroying. It calls the destroy_X functions as destroy_X($self,
2108 $k)
2109  
2110 =cut
2111  
2112 67 ▶sub DESTROY {
Subroutine DESTROY: called CC=5 SCAR=16.2
2113 67 my $self = shift;
2114  
2115 67 my $class = ref $self;
2116  
2117 # if no cleaners are known for this class, it hasn't been imported
2118 # yet. Don't call import_schema, that would be a bad idea in a
2119 # destructor.
2120 67 ▶ exists $cleaners{$class} or return;
Branch: 1 branch
BranchTrueFalseTotal
exists $Class::Tangram::cleaners{$class} or return 67 0 67
2121  
2122 # for every attribute that is defined, and has a cleaner function,
2123 # call the cleaner function.
2124 67 for my $k (keys %$self) {
2125 180 ▶ if (defined $cleaners{$class}->{$k} and exists $self->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Class::Tangram::cleaners{$class}{$k} and exists $self->{$k}) 44 136 180
Condition66%2/3
defined $Class::Tangram::cleaners{$class}{$k} and exists $self->{$k}
!ll&&!rl&&r
136044
MC/DC50%1/2
defined $Class::Tangram::cleaners{$class}{$k} and exists $self->{$k}
defined $Class::Tangram::cleaners{$class}{$k} exists $self->{$k}
Truth table66%2/3
A and B
A= defined $Class::Tangram::cleaners{$class}{$k}B= exists $self->{$k}
ABresult
0X0
100
111
2126 44 $cleaners{$class}->{$k}->($self, $k);
2127 }
2128 }
2129 67 $self->{_DESTROYED} = 1;
2130 }
2131  
2132 =item $instance->clear_refs
2133  
2134 This clears all references from this object, ie exactly what DESTROY
2135 normally does, but calling an object's destructor method directly is
2136 bad form. Also, this function has no qualms with loading the class'
2137 schema with import_schema() as needed.
2138  
2139 This is useful for breaking circular references, if you know you are
2140 no longer going to be using an object then you can call this method,
2141 which in many cases will end up cleaning up most of the objects you
2142 want to get rid of.
2143  
2144 However, it still won't do anything about Tangram's internal reference
2145 to the object, which must still be explicitly unlinked with the
2146 Tangram::Storage->unload method.
2147  
2148 =cut
2149  
2150 1 ▶sub clear_refs {
Subroutine clear_refs: called CC=5 SCAR=17.2
Pod documented
2151 1 my $self = shift;
2152 1 my $class = ref $self;
2153  
2154 1 ▶ exists $cleaners{$class} or import_schema($class);
Branch: 1 branch
BranchTrueFalseTotal
exists $Class::Tangram::cleaners{$class} or import_schema($class) 1 0 1
2155  
2156 # break all ref's, sets, arrays
2157 1 for my $k (keys %$self) {
2158 1 ▶ if (defined $cleaners{$class}->{$k} and exists $self->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Class::Tangram::cleaners{$class}{$k} and exists $self->{$k}) 1 0 1
Condition33%1/3
defined $Class::Tangram::cleaners{$class}{$k} and exists $self->{$k}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $Class::Tangram::cleaners{$class}{$k} and exists $self->{$k}
defined $Class::Tangram::cleaners{$class}{$k} exists $self->{$k}
Truth table33%1/3
A and B
A= defined $Class::Tangram::cleaners{$class}{$k}B= exists $self->{$k}
ABresult
0X0
100
111
2159 1 $cleaners{$class}->{$k}->($self, $k);
2160 }
2161 }
2162 1 $self->{_NOREFS} = 1;
2163 }
2164  
2165 =back
2166  
2167 =head1 FUNCTIONS
2168  
2169 The following functions are not intended to be called as object
2170 methods.
2171  
2172 =head2 Schema Import
2173  
2174 our $fields = { int => [ qw(foo bar) ],
2175 string => [ qw(baz quux) ] };
2176  
2177 # Version 1.115 and below compatibility:
2178 our $schema = {
2179 fields => { int => [ qw(foo bar) ],
2180 string => [ qw(baz quux) ] }
2181 };
2182  
2183 =over
2184  
2185 =item Class::Tangram::import_schema($class)
2186  
2187 Parses a tangram object field list, in C<${"${class}::fields"}> (or
2188 C<${"${class}::schema"}-E<gt>{fields}> to the internal type information
2189 hashes. It will also define all of the attribute accessor and update
2190 methods in the C<$class> package.
2191  
2192 Note that calling this function twice for the same class is not
2193 tested and may produce arbitrary results. Patches welcome.
2194  
2195 =cut
2196  
2197 # "parse" is special - it is passed the options hash given
2198 # by the user and should return (\&check_func,
2199 # \&destroy_func). This is how the magical string type
2200 # checking is performed - see the entry for parse_string(),
2201 # below.
2202  
2203 %defaults = (
2204 int => { check_func => \&check_int,
2205 load => "Tangram/Scalar.pm",
2206 },
2207 real => { check_func => \&check_real,
2208 load => "Tangram/Scalar.pm",
2209 },
2210 string => { parse => \&parse_string,
2211 load => "Tangram/Scalar.pm",
2212 },
2213 ref => { check_func => \&check_obj,
2214 destroy_func => \&destroy_ref,
2215 load => "Tangram/Ref.pm",
2216 },
2217 array => { check_func => \&check_array,
2218 destroy_func => \&destroy_array,
2219 load => "Tangram/Array.pm",
2220 },
2221 iarray => { check_func => \&check_array,
2222 destroy_func => \&destroy_array,
2223 load => "Tangram/IntrArray.pm",
2224 },
2225 flat_array => { check_func => \&check_flat_array,
2226 load => "Tangram/FlatArray.pm",
2227 },
2228 set => { check_func => \&check_set,
2229 destroy_func => \&destroy_set,
2230 28 ▶ init_default => sub { Set::Object->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
2231 load => "Tangram/Set.pm",
2232 },
2233 iset => { check_func => \&check_set,
2234 destroy_func => \&destroy_set,
2235 2 ▶ init_default => sub { Set::Object->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
2236 4 load => "Tangram/IntrSet.pm",
2237 },
2238 dmdatetime => { check_func => \&check_dmdatetime,
2239 load => "Tangram/DMDateTime.pm",
2240 },
2241 rawdatetime => { check_func => \&check_rawdatetime,
2242 load => "Tangram/RawDateTime.pm",
2243 },
2244 rawdate => { check_func => \&check_rawdate,
2245 load => "Tangram/RawDate.pm",
2246 },
2247 rawtime => { check_func => \&check_rawtime,
2248 load => "Tangram/RawTime.pm",
2249 },
2250 flat_hash => { check_func => \&check_flat_hash,
2251 load => "Tangram/FlatHash.pm",
2252 },
2253 transient => { check_func => \&check_nothing,
2254 },
2255 hash => { check_func => \&check_hash,
2256 destroy_func => \&destroy_hash,
2257 load => "Tangram/Hash.pm",
2258 },
2259 ihash => { check_func => \&check_hash,
2260 destroy_func => \&destroy_hash,
2261 load => "Tangram/IntrHash.pm",
2262 },
2263 perl_dump => { check_func => \&check_nothing,
2264 load => "Tangram/PerlDump.pm",
2265 },
2266 yaml => { check_func => \&check_nothing,
2267 load => "Tangram/YAML.pm",
2268 },
2269 backref => { check_func => \&check_nothing,
2270 },
2271 storable => { check_func => \&check_nothing,
2272 load => "Tangram/Storable.pm",
2273 },
2274 idbif => { check_func => \&check_nothing,
2275 load => "Tangram/IDBIF.pm",
2276 },
2277 );
2278  
2279 25 ▶sub import_schema { # Damn this function is long
Subroutine import_schema: called CC=29 SCAR=34.0
Pod documented
2280 25 my $class = shift;
2281  
2282 25 ▶ return if exists $abstract{$class};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Class::Tangram::abstract{$class} 0 25 25
2283  
2284 25 eval {
2285 25 my ($fields, $bases, $abstract);
2286 {
2287  
2288 # Here, we go hunting around for their defined schema and
2289 # options
2290 25 local $^W=0;
2291 25 eval {
2292 25 $fields = (${"${class}::fields"} ||
2293 25 ▶ ${"${class}::schema"}->{fields});
Condition100%3/3
${"${class}::fields";} || ${"${class}::schema";}->{'fields'}
l!l&&r!l&&!r
1474
MC/DC100%2/2
${"${class}::fields";} || ${"${class}::schema";}->{'fields'}
${"${class}::fields";} ${"${class}::schema";}->{'fields'}
Truth table100%3/3
A || B
A= ${"${class}::fields";}B= ${"${class}::schema";}->{'fields'}
ABresult
1X1
011
000
2294 25 $abstract = (${"${class}::abstract"} ||
2295 25 ▶ ${"${class}::schema"}->{abstract});
Condition33%1/3
${"${class}::abstract";} || ${"${class}::schema";}->{'abstract'}
l!l&&r!l&&!r
0025
MC/DC0%0/2
${"${class}::abstract";} || ${"${class}::schema";}->{'abstract'}
${"${class}::abstract";} ${"${class}::schema";}->{'abstract'}
Truth table33%1/3
A || B
A= ${"${class}::abstract";}B= ${"${class}::schema";}->{'abstract'}
ABresult
1X1
011
000
2296 25 $bases = ${"${class}::schema"}->{bases};
2297 };
2298 25 ▶ if ( my @stack = (grep !/${class}::CT/,
Branch: 1 branch
BranchTrueFalseTotal
if (my(@stack) = grep((!/$class::CT/), @{"${class}::ISA";})) 25 0 25
2299 25 @{"${class}::ISA"} )) {
2300 # clean "bases" information from @ISA
2301 25 my %seen = map { $_ => 1 } $class, __PACKAGE__;
2302 25 $bases = [];
2303 25 ▶ while ( my $super = pop @stack ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $super = pop @stack) 30 25 55
2304 30 ▶ if ( defined ${"${super}::schema"}
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${"${super}::schema";} or defined ${"${super}::fields";}) { } 12 18 30
Condition66%2/3
defined ${"${super}::schema";} or defined ${"${super}::fields";}
l!l&&r!l&&!r
12018
MC/DC50%1/2
defined ${"${super}::schema";} or defined ${"${super}::fields";}
defined ${"${super}::schema";} defined ${"${super}::fields";}
Truth table66%2/3
A or B
A= defined ${"${super}::schema";}B= defined ${"${super}::fields";}
ABresult
1X1
011
000
2305 18 or defined ${"${super}::fields"} ) {
2306 12 push @$bases, $super;
2307 } else {
2308 2 push @stack, grep { !$seen{$_}++ }
2309 18 @{"${super}::ISA"};
2310 }
2311 }
2312 25 ▶ if ( !$fields and !@$bases ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fields or @$bases) 0 25 25
Condition66%2/3
$fields or @$bases
l!l&&r!l&&!r
2140
MC/DC0%0/2
$fields or @$bases
$fields @$bases
Truth table66%2/3
A or B
A= $fieldsB= @$bases
ABresult
1X1
011
000
2313 0 croak ("No schema and no Class::Tangram "
2314 ."superclass for $class; define "
2315 ."${class}::fields!");
2316 }
2317 }
2318 }
2319  
2320 # play around with the @ISA to insert an intermediate package
2321 25 my $target_pkg = $class."::CT";
2322 25 my $target_stash = \%{$target_pkg."::"};
2323 25 (@{$target_pkg."::ISA"}, @{$class."::ISA"})
2324 25 = @{$class."::ISA"};
2325 25 @{$class."::ISA"} = $target_pkg;
2326  
2327 # if this is an abstract type, do not allow it to be
2328 # instantiated
2329 25 ▶ $abstract{$class} = $abstract ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$abstract ? : 0 25 25
2330  
2331 # If there are any base classes, import them first so that the
2332 # check, cleaners and init_defaults can be inherited
2333 25 ▶ if (defined $bases) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bases) 25 0 25
2334 25 ▶ (ref $bases eq "ARRAY")
Branch: 1 branch
BranchTrueFalseTotal
ref $bases eq "ARRAY" or croak("bases not an array ref for $class") 25 0 25
2335 or croak "bases not an array ref for $class";
2336  
2337 # Note that the order of your bases is significant, that
2338 # is if you are using multiple iheritance then the later
2339 # classes override the earlier ones.
2340 25 for my $super ( @$bases ) {
2341 12 ▶ import_schema($super) unless (exists $check{$super});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Class::Tangram::check{$super} 0 12 12
2342  
2343 # copy each of the per-class configuration hashes to
2344 # this class as defaults.
2345 12 my ($k, $v);
2346  
2347 # FIXME - this repetition of code is getting silly :)
2348 $types{$class}->{$k} = $v
2349 12 ▶ while (($k, $v) = each %{ $types{$super} } );
Branch: 1 branch
BranchTrueFalseTotal
() = each %{$Class::Tangram::types{$super};} and do { $Class::Tangram::types{$class}{$k} = $v } 84 12 96
2350 $check{$class}->{$k} = $v
2351 12 ▶ while (($k, $v) = each %{ $check{$super} } );
Branch: 1 branch
BranchTrueFalseTotal
() = each %{$Class::Tangram::check{$super};} and do { $Class::Tangram::check{$class}{$k} = $v } 84 12 96
2352 $cleaners{$class}->{$k} = $v
2353 12 ▶ while (($k, $v) = each %{ $cleaners{$super} } );
Branch: 1 branch
BranchTrueFalseTotal
() = each %{$Class::Tangram::cleaners{$super};} and do { $Class::Tangram::cleaners{$class}{$k} = $v } 26 12 38
2354 $attribute_options{$class}->{$k} = $v
2355 12 ▶ while (($k, $v) = each %{ $attribute_options{$super} } );
Branch: 1 branch
BranchTrueFalseTotal
() = each %{$Class::Tangram::attribute_options{$super};} and do { $Class::Tangram::attribute_options{$class}{$k} = $v } 84 12 96
2356 $init_defaults{$class}->{$k} = $v
2357 12 ▶ while (($k, $v) = each %{ $init_defaults{$super} } );
Branch: 1 branch
BranchTrueFalseTotal
() = each %{$Class::Tangram::init_defaults{$super};} and do { $Class::Tangram::init_defaults{$class}{$k} = $v } 11 12 23
2358 $required_attributes{$class}->{$k} = $v
2359 12 ▶ while (($k, $v) = each %{ $required_attributes{$super} } );
Branch: 1 branch
BranchTrueFalseTotal
() = each %{$Class::Tangram::required_attributes{$super};} and do { $Class::Tangram::required_attributes{$class}{$k} = $v } 3 12 15
2360 $companions{$class}->{$k} = $v
2361 12 ▶ while (($k, $v) = each %{ $companions{$super} } );
Branch: 1 branch
BranchTrueFalseTotal
() = each %{$Class::Tangram::companions{$super};} and do { $Class::Tangram::companions{$class}{$k} = $v } 20 12 32
2362 }
2363 }
2364  
2365 # iterate over each of the *types* of fields (string, int, ref, etc.)
2366 25 ▶ while (my ($type, $v) = each %$fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$fields) 55 25 80
2367 55 ▶ if (ref $v eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "ARRAY") 25 30 55
2368 25 $v = { map { $_, undef } @$v };
2369 }
2370  
2371 # iterate each of the *attributes* of a particular type
2372 55 ▶ while (my ($attribute, $options) = each %$v) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$v) 74 55 129
2373  
2374 74 my $accessors = _mk_accessor($attribute, $options, $class,
2375 $target_pkg, $type);
2376 # now export all these accessors into caller's namespace
2377 74 ▶ while (my ($accessor, $coderef) = each %$accessors) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$accessors) 618 74 692
2378 618 my $accessor_name = $accessor;
2379 # comes in like $class::$meth, so extract our meth
2380 618 $accessor_name =~ s/(.*\:\:)+(\w+)$/$2/;
2381 618 ▶ *{$accessor} = $coderef
Branch: 1 branch
BranchTrueFalseTotal
unless $target_pkg->can($accessor_name) 618 0 618
2382 unless $target_pkg->can($accessor_name);
2383 }
2384 }
2385 }
2386 };
2387 25 ▶ $cleaners{$class} ||= {};
Condition100%2/2
$Class::Tangram::cleaners{$class} ||= {}
l!l
223
MC/DC100%1/1
$Class::Tangram::cleaners{$class} ||= {}
$Class::Tangram::cleaners{$class}
Truth table100%2/2
A
A= $Class::Tangram::cleaners{$class}
Aresult
11
00
2388  
2389 25 ▶ $@ && die "$@ while trying to import schema for $class";
Branch: 1 branch
BranchTrueFalseTotal
$@ and die "$@ while trying to import schema for $class" 0 25 25
2390 }
2391  
2392 74 ▶sub _mk_accessor {
Subroutine _mk_accessor: called CC=37 SCAR=39.1
2393  
2394 74 my ($attribute, $options, $class, $target_pkg, $type, $dontcarp) = @_;
2395  
2396 74 my $def = $defaults{$type};
2397  
2398 # hash of various accessor code refs to return
2399 74 my %accessors;
2400  
2401 # this is what we are finding out about each attribute
2402 # $type is already set
2403 74 my ($check_func, $default, $required, $cleaner,
2404 $companion, $base_type, $load);
2405 # set defaults from what they give
2406 74 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
4232
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
2407 74 ▶ if (ref $options eq "HASH" or
Branch: 1 branch
BranchTrueFalseTotal
if (ref $options eq "HASH" or UNIVERSAL::isa($options, "Tangram::Type")) 71 3 74
Condition66%2/3
ref $options eq "HASH" or UNIVERSAL::isa($options, "Tangram::Type")
l!l&&r!l&&!r
7103
MC/DC50%1/2
ref $options eq "HASH" or UNIVERSAL::isa($options, "Tangram::Type")
ref $options eq "HASH" UNIVERSAL::isa($options, "Tangram::Type")
Truth table66%2/3
A or B
A= ref $options eq "HASH"B= UNIVERSAL::isa($options, "Tangram::Type")
ABresult
1X1
011
000
2408 UNIVERSAL::isa($options, 'Tangram::Type')) {
2409 ($check_func, $default, $required, $cleaner,
2410 $companion, $base_type, $load)
2411 71 = @{$options}{qw(check_func init_default
2412 required destroy_func
2413 companion class load)};
2414 }
2415  
2416 # Fill their settings with info from defaults
2417 74 ▶ if (ref $def eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $def eq "HASH") 74 0 74
2418  
2419 # try to magically parse their options
2420 74 ▶ if ( $def->{parse} and !($check_func and $cleaner) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{"parse"} and not $check_func && $cleaner) 22 52 74
Condition66%4/6
$def->{"parse"} and not $check_func && $cleaner
!ll&&!rl&&r
52022
$check_func && $cleaner
!ll&&!rl&&r
2020
MC/DC33%1/3
$def->{"parse"} and not $check_func && $cleaner
$def->{"parse"} $check_func $cleaner
Truth table75%3/4
A and not(B && C)
A= $def->{"parse"}B= $check_funcC= $cleaner
ABCresult
0XX0
1110
10X1
1101
2421 22 my @a = $def->{parse}->($attribute, $options);
2422 22 ▶ $check_func ||= $a[0];
Condition66%2/3
$check_func ||= $a[0]
l!l&&r!l&&!r
2200
MC/DC0%0/2
$check_func ||= $a[0]
$check_func $a[0]
Truth table66%2/3
A ||= B
A= $check_funcB= $a[0]
ABresult
1X1
011
000
2423 22 ▶ $cleaner ||= $a[1];
Condition33%1/3
$cleaner ||= $a[1]
l!l&&r!l&&!r
0022
MC/DC0%0/2
$cleaner ||= $a[1]
$cleaner $a[1]
Truth table33%1/3
A ||= B
A= $cleanerB= $a[1]
ABresult
1X1
011
000
2424 }
2425  
2426 # fall back to defaults for this class
2427 74 ▶ $load ||= $def->{load};
Condition66%2/3
$load ||= $def->{"load"}
l!l&&r!l&&!r
0731
MC/DC50%1/2
$load ||= $def->{"load"}
$load $def->{"load"}
Truth table66%2/3
A ||= B
A= $loadB= $def->{"load"}
ABresult
1X1
011
000
2428 74 ▶ $check_func ||= $def->{check_func};
Condition66%2/3
$check_func ||= $def->{"check_func"}
l!l&&r!l&&!r
24500
MC/DC0%0/2
$check_func ||= $def->{"check_func"}
$check_func $def->{"check_func"}
Truth table66%2/3
A ||= B
A= $check_funcB= $def->{"check_func"}
ABresult
1X1
011
000
2429 74 ▶ $cleaner ||= $def->{destroy_func};
Condition66%2/3
$cleaner ||= $def->{"destroy_func"}
l!l&&r!l&&!r
03341
MC/DC50%1/2
$cleaner ||= $def->{"destroy_func"}
$cleaner $def->{"destroy_func"}
Truth table66%2/3
A ||= B
A= $cleanerB= $def->{"destroy_func"}
ABresult
1X1
011
000
2430 74 ▶ $default = $def->{init_default} unless defined $default;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default 70 4 74
2431 }
2432  
2433 # load a Tangram::Type module, if specified
2434 74 ▶ unless ($no_tangram or not defined $load) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_tangram or not defined $load) 4 70 74
Condition66%2/3
$no_tangram or not defined $load
l!l&&r!l&&!r
7004
MC/DC50%1/2
$no_tangram or not defined $load
$no_tangram not defined $load
Truth table66%2/3
A or not(B)
A= $no_tangramB= not defined $load
ABresult
1X1
011
000
2435 4 ▶ if (!exists $INC{$load}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $INC{$load}) 4 0 4
2436 4 eval 'require $load';
2437 4 ▶ $no_tangram = 1 if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 4 0 4
2438 }
2439 }
2440  
2441 # everything must be checked!
2442 74 ▶ croak("No check function for ${class}\->$attribute "
Branch: 1 branch
BranchTrueFalseTotal
unless ref $check_func eq "CODE" 0 74 74
2443 ."(type $type); set \$Class::Tangram::defaults"
2444 ."{backref} to a sub (eg, \&Class::Tangram::"
2445 ."check_nothing)")
2446 unless (ref $check_func eq "CODE");
2447  
2448 carp("re-defining attribute `$attribute' in subclass "
2449 ."`$class'") if $^W and
2450 74 ▶ exists $types{$class}->{$attribute} and not $dontcarp;
Branch: 1 branch
BranchTrueFalseTotal
if $^W and exists $Class::Tangram::types{$class}{$attribute} and not $dontcarp 0 74 74
Condition50%3/6
$^W and exists $Class::Tangram::types{$class}{$attribute} and not $dontcarp
!ll&&!rl&&r
7400
$^W and exists $Class::Tangram::types{$class}{$attribute}
!ll&&!rl&&r
3710
MC/DC0%0/3
$^W and exists $Class::Tangram::types{$class}{$attribute} and not $dontcarp
$^W exists $Class::Tangram::types{$class}{$attribute} not $dontcarp
Truth table50%2/4
A and B and not(C)
A= $^WB= exists $Class::Tangram::types{$class}{$attribute}C= not $dontcarp
ABCresult
0XX0
10X0
1100
1111
2451  
2452 74 $types{$class}->{$attribute} = $type;
2453 74 $check{$class}->{$attribute} = $check_func;
2454 {
2455 74 local ($^W) = 0;
2456  
2457 # build an appropriate "get_attribute" method, and
2458 # define other per-type methods
2459 74 my ($get_closure, $set_closure, $is_assoc,
2460 $method_type);
2461  
2462 # implement with closures for speed
2463 74 ▶ if ( $type =~ m/^i?(set|array|hash|ref)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^i?(set|array|hash|ref)$/) { } 33 41 74
2464 33 $method_type = $1;
2465 33 $is_assoc = 1;
2466 33 $get_closure = "_get_X_$method_type";
2467 33 $set_closure = "_set_X_$method_type";
2468 } else {
2469 # GET_$attribute (scalar)
2470 # return value only
2471 41 ▶ $get_closure = sub { $_[0]->{$attribute}; };
Subroutine __ANON__: called CC=1 SCAR=0.0
2472 }
2473  
2474 # SET_$attribute (all)
2475 74 my $checkit = \$check{$class}->{$attribute};
2476  
2477 74 ▶ unless ($is_assoc or $set_closure) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_assoc or $set_closure) 41 33 74
Condition66%2/3
$is_assoc or $set_closure
l!l&&r!l&&!r
33041
MC/DC50%1/2
$is_assoc or $set_closure
$is_assoc $set_closure
Truth table66%2/3
A or B
A= $is_assocB= $set_closure
ABresult
1X1
011
000
2478 # `required' hack for strings - duplicate the code
2479 # to avoid the following string comparison for
2480 # every set
2481 41 ▶ if ( $type eq "string" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'string') { } 22 19 41
2482 128 ▶ $set_closure = sub {
Subroutine __ANON__: called CC=8 SCAR=21.1
2483 128 my $self = shift;
2484 128 my $value = shift;
2485 128 my $err = '';
2486 128 ▶ if ( defined $value and length $value ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $value and length $value) { } 118 10 128
elsif ($required) { } 4 6 10
elsif (defined $required) { } 6 0 6
Condition66%2/3
defined $value and length $value
!ll&&!rl&&r
010118
MC/DC50%1/2
defined $value and length $value
defined $value length $value
Truth table66%2/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
2487 118 $ {$checkit}->(\$value);
2488 } elsif ( $required ) {
2489 4 $err = "value is required";
2490 } elsif ( defined $required ) {
2491 6 ▶ $err = "value must be defined"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 6 6
2492 unless defined $value;
2493 }
2494 120 ▶ $err && croak
Branch: 1 branch
BranchTrueFalseTotal
$err and croak("value failed type check - $class->" . "set_$attribute('" . ($value || "") . "') ($err)") 4 116 120
Condition50%1/2
$value || ''
l!l
04
MC/DC0%0/1
$value || ''
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
2495 ("value failed type check - ${class}->"
2496 ."set_$attribute('".($value || '')."') ($err)");
2497 116 $self->{$attribute} = $value;
2498 }
2499 22 } else {
2500 48 ▶ $set_closure = sub {
Subroutine __ANON__: called CC=7 SCAR=27.0
2501 48 my $self = shift;
2502 48 my $value = shift;
2503 48 my $err = '';
2504 48 ▶ if ( defined $value ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $value) { } 48 0 48
elsif ($required) { } 0 0 0
elsif (defined $required) { } 0 0 0
2505 48 $ {$checkit}->(\$value);
2506 } elsif ( $required ) {
2507 0 $err = "value is required";
2508 } elsif ( defined $required ) {
2509 0 ▶ $err = "value must be defined"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
2510 unless defined $value;
2511 }
2512 39 ▶ $err && croak
Branch: 1 branch
BranchTrueFalseTotal
$err and croak("value failed type check - $class->" . "set_$attribute('" . ($value || "") . "') ($err)") 0 39 39
Condition0%0/2
$value || ''
l!l
00
MC/DC0%0/1
$value || ''
$value
Truth table0%0/2
A
A= $value
Aresult
11
00
2513 ("value failed type check - ${class}->"
2514 ."set_$attribute('".($value || '')."') ($err)");
2515 39 $self->{$attribute} = $value;
2516 }
2517 19 }
2518 }
2519  
2520 # flat hashes & arrays
2521 74 ▶ if ( $type =~ m/^flat_(array|hash)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^flat_(array|hash)$/) 2 72 74
2522 2 ▶ if ($1 eq "hash") {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq 'hash') { } 1 1 2
2523 5 ▶ $get_closure = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
2524 5 my $self = shift;
2525 5 ▶ my $a = ($self->{$attribute} ||= {});
Condition100%2/2
$self->{$attribute} ||= {}
l!l
41
MC/DC100%1/1
$self->{$attribute} ||= {}
$self->{$attribute}
Truth table100%2/2
A
A= $self->{$attribute}
Aresult
11
00
2526 5 ▶ return (wantarray ? values %{ $a }
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 4 5
2527 : $a);
2528 }
2529 1 } else {
2530 5 ▶ $get_closure = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
2531 5 my $self = shift;
2532 5 ▶ my $a = ($self->{$attribute} ||= []);
Condition100%2/2
$self->{$attribute} ||= []
l!l
41
MC/DC100%1/1
$self->{$attribute} ||= []
$self->{$attribute}
Truth table100%2/2
A
A= $self->{$attribute}
Aresult
11
00
2533 5 ▶ return (wantarray ? @{ $a } : $a);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 4 5
2534 }
2535 1 }
2536 }
2537  
2538 # now collect the closures
2539 74 my ($getter, $setter)
2540 = ("get_$attribute", "set_$attribute");
2541  
2542 $accessors{$target_pkg."::".$getter} =
2543 (ref $get_closure ? $get_closure
2544 151 ▶ : sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2545 151 my $self = shift;
2546 151 return $self->$get_closure($attribute, @_);
2547 74 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
ref $get_closure ? : 41 33 74
2548 $accessors{$target_pkg."::".$setter} =
2549 (ref $set_closure ? $set_closure
2550 120 ▶ : sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2551 120 my $self = shift;
2552 120 return $self->$set_closure
2553 ($base_type, $companion, $attribute, @_);
2554 74 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
ref $set_closure ? : 41 33 74
2555  
2556 74 ▶ if ($is_assoc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_assoc) { } 33 41 74
2557  
2558 33 foreach my $func (qw(includes insert replace
2559 pairs size clear remove
2560 push pop shift unshift
2561 splice)) {
2562 396 my $method = $target_pkg."::".$attribute."_".$func;
2563 396 my $real_method =
2564 "_${func}_X_$method_type";
2565 $accessors{$method} =
2566 123 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2567 123 my $self = shift;
2568 123 return $self->$real_method($attribute, @_);
2569 }
2570 396 }
2571  
2572 # XXX - use `Want' to return lvalue subs here
2573 24 ▶ $accessors{$target_pkg."::$attribute"} = sub {
Subroutine __ANON__: called CC=6 SCAR=23.5
2574 24 my $self = shift;
2575 24 ▶ if ( @_ && looks_like_KVKV(@_) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ and looks_like_KVKV(@_)) { } 0 24 24
elsif (not @_ or looks_like_KK(@_)) { } 24 0 24
Condition66%2/3
@_ and looks_like_KVKV(@_)
!ll&&!rl&&r
2220
MC/DC0%0/2
@_ and looks_like_KVKV(@_)
@_ looks_like_KVKV(@_)
Truth table66%2/3
A and B
A= @_B= looks_like_KVKV(@_)
ABresult
0X0
100
111
2576 0 ▶ carp("The OO Police say change your call "
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
2577 ."to ->set_$attribute") if ($^W);
2578 0 return $self->$setter(@_);
2579 ▶ } elsif ( !@_ || looks_like_KK(@_) ) {
Condition66%2/3
not @_ or looks_like_KK(@_)
l!l&&r!l&&!r
2220
MC/DC0%0/2
not @_ or looks_like_KK(@_)
not @_ looks_like_KK(@_)
Truth table66%2/3
not(A) or B
A= not @_B= looks_like_KK(@_)
ABresult
1X1
011
000
2580 24 return $self->$getter(@_);
2581 } else {
2582 0 croak("Ambiguous argument list "
2583 ."passed to ${class}::"
2584 ."${attribute}");
2585 }
2586 }
2587  
2588 33 } else {
2589  
2590 # XXX - use `Want' to return lvalue subs here
2591 28 ▶ $accessors{$target_pkg."::$attribute"} = sub {
Subroutine __ANON__: called CC=3 SCAR=15.1
2592 28 my $self = shift;
2593 28 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 28 28
2594 0 ▶ carp("The OO Police say change your call "
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
2595 ."to ->set_$attribute") if ($^W);
2596 0 return $self->$setter(@_);
2597 } else {
2598 28 return $self->$getter(@_);
2599 }
2600 }
2601  
2602 41 }
2603  
2604 74 ▶ $cleaners{$class}->{$attribute} = $cleaner
Branch: 1 branch
BranchTrueFalseTotal
if defined $cleaner 33 41 74
2605 if (defined $cleaner);
2606 74 ▶ $init_defaults{$class}->{$attribute} = $default
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 19 55 74
2607 if (defined $default);
2608 74 ▶ $required_attributes{$class}->{$attribute} = $required
Branch: 1 branch
BranchTrueFalseTotal
if defined $required 3 71 74
2609 if (defined $required);
2610 74 ▶ $attribute_options{$class}->{$attribute} =
Condition50%1/2
$options || {}
l!l
740
MC/DC0%0/1
$options || {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
2611 ( $options || {} );
2612 74 ▶ $companions{$class}->{$attribute} = $companion
Branch: 1 branch
BranchTrueFalseTotal
if defined $companion 22 52 74
2613 if (defined $companion);
2614  
2615 }
2616 74 return \%accessors;
2617 }
2618  
2619 =back
2620  
2621 =head2 Run-time type information
2622  
2623 It is possible to access the data structures that Class::Tangram uses
2624 internally to verify attributes, create objects and so on.
2625  
2626 This should be considered a B<HIGHLY EXPERIMENTAL> interface to
2627 B<INTERNALS> of Class::Tangram.
2628  
2629 Class::Tangram keeps seven internal hashes:
2630  
2631 =over
2632  
2633 =item C<%types>
2634  
2635 C<$types{$class}-E<gt>{$attribute}> is the tangram type of each attribute,
2636 ie "ref", "iset", etc. See L<Tangram::Type>.
2637  
2638 =item C<%attribute_options>
2639  
2640 C<$attribute_options{$class}-E<gt>{$attribute}> is the options hash
2641 for a given attribute.
2642  
2643 =item C<%required_attributes>
2644  
2645 C<$required_attributes{$class}-E<gt>{$attribute}> is the 'required'
2646 option setting for a given attribute.
2647  
2648 =item C<%check>
2649  
2650 C<$check{$class}-E<gt>{$attribute}> is a function that will be passed
2651 a reference to the value to be checked and either throw an exception
2652 (die) or return true.
2653  
2654 =item C<%cleaners>
2655  
2656 C<$attribute_options{$class}-E<gt>{$attribute}> is a reference to a
2657 destructor function for that attribute. It is called as an object
2658 method on the object being destroyed, and should ensure that any
2659 circular references that this object is involved in get cleared.
2660  
2661 =item C<%abstract>
2662  
2663 C<$abstract-E<gt>{$class}> is set if the class is abstract
2664  
2665 =item C<%init_defaults>
2666  
2667 C<$init_defaults{$class}-E<gt>{$attribute}> represents what an
2668 attribute is set to automatically if it is not specified when an
2669 object is created. If this is a scalar value, the attribute is set to
2670 the value. If it is a function, then that function is called (as a
2671 method) and should return the value to be placed into that attribute.
2672 If it is a hash ref or an array ref, then that structure is COPIED in
2673 to the new object. If you don't want that, you can do something like
2674 this:
2675  
2676 [...]
2677 flat_hash => {
2678 attribute => {
2679 init_default => sub { { key => "value" } },
2680 },
2681 },
2682 [...]
2683  
2684 Now, every new object will share the same hash for that attribute.
2685  
2686 =item C<%companions>
2687  
2688 Any "Companion" relationships between attributes, that are to be
2689 treated as linked pairs of relationships; deleting object A from
2690 container B of object C will also cause object C to be removed from
2691 container D of object A.
2692  
2693 =back
2694  
2695 There are currently four functions that allow you to access parts of
2696 this information.
2697  
2698 =over
2699  
2700 =item Class::Tangram::attribute_options($class)
2701  
2702 Returns a hash ref to a data structure from attribute names to the
2703 option hash for that attribute.
2704  
2705 =cut
2706  
2707 0 ▶sub attribute_options {
Subroutine attribute_options: not called CC=1 SCAR=6.9
Pod documented
2708 0 my $class = shift;
2709 0 return $attribute_options{$class};
2710 }
2711  
2712 =item Class::Tangram::attribute_types($class)
2713  
2714 Returns a hash ref from attribute names to the tangram type for that
2715 attribute.
2716  
2717 =cut
2718  
2719 0 ▶sub attribute_types {
Subroutine attribute_types: not called CC=1 SCAR=6.9
Pod documented
2720 0 my $class = shift;
2721 0 return $types{$class};
2722 }
2723  
2724 =item Class::Tangram::required_attributes($class)
2725  
2726 Returns a hash ref from attribute names to the 'required' option setting for
2727 that attribute. May also be called as a method, as in
2728 C<$instance-E<gt>required_attributes>.
2729  
2730 =cut
2731  
2732 0 ▶sub required_attributes {
Subroutine required_attributes: not called CC=2 SCAR=17.9
Pod documented
2733 0 ▶ my $class = ref $_[0] || $_[0];
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
2734 0 return $required_attributes{$class};
2735 }
2736  
2737 =item Class::Tangram::init_defaults($class)
2738  
2739 Returns a hash ref from attribute names to the default intial values for
2740 that attribute. May also be called as a method, as in
2741 C<$instance-E<gt>init_defaults>.
2742  
2743 =cut
2744  
2745 0 ▶sub init_defaults {
Subroutine init_defaults: not called CC=2 SCAR=17.9
Pod documented
2746 0 ▶ my $class = ref $_[0] || $_[0];
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
2747 0 return $init_defaults{$class};
2748 }
2749  
2750 =item Class::Tangram::companions($class)
2751  
2752 Returns a hash ref from attribute names to the default intial values for
2753 that attribute. May also be called as a method, as in
2754 C<$instance-E<gt>init_defaults>.
2755  
2756 =cut
2757  
2758 0 ▶sub companions {
Subroutine companions: not called CC=3 SCAR=24.8
Pod documented
2759 0 ▶ my $class = ref $_[0] || $_[0];
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
2760 0 ▶ if (!defined($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $class) { } 0 0 0
2761 0 return keys %companions;
2762 } else {
2763 0 return $companions{$class};
2764 }
2765 }
2766  
2767 =item Class::Tangram::known_classes
2768  
2769 This function returns a list of all the classes that have had their
2770 object schema imported by Class::Tangram.
2771  
2772 =cut
2773  
2774 0 ▶sub known_classes {
Subroutine known_classes: not called CC=1 SCAR=6.9
Pod documented
2775 0 return keys %types;
2776 }
2777  
2778 =item Class::Tangram::is_abstract($class)
2779  
2780 This function returns true if the supplied class is abstract.
2781  
2782 =cut
2783  
2784 0 ▶sub is_abstract {
Subroutine is_abstract: not called CC=3 SCAR=24.8
Pod documented
2785 0 my $class = shift;
2786 0 ▶ $class eq "Class::Tangram" && ($class = shift);
Branch: 1 branch
BranchTrueFalseTotal
$class eq "Class::Tangram" and $class = shift() 0 0 0
2787  
2788 0 ▶ exists $cleaners{$class} or import_schema($class);
Branch: 1 branch
BranchTrueFalseTotal
exists $Class::Tangram::cleaners{$class} or import_schema($class) 0 0 0
2789 }
2790  
2791 =item Class->set_init_default(attribute => $value);
2792  
2793 Sets the default value on an attribute for newly created "Class"
2794 objects, as if it had been declared with init_default. Can be called
2795 as a class or an instance method.
2796  
2797 =cut
2798  
2799 2 ▶sub set_init_default {
Subroutine set_init_default: called CC=4 SCAR=14.0
Pod documented
2800 2 my $invocant = shift;
2801 2 ▶ my $class = ref $invocant || $invocant;
Condition66%2/3
ref $invocant || $invocant
l!l&&r!l&&!r
110
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table66%2/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
2802  
2803 2 ▶ exists $init_defaults{$class} or import_schema($class);
Branch: 1 branch
BranchTrueFalseTotal
exists $Class::Tangram::init_defaults{$class} or import_schema($class) 2 0 2
2804  
2805 2 ▶ while ( my ($attribute, $value) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 2 2 4
2806 2 $init_defaults{$class}->{$attribute} = $value;
2807 }
2808 }
2809  
2810 =back
2811  
2812 =cut
2813  
2814 # a little embedded package
2815  
2816 package Tangram::Transient;
2817  
2818 4 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
2819 4 ▶ eval "use base qw(Tangram::Type)";
Subroutine BEGIN: called CC=1 SCAR=0.0
2820 4 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 4 0 4
2821 # no tangram
2822 } else {
2823 0 $Tangram::Schema::TYPES{transient} = bless {}, __PACKAGE__;
2824 }
2825 }
2826  
2827 0 ▶sub coldefs { }
Subroutine coldefs: not called CC=1 SCAR=0.0
2828  
2829 0 ▶sub get_exporter { }
Subroutine get_exporter: not called CC=1 SCAR=0.0
2830 0 ▶sub get_importer { }
Subroutine get_importer: not called CC=1 SCAR=0.0
2831  
2832 0 ▶sub get_import_cols {
Subroutine get_import_cols: not called CC=1 SCAR=6.9
2833 # print "Get_import_cols:" , Dumper \@_;
2834 0 return ();
2835 }
2836  
2837 =head1 SEE ALSO
2838  
2839 L<Tangram::Schema>
2840  
2841 B<A guided tour of Tangram, by Sound Object Logic.>
2842  
2843 http://www.soundobjectlogic.com/tangram/guided_tour/fs.html
2844  
2845 =head1 DEPENDENCIES
2846  
2847 The following modules are required to be installed to use
2848 Class::Tangram:
2849  
2850 Set::Object => 1.02
2851 Test::Simple => 0.18
2852 Date::Manip => 5.21
2853  
2854 Test::Simple and Date::Manip are only required to run the test suite.
2855  
2856 If you find Class::Tangram passes the test suite with earlier versions
2857 of the above modules, please send me an e-mail.
2858  
2859 =head2 MODULE RELEASE
2860  
2861 This is Class::Tangram version 1.14.
2862  
2863 =head1 BUGS/TODO
2864  
2865 =over
2866  
2867 =item *
2868  
2869 Inside an over-ridden C<$obj->set_attribute> function, it is not
2870 possible to call C<$self->SUPER::set_attribute>, because that function
2871 does not exist in any superclass' namespace. So, you have to modify
2872 your own hash directly - ie
2873  
2874 $self->{attribute} = $value;
2875  
2876 Instead of the purer OO
2877  
2878 $self->SUPER::set_attribute($value);
2879  
2880 Solutions to this problem may involve creating an intermediate
2881 super-class that contains those functions, and then replacing
2882 C<Class::Tangram> in C<@Class::ISA> with the intermediate class.
2883  
2884 =item *
2885  
2886 Container enhancements;
2887  
2888 =over
2889  
2890 =item copy constructor
2891  
2892 The copy constructor now automatically duplicates
2893  
2894 =back
2895  
2896  
2897 =back
2898  
2899  
2900 - $obj->new() should take a copy of containers etc
2901  
2902 New `array' functions:
2903 - $obj->attr_push()
2904  
2905 * Container notification system
2906  
2907 - all $obj->attr_do functions call $obj->set_attr to provide a
2908 single place to catch modifications of that attribute
2909  
2910 -
2911  
2912 *
2913  
2914 * back-reference notification system
2915  
2916 There should be more functions for breaking loops; in particular, a
2917 standard function called C<drop_refs($obj)>, which replaces references
2918 to $obj with the appropriate C<Tangram::RefOnDemand> object so that an
2919 object can be unloaded via C<Tangram::Storage->unload()> and actually
2920 have a hope of being reclaimed. Another function that would be handy
2921 would be a deep "mark" operation for manual mark & sweep garbage
2922 collection.
2923  
2924 Need to think about writing some functions using C<Inline> for speed.
2925 One of these days...
2926  
2927 Allow C<init_default> values to be set in a default import function?
2928  
2929 ie
2930  
2931 use MyClassTangramObject -defaults => { foo => "bar" };
2932  
2933 =head1 AUTHOR
2934  
2935 Sam Vilain, <sam@vilain.net>
2936  
2937 =head2 CREDITS
2938  
2939 # Some modifications
2940 # Copyright © 2001 Micro Sharp Technologies, Inc., Vancouver, WA, USA
2941 # Author: Karl M. Hegbloom <karlheg@microsharp.com>
2942 # Perl Artistic Licence.
2943  
2944 Many thanks to Charles Owens and David Wheeler for their feedback,
2945 ideas, patches and bug testing.
2946  
2947 =cut
2948  
2949 4 69;
2950  
2951 __END__
« Tangram/Generator.pm ↑ Summary Tangram/Generator/Stub.pm »