| 1 | package EntityModel::Class; | ||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Helper module for generating class definitions | ||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use feature (); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||
| 10 | 2 | our $VERSION = '0.016'; | |||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | EntityModel::Class - define class definition | ||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||
| 18 | Version 0.016 | ||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||
| 22 | package Thing; | ||||||||||||||||||||||||||||||||||||||
| 23 | use EntityModel::Class { | ||||||||||||||||||||||||||||||||||||||
| 24 | name => 'string', | ||||||||||||||||||||||||||||||||||||||
| 25 | items => { type => 'array', subclass => 'string' } | ||||||||||||||||||||||||||||||||||||||
| 26 | }; | ||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||
| 28 | package main; | ||||||||||||||||||||||||||||||||||||||
| 29 | my $thing = Thing->new; | ||||||||||||||||||||||||||||||||||||||
| 30 | $thing->name('A thing'); | ||||||||||||||||||||||||||||||||||||||
| 31 | $thing->items->push('an entry'); | ||||||||||||||||||||||||||||||||||||||
| 32 | $thing->items->push('another entry'); | ||||||||||||||||||||||||||||||||||||||
| 33 | print "Have " . $thing->items->count . " items\n"; | ||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||
| 35 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||
| 37 | Applies a class definition to a package. Automatically includes strict, warnings, error handling and other | ||||||||||||||||||||||||||||||||||||||
| 38 | standard features without needing to copy and paste boilerplate code. | ||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||
| 40 | =head1 USAGE | ||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||
| 42 | NOTE: This is mainly intended for use with L<EntityModel> only, please consider L<Moose> or similar for other | ||||||||||||||||||||||||||||||||||||||
| 43 | projects. | ||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||
| 45 | Add EntityModel::Class near the top of the target package: | ||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||
| 47 | package Test; | ||||||||||||||||||||||||||||||||||||||
| 48 | use EntityModel::Class { }; | ||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||
| 50 | The hashref parameter contains the class definition. Each key is the name of an attribute for the class, | ||||||||||||||||||||||||||||||||||||||
| 51 | with the exception of the following underscore-prefixed keys: | ||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||
| 53 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||
| 55 | =item * C<_vcs> - version control system information, a plain string containing information about the last | ||||||||||||||||||||||||||||||||||||||
| 56 | changed revision and author for this file. | ||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||
| 58 | use EntityModel::Class { _vcs => '$Id$' }; | ||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||
| 60 | =item * C<_isa> - set up the parents for this class, similar to C<use parent>. | ||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||
| 62 | use EntityModel::Class { _isa => 'DateTime' }; | ||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||
| 64 | =back | ||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||
| 66 | An attribute definition will typically create an accessor with the same name, and depending on type may | ||||||||||||||||||||||||||||||||||||||
| 67 | also include some additional helper methods. | ||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||
| 69 | Available types include: | ||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||
| 71 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||
| 73 | =item * C<string> - simple string scalar value. | ||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||
| 75 | use EntityModel::Class { name => { type => 'string' } }; | ||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||
| 77 | =item * C<array> - an array of objects, provide the object type as the subclass parameter | ||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||
| 79 | use EntityModel::Class { childNodes => { type => 'array', subclass => 'Node' } }; | ||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||
| 81 | =item * C<hash> - hash of objects of subclass type | ||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||
| 83 | use EntityModel::Class { authorMap => { type => 'hash', subclass => 'Author' } }; | ||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||
| 85 | =back | ||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||
| 87 | If the type (or subclass) contains '::', or starts with a Capitalised letter, then it will be treated | ||||||||||||||||||||||||||||||||||||||
| 88 | as a class. All internal type names are lowercase. | ||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||
| 90 | You can also set the scope on a variable, which defines whether it should be include when exporting or | ||||||||||||||||||||||||||||||||||||||
| 91 | importing: | ||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||
| 93 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||
| 95 | =item * C<private> - private attributes are not exported or visible in attribute lists | ||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||
| 97 | use EntityModel::Class { authorMap => { type => 'hash', subclass => 'Author', scope => 'private' } }; | ||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||
| 99 | =item * C<public> (default) - public attributes are included in export/import, and will be visible when listing attributes for the class | ||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||
| 101 | use EntityModel::Class { name => { type => 'string', scope => 'public' } }; | ||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||
| 103 | =back | ||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||
| 105 | You can also specify actions to take when a variable is changed, to support internal attribute observers, | ||||||||||||||||||||||||||||||||||||||
| 106 | by specifying the C<watch> parameter. This takes a hashref with key corresponding to the attribute to watch, | ||||||||||||||||||||||||||||||||||||||
| 107 | and value indicating the method on that object. For example, C<page => 'path'> would update whenever the | ||||||||||||||||||||||||||||||||||||||
| 108 | C<path> mutator is called on the C<page> attribute. This is intended for use with hash and array containers, | ||||||||||||||||||||||||||||||||||||||
| 109 | rather than classes or simple types. | ||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||
| 111 | package Compendium; | ||||||||||||||||||||||||||||||||||||||
| 112 | use EntityModel::Class { | ||||||||||||||||||||||||||||||||||||||
| 113 | authors => { type => 'array', subclass => 'Author' }, | ||||||||||||||||||||||||||||||||||||||
| 114 | authorByName => { type => 'hash', subclass => 'Author', scope => 'private', watch => { authors => 'name' } } | ||||||||||||||||||||||||||||||||||||||
| 115 | }; | ||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||
| 117 | package main; | ||||||||||||||||||||||||||||||||||||||
| 118 | my $c = Compendium->new; | ||||||||||||||||||||||||||||||||||||||
| 119 | $c->authors->push(Author->new("Adams")); | ||||||||||||||||||||||||||||||||||||||
| 120 | $c->authors->push(Author->new("Brecht")); | ||||||||||||||||||||||||||||||||||||||
| 121 | print $c->authorByName->{'Adams'}->id; | ||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||
| 123 | =cut | ||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||
| 125 | 2 | ▶ | use Scalar::Util qw(refaddr); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 126 | 2 | ▶ | use Check::UnitCheck; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 127 | 2 | ▶ | use Module::Load; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 128 | 2 | ▶ | use overload; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||
| 130 | 2 | ▶ | use EntityModel::Log ':all'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 131 | 2 | ▶ | use EntityModel::Array; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 132 | 2 | ▶ | use EntityModel::Hash; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 133 | 2 | ▶ | use EntityModel::Error; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 134 | 2 | ▶ | use EntityModel::Class::Accessor; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 135 | 2 | ▶ | use EntityModel::Class::Accessor::Array; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 136 | 2 | ▶ | use EntityModel::Class::Accessor::Hash; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | 2 | my %classInfo; | |||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||
| 140 | my %CLASS_DEFAULTS; | ||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||
| 142 | =head2 import | ||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||
| 144 | Apply supplied attributes, and load in the following modules: | ||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||
| 146 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||
| 148 | =item use strict; | ||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||
| 150 | =item use warnings; | ||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||
| 152 | =item use feature; | ||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||
| 154 | =item use 5.010; | ||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||
| 156 | =back | ||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||
| 158 | =cut | ||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||
| 160 | 3 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=7 SCAR=20.1
| |||||||||||||||||||||||||||||||||||||||
| 161 | 3 | my $class = __PACKAGE__; | |||||||||||||||||||||||||||||||||||||
| 162 | 3 | my $called_on = shift; | |||||||||||||||||||||||||||||||||||||
| 163 | 3 | my $pkg = caller(0); | |||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||
| 165 | 3 | ▶ | my $info = (ref($_[0]) && ref($_[0]) eq 'HASH') ? $_[0] : { @_ }; # support list of args or hashref | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $_[0] and ref $_[0] eq "HASH"
MC/DC50%1/2
ref $_[0] and ref $_[0] eq "HASH"
ref $_[0]
ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||
| 167 | # Expand 'string' to { type => 'string' } | ||||||||||||||||||||||||||||||||||||||
| 168 | 3 | ▶ | $_ = { type => $_ } foreach grep { !ref($_) && /^[a-z]/i } values %$info; | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
!ref($_) && /^[a-z]/i
MC/DC0%0/2
!ref($_) && /^[a-z]/i
!ref($_)
/^[a-z]/i
Truth table33%1/3
not(A) && B
A= !ref($_)B= /^[a-z]/i
| |||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||
| 170 | # Bail out early if we already have inheritance or have recorded this entry in the master list | ||||||||||||||||||||||||||||||||||||||
| 171 | 3 | ▶ | return if $classInfo{$pkg} || $pkg->isa('EntityModel::BaseClass'); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$classInfo{$pkg} or $pkg->isa("EntityModel::BaseClass")
MC/DC0%0/2
$classInfo{$pkg} or $pkg->isa("EntityModel::BaseClass")
$classInfo{$pkg}
$pkg->isa("EntityModel::BaseClass")
Truth table33%1/3
A or B
A= $classInfo{$pkg}B= $pkg->isa("EntityModel::BaseClass")
| |||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||
| 173 | # Basic setup, including strict and other pragmas | ||||||||||||||||||||||||||||||||||||||
| 174 | 3 | $class->setup($pkg); | |||||||||||||||||||||||||||||||||||||
| 175 | 3 | $class->apply_inheritance($pkg, $info); | |||||||||||||||||||||||||||||||||||||
| 176 | 3 | $class->load_dependencies($pkg, $info); | |||||||||||||||||||||||||||||||||||||
| 177 | 3 | $class->apply_logging($pkg, $info); | |||||||||||||||||||||||||||||||||||||
| 178 | 3 | $class->apply_version($pkg, $info); | |||||||||||||||||||||||||||||||||||||
| 179 | 3 | $class->apply_attributes($pkg, $info); | |||||||||||||||||||||||||||||||||||||
| 180 | 3 | $class->record_class($pkg, $info); | |||||||||||||||||||||||||||||||||||||
| 181 | 3 | 1; | |||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||
| 184 | =head2 record_class | ||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||
| 186 | Add an entry for this class in the central class info hash. | ||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||
| 188 | =cut | ||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||
| 190 | 3 | ▶ | sub record_class { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
record_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 191 | 3 | my ($class, $pkg, $info) = @_; | |||||||||||||||||||||||||||||||||||||
| 192 | 3 | my @attribs = grep { !/^_/ } keys %$info; | |||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | { no strict 'refs'; *{$pkg . '::ATTRIBS'} = sub () { @attribs }; } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 194 | 3 | $classInfo{$pkg} = $info; | |||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||
| 197 | =head2 apply_inheritance | ||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||
| 199 | Set up inheritance as required for this class. | ||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||
| 201 | =cut | ||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||
| 203 | 3 | ▶ | sub apply_inheritance { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
apply_inheritance: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 204 | 3 | my ($class, $pkg, $info) = @_; | |||||||||||||||||||||||||||||||||||||
| 205 | # Inheritance | ||||||||||||||||||||||||||||||||||||||
| 206 | 3 | ▶ | my @inheritFrom = @{ $info->{_isa} // [] }; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$info->{"_isa"} // []
MC/DC0%0/1
$info->{"_isa"} // []
$info->{"_isa"}
Truth table50%1/2
A
A= $info->{"_isa"}
| |||||||||||||||||||||||||||||||||||||||
| 207 | 3 | push @inheritFrom, 'EntityModel::BaseClass'; | |||||||||||||||||||||||||||||||||||||
| 208 | # TODO we want to skip loading if the module has already been loaded or defined | ||||||||||||||||||||||||||||||||||||||
| 209 | # earlier, but there is probably a cleaner way to do this? | ||||||||||||||||||||||||||||||||||||||
| 210 | 3 | Module::Load::load($_) for grep !$pkg->isa($_), @inheritFrom; | |||||||||||||||||||||||||||||||||||||
| 211 | 2 | ▶ | { no strict 'refs'; push @{$pkg . '::ISA'}, @inheritFrom; } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 212 | 3 | delete $info->{_isa}; | |||||||||||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||
| 215 | =head2 load_dependencies | ||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||
| 217 | Load all modules required for classes | ||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||
| 219 | =cut | ||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||
| 221 | 3 | ▶ | sub load_dependencies { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
load_dependencies: called CC=8 SCAR=22.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 222 | 3 | my ($class, $pkg, $info) = @_; | |||||||||||||||||||||||||||||||||||||
| 223 | 3 | ▶ | my @attribs = grep { !/^_/ && !/~~/ } keys %$info; | ||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
/^_/ || /~~/
MC/DC0%0/2
/^_/ || /~~/
/^_/
/~~/
Truth table33%1/3
A || B
A= /^_/B= /~~/
| |||||||||||||||||||||||||||||||||||||||
| 224 | 3 | ▶ | my @classList = grep { $_ && /:/ } map { $info->{$_}->{subclass} // $info->{$_}->{type} } grep { !$info->{$_}->{defer} } @attribs; | ||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
$_ && /:/
$info->{$_}{"subclass"} // $info->{$_}{"type"}
MC/DC50%1/2
$_ && /:/
$_
/:/
MC/DC0%0/2
$info->{$_}{"subclass"} // $info->{$_}{"type"}
$info->{$_}{"subclass"}
$info->{$_}{"type"}
Truth table66%2/3
A && B
A= $_B= /:/
Truth table66%2/3
A // B
A= $info->{$_}{"subclass"}B= $info->{$_}{"type"}
| |||||||||||||||||||||||||||||||||||||||
| 225 | CLASS: | ||||||||||||||||||||||||||||||||||||||
| 226 | 3 | foreach my $c (@classList) { | |||||||||||||||||||||||||||||||||||||
| 227 | 1 | my $file = $c; | |||||||||||||||||||||||||||||||||||||
| 228 | 1 | $file =~ s{::|'}{/}g; | |||||||||||||||||||||||||||||||||||||
| 229 | 1 | $file .= '.pm'; | |||||||||||||||||||||||||||||||||||||
| 230 | 1 | ▶ | if($INC{$file}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 231 | 0 | logDebug("Already in INC: $file"); | |||||||||||||||||||||||||||||||||||||
| 232 | 0 | next CLASS; | |||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||
| 234 | eval { | ||||||||||||||||||||||||||||||||||||||
| 235 | 1 | Module::Load::load($c); | |||||||||||||||||||||||||||||||||||||
| 236 | 0 | 1 | |||||||||||||||||||||||||||||||||||||
| 237 | 1 | ▶ | } or do { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 238 | 1 | ▶ | logError($@) unless $@ =~ /^Can't locate /; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 239 | }; | ||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||
| 243 | =head2 apply_logging | ||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||
| 245 | =cut | ||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||
| 247 | 3 | ▶ | sub apply_logging { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
apply_logging: called CC=6 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 248 | 3 | my ($class, $pkg, $info) = @_; | |||||||||||||||||||||||||||||||||||||
| 249 | # Support logging methods by default, unless explicitly disabled | ||||||||||||||||||||||||||||||||||||||
| 250 | EntityModel::Log->export_to_level(2, $pkg, ':all') | ||||||||||||||||||||||||||||||||||||||
| 251 | 3 | ▶ | if $info->{_log} || !exists $info->{_log}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$info->{"_log"} or not exists $info->{"_log"}
MC/DC0%0/2
$info->{"_log"} or not exists $info->{"_log"}
$info->{"_log"}
not exists $info->{"_log"}
Truth table33%1/3
A or not(B)
A= $info->{"_log"}B= not exists $info->{"_log"}
| |||||||||||||||||||||||||||||||||||||||
| 252 | # Apply any log-level overrides first at package level | ||||||||||||||||||||||||||||||||||||||
| 253 | 3 | ▶ | if(exists $info->{_logMask}->{default}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 254 | 0 | $EntityModel::Log::LogMask{$pkg}->{level} = EntityModel::Log::levelFromString($info->{_logMask}->{default}); | |||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||
| 257 | # ... then at method level | ||||||||||||||||||||||||||||||||||||||
| 258 | 3 | ▶ | if(exists $info->{_logMask}->{methods}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 259 | 0 | my %meth = %{$info->{_logMask}->{methods}}; | |||||||||||||||||||||||||||||||||||||
| 260 | 0 | foreach my $k (keys %meth) { | |||||||||||||||||||||||||||||||||||||
| 261 | 0 | $EntityModel::Log::LogMask{$pkg . '::' . $k}->{level} = EntityModel::Log::levelFromString($meth{$k}); | |||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||
| 266 | =head2 apply_version | ||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||
| 268 | Record the VCS revision information from C<_vcs> attribute. | ||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||
| 270 | =cut | ||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||
| 272 | 3 | ▶ | sub apply_version { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
apply_version: called CC=2 SCAR=9.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 273 | 3 | my ($class, $pkg, $info) = @_; | |||||||||||||||||||||||||||||||||||||
| 274 | # Typically version is provided as an SVN Rev property wrapped in $ signs. | ||||||||||||||||||||||||||||||||||||||
| 275 | 3 | ▶ | if(exists $info->{_vcs}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 276 | 0 | my $v = delete $info->{_vcs}; | |||||||||||||||||||||||||||||||||||||
| 277 | 0 | $class->vcs($pkg, $v); | |||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||
| 281 | =head2 apply_attributes | ||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||
| 283 | =cut | ||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||
| 285 | 3 | ▶ | sub apply_attributes { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
apply_attributes: called CC=9 SCAR=29.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 286 | 3 | my ($class, $pkg, $info) = @_; | |||||||||||||||||||||||||||||||||||||
| 287 | 3 | my %methodList; | |||||||||||||||||||||||||||||||||||||
| 288 | 3 | my @attribs = grep { !/^_/ } keys %$info; | |||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||
| 290 | # Smart match support - 1 to use a default refaddr-based system, coderef for anything else | ||||||||||||||||||||||||||||||||||||||
| 291 | 3 | ▶ | if(my $match = delete $info->{'~~'}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 292 | 0 | ▶ | $class->add_method($pkg, '()', sub () { }); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 293 | 0 | ▶ | if(ref $match) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 294 | 0 | $class->add_method($pkg, '(~~', $match); | |||||||||||||||||||||||||||||||||||||
| 295 | } else { | ||||||||||||||||||||||||||||||||||||||
| 296 | 0 | ▶ | $class->add_method($pkg, '(~~', sub { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||
| 297 | 0 | my ($self, $target) = @_; | |||||||||||||||||||||||||||||||||||||
| 298 | 0 | ▶ | return 0 unless defined($self) && defined($target); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self and defined $target
MC/DC0%0/2
defined $self and defined $target
defined $self
defined $target
Truth table0%0/3
A and B
A= defined $selfB= defined $target
| |||||||||||||||||||||||||||||||||||||||
| 299 | 0 | ▶ | return 0 unless ref($self) && ref($target); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $self and ref $target
MC/DC0%0/2
ref $self and ref $target
ref $self
ref $target
Truth table0%0/3
A and B
A= ref $selfB= ref $target
| |||||||||||||||||||||||||||||||||||||||
| 300 | 0 | ▶ | return 0 unless $self->isa($pkg); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 301 | 0 | ▶ | return 0 unless $target->isa($pkg); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 302 | 0 | ▶ | return 0 unless refaddr($self) == refaddr($target); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 303 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||
| 304 | 0 | }); | |||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||
| 307 | # Update overload cache if we previously invalidated (for smartmatch or other operators), | ||||||||||||||||||||||||||||||||||||||
| 308 | # possibly required if calling L<apply_attributes> at runtime. | ||||||||||||||||||||||||||||||||||||||
| 309 | 0 | bless {}, $pkg; | |||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||
| 312 | # Anything else is an accessor, set it up | ||||||||||||||||||||||||||||||||||||||
| 313 | 3 | foreach my $attr (@attribs) { | |||||||||||||||||||||||||||||||||||||
| 314 | 7 | my $type = $info->{$attr}->{type}; | |||||||||||||||||||||||||||||||||||||
| 315 | 7 | ▶ | if($type eq 'array') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 316 | 1 | %methodList = (%methodList, EntityModel::Class::Accessor::Array->add_to_class($pkg, $attr => $info->{$attr})) | |||||||||||||||||||||||||||||||||||||
| 317 | } elsif($type eq 'hash') { | ||||||||||||||||||||||||||||||||||||||
| 318 | 2 | %methodList = (%methodList, EntityModel::Class::Accessor::Hash->add_to_class($pkg, $attr => $info->{$attr})) | |||||||||||||||||||||||||||||||||||||
| 319 | } else { | ||||||||||||||||||||||||||||||||||||||
| 320 | 4 | %methodList = (%methodList, EntityModel::Class::Accessor->add_to_class($pkg, $attr => $info->{$attr})) | |||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||
| 324 | 3 | $CLASS_DEFAULTS{$pkg} = [ grep { exists $info->{$_}->{default} } @attribs ]; | |||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||
| 326 | # Apply watchers after we've defined the fields - each watcher is field => method | ||||||||||||||||||||||||||||||||||||||
| 327 | 3 | foreach my $watcher (grep { exists $info->{$_}->{watch} } @attribs) { | |||||||||||||||||||||||||||||||||||||
| 328 | 1 | my $w = $info->{$watcher}->{watch}; | |||||||||||||||||||||||||||||||||||||
| 329 | 1 | foreach my $watched (keys %$w) { | |||||||||||||||||||||||||||||||||||||
| 330 | 1 | $class->add_watcher($pkg, $watcher, $watched, $info->{$watched}, $w->{$watched}); | |||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||
| 334 | # Thanks to Check::UnitCheck | ||||||||||||||||||||||||||||||||||||||
| 335 | 2 | ▶ | Check::UnitCheck::unitcheckify(sub { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||
| 336 | # FIXME Can't call any log functions within UNITCHECK | ||||||||||||||||||||||||||||||||||||||
| 337 | 2 | local $::DISABLE_LOG = 1; | |||||||||||||||||||||||||||||||||||||
| 338 | 2 | my %ml = %methodList; | |||||||||||||||||||||||||||||||||||||
| 339 | 2 | $class->add_method($pkg, $_, $ml{$_}) foreach keys %ml; | |||||||||||||||||||||||||||||||||||||
| 340 | 2 | ▶ | $class->add_method($pkg, 'import', sub { }) unless $pkg->can('import'); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 341 | 3 | ▶ | }) if %methodList; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||
| 344 | =head2 add_method | ||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||
| 346 | =cut | ||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||
| 348 | 7 | ▶ | sub add_method { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
add_method: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 349 | 7 | my $class = shift; | |||||||||||||||||||||||||||||||||||||
| 350 | 7 | my ($pkg, $name, $method) = @_; | |||||||||||||||||||||||||||||||||||||
| 351 | 7 | my $sym = $pkg . '::' . $name; | |||||||||||||||||||||||||||||||||||||
| 352 | 7 | logDebug("Add method $sym"); | |||||||||||||||||||||||||||||||||||||
| 353 | 2 | ▶ | { no strict 'refs'; *$sym = $method unless *$sym{CODE}; } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 354 | 7 | return $sym; | |||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||
| 357 | =head2 vcs | ||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||
| 359 | Add a version control system tag to the class. | ||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||
| 361 | =cut | ||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||
| 363 | 0 | ▶ | sub vcs { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
vcs: not called CC=2 SCAR=13.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 364 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||
| 365 | 0 | my $pkg = shift; | |||||||||||||||||||||||||||||||||||||
| 366 | 0 | my $v = shift; | |||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||
| 368 | # Define with empty prototype, which should mean we compile to a constant | ||||||||||||||||||||||||||||||||||||||
| 369 | 0 | ▶ | my $versionSub = sub () { $v }; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 370 | 0 | my $sym = $pkg . '::VCS_INFO'; | |||||||||||||||||||||||||||||||||||||
| 371 | 0 | ▶ | { no strict 'refs'; *$sym = $versionSub unless *$sym{CODE}; } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.7
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||
| 374 | =head2 setup | ||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||
| 376 | Standard module setup - enable strict and warnings, and disable 'import' fallthrough. | ||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||
| 378 | =cut | ||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||
| 380 | 3 | ▶ | sub setup { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
setup: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 381 | 3 | my ($class, $pkg) = @_; | |||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||
| 383 | 3 | strict->import; | |||||||||||||||||||||||||||||||||||||
| 384 | 3 | warnings->import(); | |||||||||||||||||||||||||||||||||||||
| 385 | 3 | feature->import(':5.10'); | |||||||||||||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||
| 389 | =head2 validator | ||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||
| 391 | Basic validation function. | ||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||
| 393 | =cut | ||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||
| 395 | 0 | ▶ | sub validator { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
validator: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 396 | 0 | my $v = shift; | |||||||||||||||||||||||||||||||||||||
| 397 | 0 | my $allowed = $v->{valid}; | |||||||||||||||||||||||||||||||||||||
| 398 | return defined $allowed | ||||||||||||||||||||||||||||||||||||||
| 399 | ? ref $allowed eq 'CODE' | ||||||||||||||||||||||||||||||||||||||
| 400 | 0 | ▶ | ? $allowed : sub { $_[0] eq $allowed } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 401 | 0 | ▶ | : undef; | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||
| 404 | =head2 _attrib_info | ||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||
| 406 | Returns attribute information for a given package's attribute. | ||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||
| 408 | =cut | ||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||
| 410 | 0 | ▶ | sub _attrib_info { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_attrib_info: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||
| 411 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||
| 412 | 0 | my $attr = shift; | |||||||||||||||||||||||||||||||||||||
| 413 | # return unless ref $self; | ||||||||||||||||||||||||||||||||||||||
| 414 | 0 | ▶ | return $classInfo{ref $class || $class}->{$attr}; | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||
| 417 | =head2 has_defaults | ||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||
| 419 | Returns any defaults defined for this class. | ||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||
| 421 | =cut | ||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||
| 423 | 1 | ▶ | sub has_defaults { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
has_defaults: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 424 | 1 | my $class = shift; | |||||||||||||||||||||||||||||||||||||
| 425 | 1 | ▶ | return @{ $CLASS_DEFAULTS{$class} // [] }; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$CLASS_DEFAULTS{$class} // []
MC/DC0%0/1
$CLASS_DEFAULTS{$class} // []
$CLASS_DEFAULTS{$class}
Truth table50%1/2
A
A= $CLASS_DEFAULTS{$class}
| |||||||||||||||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||
| 428 | =head2 add_watcher | ||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||
| 430 | Add watchers as required for all package definitions. | ||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||
| 432 | Call this after all the class definitions have been loaded. | ||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||
| 434 | =cut | ||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||
| 436 | 1 | ▶ | sub add_watcher { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
add_watcher: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 437 | 1 | my $class = shift; | |||||||||||||||||||||||||||||||||||||
| 438 | 1 | my ($pkg, $obj, $target, $attrDef, $meth) = @_; | |||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||
| 440 | # The watcher is called with the new value as add|drop => $v | ||||||||||||||||||||||||||||||||||||||
| 441 | 4 | ▶ | my $sub = sub { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||||||||||||
| 442 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 443 | 4 | my ($action, $v) = @_; | |||||||||||||||||||||||||||||||||||||
| 444 | 4 | ▶ | return unless $v; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 445 | 4 | ▶ | my $k = $meth ? $v->$meth : $v; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 446 | 4 | logDebug("%s for %s with %s", $action, $k, $v); | |||||||||||||||||||||||||||||||||||||
| 447 | 4 | ▶ | if($action eq 'add') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 448 | 3 | $self->$obj->set($k, $v); | |||||||||||||||||||||||||||||||||||||
| 449 | } elsif($action eq 'drop') { | ||||||||||||||||||||||||||||||||||||||
| 450 | 1 | $self->$obj->erase($k); | |||||||||||||||||||||||||||||||||||||
| 451 | } else { | ||||||||||||||||||||||||||||||||||||||
| 452 | 0 | logError("Don't know %s", $_); | |||||||||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||||||||
| 454 | 4 | return $self; | |||||||||||||||||||||||||||||||||||||
| 455 | 1 | }; | |||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||
| 457 | 1 | ▶ | if($attrDef->{type} eq 'array') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 458 | 1 | EntityModel::Class::Accessor::Array->add_watcher($pkg, $target, $sub); | |||||||||||||||||||||||||||||||||||||
| 459 | } else { | ||||||||||||||||||||||||||||||||||||||
| 460 | 0 | ▶ | die "Unknown type " . ($_ // 'undef'); | ||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$_ // "undef"
MC/DC0%0/1
$_ // "undef"
$_
Truth table0%0/2
A
A= $_
| |||||||||||||||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||
| 464 | 2 | 1; | |||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||
| 466 | __END__ | ||||||||||||||||||||||||||||||||||||||