| 1 | package Object::Lazy; ## no critic (TidyCode) | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 15 | our $VERSION = '0.16'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 15 | ▶ | use Object::Lazy::Validate; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 18 | ▶ | sub new { ## no critic (ArgUnpacking) | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 18 | my ($class, $params) = Object::Lazy::Validate::validate_new(@_); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 18 | $params = Object::Lazy::Validate::init($params); | |||||||||||||||||||||||||||
| 16 | 17 | my $self = bless $params, $class; | |||||||||||||||||||||||||||
| 17 | 17 | ▶ | if ( exists $params->{ref} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 1 | Object::Lazy::Ref::register($self); | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 17 | return $self; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 14 | ▶ | my $build_object = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 25 | 14 | my ($self, $self_ref) = @_; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 14 | local *__ANON__ = 'BUILD_OBJECT'; ## no critic (LocalVars) | |||||||||||||||||||||||||||
| 28 | 14 | my $built_object = $self->{build}->(); | |||||||||||||||||||||||||||
| 29 | # don't build a second time | ||||||||||||||||||||||||||||
| 30 | 14 | ▶ | $self->{build} = sub { return $built_object }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 14 | ▶ | if ( ! $self->{is_built} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 9 | $self->{is_built} = 1; | |||||||||||||||||||||||||||
| 33 | 9 | ▶ | if ( exists $self->{logger} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 1 | confess('object built'); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 1 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 1 | $self->{logger}->($_); | |||||||||||||||||||||||||||
| 39 | 1 | }; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 14 | ${$self_ref} = $built_object; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 14 | return $built_object; | |||||||||||||||||||||||||||
| 45 | 15 | }; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub DESTROY {} # is not AUTOLOAD | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 6 | ▶ | sub AUTOLOAD { ## no critic (Autoloading ArgUnpacking) | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | 6 | my $self = $_[0]; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 6 | my $method = substr our $AUTOLOAD, 2 + length __PACKAGE__; | |||||||||||||||||||||||||||
| 53 | 6 | my $built_object = $build_object->($self, \$_[0]); | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 6 | return $built_object->$method( @_[1 .. $#_] ); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 5 | ▶ | sub isa { ## no critic (ArgUnpacking) | ||||||||||||||||||||||||||
|
Subroutine
isa: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 5 | my ($self, $class2check) = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | my @isa | ||||||||||||||||||||||||||||
| 62 | = ref $self->{isa} eq 'ARRAY' | ||||||||||||||||||||||||||||
| 63 | 5 | ? @{ $self->{isa} } | |||||||||||||||||||||||||||
| 64 | 5 | ▶ | : ( $self->{isa} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 5 | ▶ | if ( $self->{is_built} || ! @isa ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"is_built"} or not @isa
MC/DC100%2/2
$self->{"is_built"} or not @isa
$self->{"is_built"}
not @isa
Truth table100%3/3
A or not(B)
A= $self->{"is_built"}B= not @isa
| |||||||||||||||||||||||||||||
| 66 | 3 | my $built_object = $build_object->($self, \$_[0]); | |||||||||||||||||||||||||||
| 67 | 3 | return $built_object->isa($class2check); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 2 | CLASS: for my $class (@isa) { | |||||||||||||||||||||||||||
| 70 | 4 | ▶ | $class->isa($class2check) and return 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 1 | my %isa = map { ($_ => undef) } @isa; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | return exists $isa{$class2check}; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 3 | ▶ | sub DOES { ## no critic (ArgUnpacking) | ||||||||||||||||||||||||||
|
Subroutine
DOES: called CC=8 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 3 | my ($self, $class2check) = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 3 | ▶ | UNIVERSAL->can('DOES') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | or confess 'UNIVERSAL 1.04 (Perl 5.10) required for method DOES'; | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | my @does | ||||||||||||||||||||||||||||
| 84 | = ref $self->{DOES} eq 'ARRAY' | ||||||||||||||||||||||||||||
| 85 | 3 | ? @{ $self->{DOES} } | |||||||||||||||||||||||||||
| 86 | 3 | ▶ | : ( $self->{DOES} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | my @isa_and_does = ( | ||||||||||||||||||||||||||||
| 88 | ( | ||||||||||||||||||||||||||||
| 89 | ref $self->{isa} eq 'ARRAY' | ||||||||||||||||||||||||||||
| 90 | 3 | ? @{ $self->{isa} } | |||||||||||||||||||||||||||
| 91 | : ( $self->{isa} ) | ||||||||||||||||||||||||||||
| 92 | 3 | ▶ | ), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | @does, | ||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||
| 95 | 3 | ▶ | if ( $self->{is_built} || ! @isa_and_does ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"is_built"} or not @isa_and_does
MC/DC50%1/2
$self->{"is_built"} or not @isa_and_does
$self->{"is_built"}
not @isa_and_does
Truth table66%2/3
A or not(B)
A= $self->{"is_built"}B= not @isa_and_does
| |||||||||||||||||||||||||||||
| 96 | 1 | my $built_object = $build_object->($self, \$_[0]); | |||||||||||||||||||||||||||
| 97 | 1 | return $built_object->DOES($class2check); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 2 | CLASS: for my $class (@does) { | |||||||||||||||||||||||||||
| 100 | 3 | ▶ | $class->DOES($class2check) and return 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 0 | my %isa_and_does = map { ($_ => undef) } @isa_and_does; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | return exists $isa_and_does{$class2check}; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 3 | ▶ | sub can { ## no critic (ArgUnpacking) | ||||||||||||||||||||||||||
|
Subroutine
can: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 3 | my ($self, $method) = @_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 3 | my $built_object = $build_object->($self, \$_[0]); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 3 | return $built_object->can($method); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 3 | ▶ | sub VERSION { ## no critic (ArgUnpacking) | ||||||||||||||||||||||||||
|
Subroutine
VERSION: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 3 | my ($self, @version) = @_; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 3 | ▶ | if ( ! $self->{is_built} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 3 | ▶ | if ( defined $self->{VERSION} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 1 | $Object::Lazy::Version::VERSION = $self->{VERSION}; | |||||||||||||||||||||||||||
| 121 | 1 | return +( bless {}, 'Object::Lazy::Version' )->VERSION(@version); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 2 | ▶ | if ( $self->{version_from} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 1 | return +( bless {}, $self->{version_from} )->VERSION(@version); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 1 | my $built_object = $build_object->($self, \$_[0]); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 1 | return $built_object->VERSION(@version); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # $Id$ | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 15 | 1; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | __END__ | ||||||||||||||||||||||||||||