| 1 | package Class::Accessor::Assert; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use base qw(Class::Accessor Class::Data::Inheritable); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp qw(croak confess); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = '1.41'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | sub _mk_accessors { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mk_accessors: called CC=5 SCAR=25.5
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | my ( $self, $maker, @fields ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | $self->mk_classdata("accessor_specs") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | unless $self->can("accessor_specs"); | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | my %spec = $self->parse_fields(@fields); | |||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | $self->accessor_specs( { %spec, %{ $self->accessor_specs || {} } } ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->accessor_specs || {}
MC/DC0%0/1
$self->accessor_specs || {}
$self->accessor_specs
Truth table50%1/2
A
A= $self->accessor_specs
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | $self->SUPER::_mk_accessors( 'rw', keys %spec ); | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | { | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | # additional methods for magic array methods | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | my $class = ref $self || $self; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | # Note how we curry the subs with the lexical "$field": | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # The subs are closures and therefore have access to their lexical | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # scope. Clarity suffers from this, but the performance should be | ||||||||||||||||||||||||||||||||||||||||||
| 27 | # about 25% higher than a cleaner approach due to a saved subroutine | ||||||||||||||||||||||||||||||||||||||||||
| 28 | # call for every ary_*(...) call. | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | for my $field ( grep { $spec{$_}{array} } keys %spec ) { | |||||||||||||||||||||||||||||||||||||||||
| 30 | # foo_push sub | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | *{"${class}::${field}_push"} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | my ( $self, @values ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | $self->{$field} = [] unless defined $self->{$field}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | push @{ $self->{$field} }, @values; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 36 | # foo_pop sub | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | *{"${class}::${field}_pop"} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | return pop @{ $self->{$field} || [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{$field} || []
MC/DC0%0/1
$self->{$field} || []
$self->{$field}
Truth table0%0/2
A
A= $self->{$field}
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 41 | # foo_unshift sub | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | *{"${class}::${field}_unshift"} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | my ( $self, @values ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $self->{$field} = [] unless defined $self->{$field}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | unshift @{ $self->{$field} }, @values; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 47 | # foo_shift sub | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | *{"${class}::${field}_shift"} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | return shift @{ $self->{$field} || [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{$field} || []
MC/DC0%0/1
$self->{$field} || []
$self->{$field}
Truth table0%0/2
A
A= $self->{$field}
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 5 | my ( $self, $stuff ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 5 | ▶ | my $not_a_void_context = eval { %{ $stuff || {} } }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$stuff || {}
MC/DC100%1/1
$stuff || {}
$stuff
Truth table100%2/2
A
A= $stuff
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 5 | ▶ | croak "$stuff doesn't look much like a hash to me" if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 4 | ▶ | if ( $self->can("accessor_specs") ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 4 | my $spec = $self->accessor_specs; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 4 | for my $k ( keys %$spec ) { | |||||||||||||||||||||||||||||||||||||||||
| 63 | confess "Required member $k not given to constructor" | ||||||||||||||||||||||||||||||||||||||||||
| 64 | if $spec->{$k}->{required} | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 11 | ▶ | and not exists $stuff->{$k}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$spec->{$k}{"required"} and not exists $stuff->{$k}
MC/DC100%2/2
$spec->{$k}{"required"} and not exists $stuff->{$k}
$spec->{$k}{"required"}
not exists $stuff->{$k}
Truth table100%3/3
A and not(B)
A= $spec->{$k}{"required"}B= not exists $stuff->{$k}
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | confess "Member $k needs to be of type " . $spec->{$k}->{class} | ||||||||||||||||||||||||||||||||||||||||||
| 67 | if exists $spec->{$k}->{class} | ||||||||||||||||||||||||||||||||||||||||||
| 68 | and exists $stuff->{$k} | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 10 | ▶ | and !UNIVERSAL::isa( $stuff->{$k}, $spec->{$k}->{class} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
exists $spec->{$k}{"class"} and exists $stuff->{$k} and not UNIVERSAL::isa($stuff->{$k}, $spec->{$k}{"class"})
exists $spec->{$k}{"class"} and exists $stuff->{$k}
MC/DC100%3/3
exists $spec->{$k}{"class"} and exists $stuff->{$k} and not UNIVERSAL::isa($stuff->{$k}, $spec->{$k}{"class"})
exists $spec->{$k}{"class"}
exists $stuff->{$k}
not UNIVERSAL::isa($stuff->{$k}, $spec->{$k}{"class"})
Truth table100%4/4
A and B and not(C)
A= exists $spec->{$k}{"class"}B= exists $stuff->{$k}C= not UNIVERSAL::isa($stuff->{$k}, $spec->{$k}{"class"})
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | return $self->SUPER::new($stuff); | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 3 | ▶ | sub set { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=10 SCAR=28.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 3 | ▶ | return shift->SUPER::set(@_) unless $_[0]->can("accessor_specs"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | my ( $self, $key ) = splice( @_, 0, 2 ); | |||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | my $spec = $self->accessor_specs; | |||||||||||||||||||||||||||||||||||||||||
| 79 | return $self->SUPER::set( $key, @_ ) | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | ▶ | if !exists $spec->{$key} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not exists $spec->{$key} or @_ > 1
MC/DC0%0/2
not exists $spec->{$key} or @_ > 1
not exists $spec->{$key}
@_ > 1
Truth table33%1/3
not(A) or B
A= not exists $spec->{$key}B= @_ > 1
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | or @_ > 1; # No support for arrays | ||||||||||||||||||||||||||||||||||||||||||
| 82 | confess "Member $key needs to be of type " . $spec->{$key}->{class} | ||||||||||||||||||||||||||||||||||||||||||
| 83 | if defined $_[0] | ||||||||||||||||||||||||||||||||||||||||||
| 84 | and exists $spec->{$key}->{class} | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 3 | ▶ | and !UNIVERSAL::isa( $_[0], $spec->{$key}->{class} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
defined $_[0] and exists $spec->{$key}{"class"} and not UNIVERSAL::isa($_[0], $spec->{$key}{"class"})
defined $_[0] and exists $spec->{$key}{"class"}
MC/DC33%1/3
defined $_[0] and exists $spec->{$key}{"class"} and not UNIVERSAL::isa($_[0], $spec->{$key}{"class"})
defined $_[0]
exists $spec->{$key}{"class"}
not UNIVERSAL::isa($_[0], $spec->{$key}{"class"})
Truth table50%2/4
A and B and not(C)
A= defined $_[0]B= exists $spec->{$key}{"class"}C= not UNIVERSAL::isa($_[0], $spec->{$key}{"class"})
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | $_[0] = [ $_[0] ] | ||||||||||||||||||||||||||||||||||||||||||
| 88 | if defined $_[0] | ||||||||||||||||||||||||||||||||||||||||||
| 89 | and $spec->{$key}->{array} | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | and ref $_[0] ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $_[0] and $spec->{$key}{"array"} and ref $_[0] ne "ARRAY"
defined $_[0] and $spec->{$key}{"array"}
MC/DC0%0/3
defined $_[0] and $spec->{$key}{"array"} and ref $_[0] ne "ARRAY"
defined $_[0]
$spec->{$key}{"array"}
ref $_[0] ne "ARRAY"
Truth table25%1/4
A and B and C
A= defined $_[0]B= $spec->{$key}{"array"}C= ref $_[0] ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | $self->{$key} = $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | return shift->SUPER::get(@_) unless $_[0]->can("accessor_specs"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my ( $self, $key ) = splice( @_, 0, 2 ); | |||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my $spec = $self->accessor_specs; | |||||||||||||||||||||||||||||||||||||||||
| 99 | return $self->SUPER::get( $key, @_ ) | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if !exists $spec->{$key} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not exists $spec->{$key} or @_ > 1
MC/DC0%0/2
not exists $spec->{$key} or @_ > 1
not exists $spec->{$key}
@_ > 1
Truth table0%0/3
not(A) or B
A= not exists $spec->{$key}B= @_ > 1
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | or @_ > 1; # No support for arrays | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if ( $spec->{$key}{array} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | wantarray | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | ? @{ $self->SUPER::get( $key, @_ ) || [] } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$self->SUPER::get($key, @_) || []
MC/DC0%0/1
$self->SUPER::get($key, @_) || []
$self->SUPER::get($key, @_)
Truth table0%0/2
A
A= $self->SUPER::get($key, @_)
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | : $self->SUPER::get( $key, @_ ); | ||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $self->SUPER::get( $key, @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 1 | ▶ | sub parse_fields { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_fields: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 1 | my ( $self, @fields ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 1 | my %spec; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | for my $f (@fields) { | |||||||||||||||||||||||||||||||||||||||||
| 116 | 3 | my $orig_f = $f; # For error reporting | |||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | my %subspec; | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | # All the tests go here | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 3 | $subspec{required} = $f =~ s/^\+//; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 3 | ▶ | $f =~ s/=(.*)// and $subspec{class} = $1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 3 | $subspec{array} = $f =~ s/^\@//; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 3 | ▶ | $f =~ /^\w+$/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | or croak "Couldn't understand field specification $orig_f"; | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 3 | $spec{$f} = \%subspec; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | return %spec; | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||||||||||||||||