| 1 | package EntityModel::BaseClass; | ||||||||||||||||||||
| 2 | 2 | $EntityModel::BaseClass::VERSION = '0.016'; | |||||||||||||||||||
| 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 Scalar::Util (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use EntityModel::Log ':all'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | =head2 new | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | Basic constructor. Will populate $self with any parameters passed. | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | Returns the new instance. | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | =cut | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=32.3
Pod
documented
| |||||||||||||||||||||
| 18 | 1 | my $class = shift; | |||||||||||||||||||
| 19 | 1 | my %data; | |||||||||||||||||||
| 20 | 1 | ▶ | if(ref($_[0]) eq 'HASH') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | 0 | %data = %{$_[0]}; | |||||||||||||||||||
| 22 | } else { | ||||||||||||||||||||
| 23 | 1 | ▶ | if(@_ % 2) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 24 | 0 | ▶ | logStack("Bad element list for [%s] - %s", $class, join(',', map { $_ // 'undef' } @_)); | ||||||||||||||||||
|
Condition0%0/2
$_ // "undef"
MC/DC0%0/1
$_ // "undef"
$_
Truth table0%0/2
A
A= $_
| |||||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | 1 | %data = @_; | |||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | 1 | my $self = bless \%data, $class; | |||||||||||||||||||
| 29 | 1 | my @defaults = EntityModel::Class::has_defaults($class); | |||||||||||||||||||
| 30 | 1 | ▶ | return $self unless @defaults; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 0 | foreach my $attr (grep { !exists $data{$_} } @defaults) { | |||||||||||||||||||
| 33 | 0 | my $def = EntityModel::Class::_attrib_info($class, $attr); | |||||||||||||||||||
| 34 | 0 | my $v = $def->{default}; | |||||||||||||||||||
| 35 | 0 | ▶ | $v = $v->() if (ref($v) // '') eq 'CODE'; | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
ref $v // ""
MC/DC0%0/1
ref $v // ""
ref $v
Truth table0%0/2
A
A= ref $v
| |||||||||||||||||||||
| 36 | # Still aliased to $self | ||||||||||||||||||||
| 37 | 0 | $data{$attr} = $v; | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | 0 | return $self; | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | =head2 clone | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | Shallow clone implementation. | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | Returns a new instance with a copy of everything in the hashref. | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 0 | ▶ | sub clone { | ||||||||||||||||||
|
Subroutine
clone: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 51 | 0 | my $self = shift; | |||||||||||||||||||
| 52 | 0 | return bless { %$self }, ref $self; | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =head2 dump | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | Simple method to dump out this object and all attributes. | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =cut | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 0 | ▶ | sub dump { | ||||||||||||||||||
|
Subroutine
dump: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||
| 62 | 0 | my $self = shift; | |||||||||||||||||||
| 63 | 0 | ▶ | my $out = shift || sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 64 | 0 | my $k = shift; | |||||||||||||||||||
| 65 | 0 | my $depth = shift; | |||||||||||||||||||
| 66 | 0 | ▶ | my $v = shift // ''; | ||||||||||||||||||
|
Condition0%0/2
shift() // ""
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 67 | 0 | print((' ' x $depth) . "$k = $v\n"); | |||||||||||||||||||
| 68 | 0 | ▶ | }; | ||||||||||||||||||
|
Condition0%0/3
shift() || sub {
my $k = shift();
my $depth = shift();
my $v = shift() // '';
print ' ' x $depth . "$k = $v\n";
}
MC/DC0%0/2
shift() || sub {
my $k = shift();
my $depth = shift();
my $v = shift() // '';
print ' ' x $depth . "$k = $v\n";
}
shift()
sub {
my $k = shift();
my $depth = shift();
my $v = shift() // '';
print ' ' x $depth . "$k = $v\n";
}
Truth table0%0/3
A || B
A= shift()B= sub {
my $k = shift();
my $depth = shift();
my $v = shift() // '';
print ' ' x $depth . "$k = $v\n";
}
| |||||||||||||||||||||
| 69 | 0 | ▶ | my $depth = shift // 0; | ||||||||||||||||||
|
Condition0%0/2
shift() // 0
MC/DC0%0/1
shift() // 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 0 | $out->(ref($self), $depth, $self); | |||||||||||||||||||
| 72 | 0 | foreach my $k (sort $self->ATTRIBS) { | |||||||||||||||||||
| 73 | 0 | my $v = $self->$k(); | |||||||||||||||||||
| 74 | 0 | ▶ | if(eval { $v->can('dump'); }) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 75 | 0 | $out->($k, $depth + 1, ':'); | |||||||||||||||||||
| 76 | 0 | $v->dump($out, $depth + 1); | |||||||||||||||||||
| 77 | } elsif(ref $v eq 'ARRAY') { | ||||||||||||||||||||
| 78 | 0 | $out->($k, $depth + 1, '[' . join(',', @$v) . ']'); | |||||||||||||||||||
| 79 | } elsif(ref $v eq 'HASH') { | ||||||||||||||||||||
| 80 | 0 | $out->($k, $depth + 1, '{' . (map { $_ . ' => ' . $v->{$_} } sort keys %$v) . '}'); | |||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||
| 82 | 0 | $out->($k, $depth + 1, $v); | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | 0 | $self; | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =head2 sap | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | Generate a coderef that takes a weakened value of $self. | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | Usage: | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | push @handler, $obj->sap(sub { | ||||||||||||||||||||
| 95 | my $self = shift; | ||||||||||||||||||||
| 96 | $self->do_something; | ||||||||||||||||||||
| 97 | }); | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 1 | ▶ | sub sap { | ||||||||||||||||||
|
Subroutine
sap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 102 | 1 | my ($self, $sub) = @_; | |||||||||||||||||||
| 103 | 1 | Scalar::Util::weaken $self; | |||||||||||||||||||
| 104 | 4 | ▶ | return sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 105 | 4 | $self->$sub(@_); | |||||||||||||||||||
| 106 | 1 | }; | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 2 | 1; | |||||||||||||||||||