Concierge/Desk/DeferredComponent.pm

filter: Statementstmt 100% 22 / 22 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 3 / 4 Podpod 100% 1 / 1 totaltotal 93.9% 31 / 33 CC 4 SCAR 10.0
CC
4
Cov
96%
AUTOLOAD
4.0
BEGIN
1.0
new
1.0
CRAP 2.7
« Concierge/Desk/UnavailableComponent.pm ↑ Summary Concierge/Desk/Component.pm »
1 package Concierge::Desk::DeferredComponent v0.13.0;
2 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 our $VERSION = 'v0.13.0';
5  
6 # ABSTRACT: Stand-in that defers a 'defer' component's real construction until first use
7  
8 1 our $AUTOLOAD;
9  
10 8 ▶sub new ($class, %args) {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
11 return bless {
12 name => $args{name},
13 class => $args{class},
14 payload => $args{payload},
15 8 _real => undef, # built on first use, cached here once successful
16 }, $class;
17 }
18  
19 10 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=13.9
20 10 my $self = shift;
21  
22 10 my $called = $AUTOLOAD;
23 10 $called =~ s/.*:://;
24 10 ▶ return if $called eq 'DESTROY'; # never fall through to build-on-teardown
Branch: 1 branch
BranchTrueFalseTotal
if $called eq "DESTROY" 0 10 10
25  
26 10 ▶ unless ($self->{_real}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_real"}) 5 5 10
27 5 my $comp;
28 5 my $ok = eval {
29 5 (my $file = $self->{class}) =~ s{::}{/}g;
30 5 require "$file.pm";
31 5 $comp = $self->{class}->new($self->{payload});
32 3 1;
33 };
34 5 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 3 2 5
35 3 $self->{_real} = $comp;
36 } else {
37 # Permanent-for-this-instance failure -- matches
38 # UnavailableComponent precedent exactly; no retry, ever.
39 2 require Concierge::Desk::UnavailableComponent;
40 $self->{_real} = Concierge::Desk::UnavailableComponent->new(
41 2 name => $self->{name}, reason => $@,
42 );
43 }
44 }
45  
46 10 return $self->{_real}->$called(@_);
47 }
48  
49 # Explicit no-op DESTROY. Without this, object teardown would invoke
50 # AUTOLOAD for 'DESTROY' (Perl calls AUTOLOAD for a missing DESTROY just
51 # like any other missing method); the 'return if DESTROY' guard above
52 # only prevents AUTOLOAD from returning early in that case, it does not
53 # avoid the AUTOLOAD call itself -- and critically, without this, object
54 # teardown at the end of every test/request would itself trigger the
55 # deferred build.
56 0 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
57  
58 1 1;
59  
60 __END__
« Concierge/Desk/UnavailableComponent.pm ↑ Summary Concierge/Desk/Component.pm »