File Coverage

blib/lib/Object/Lazy.pm
Criterion Covered Total %
statement 59 62 95.1
branch 19 24 79.1
condition 5 6 83.3
subroutine 11 12 91.6
pod 5 5 100.0
total 99 109 90.8


line stmt bran cond sub pod time code
1             package Object::Lazy; ## no critic (TidyCode)
2            
3 15     15   1008662 use strict;
  15         147  
  15         462  
4 15     15   85 use warnings;
  15         31  
  15         717  
5            
6             our $VERSION = '0.16';
7            
8 15     15   88 use Carp qw(confess);
  15         27  
  15         733  
9 15     15   7965 use Try::Tiny;
  15         31550  
  15         777  
10 15     15   6404 use Object::Lazy::Validate;
  15         40  
  15         13289  
11            
12             sub new { ## no critic (ArgUnpacking)
13 18     18 1 3851 my ($class, $params) = Object::Lazy::Validate::validate_new(@_);
14            
15 18         79 $params = Object::Lazy::Validate::init($params);
16 17         78 my $self = bless $params, $class;
17 17 100       204 if ( exists $params->{ref} ) {
18 1         3 Object::Lazy::Ref::register($self);
19             }
20            
21 17         124 return $self;
22             }
23            
24             my $build_object = sub {
25             my ($self, $self_ref) = @_;
26            
27             local *__ANON__ = 'BUILD_OBJECT'; ## no critic (LocalVars)
28             my $built_object = $self->{build}->();
29             # don't build a second time
30             $self->{build} = sub { return $built_object };
31             if ( ! $self->{is_built} ) {
32             $self->{is_built} = 1;
33             if ( exists $self->{logger} ) {
34             try {
35             confess('object built');
36             }
37             catch {
38             $self->{logger}->($_);
39             };
40             }
41             }
42             ${$self_ref} = $built_object;
43            
44             return $built_object;
45             };
46            
47       0     sub DESTROY {} # is not AUTOLOAD
48            
49             sub AUTOLOAD { ## no critic (Autoloading ArgUnpacking)
50 6     6   2488 my $self = $_[0];
51            
52 6         21 my $method = substr our $AUTOLOAD, 2 + length __PACKAGE__;
53 6         22 my $built_object = $build_object->($self, \$_[0]);
54            
55 6         34 return $built_object->$method( @_[1 .. $#_] );
56             }
57            
58             sub isa { ## no critic (ArgUnpacking)
59 5     5 1 1882 my ($self, $class2check) = @_;
60            
61             my @isa
62             = ref $self->{isa} eq 'ARRAY'
63 5         18 ? @{ $self->{isa} }
64 5 50       21 : ( $self->{isa} );
65 5 100 100     33 if ( $self->{is_built} || ! @isa ) {
66 3         10 my $built_object = $build_object->($self, \$_[0]);
67 3         25 return $built_object->isa($class2check);
68             }
69 2         5 CLASS: for my $class (@isa) {
70 4 100       26 $class->isa($class2check) and return 1;
71             }
72 1         3 my %isa = map { ($_ => undef) } @isa;
  2         6  
73            
74 1         5 return exists $isa{$class2check};
75             }
76            
77             sub DOES { ## no critic (ArgUnpacking)
78 3     3 1 1068 my ($self, $class2check) = @_;
79            
80 3 50       19 UNIVERSAL->can('DOES')
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         10 ? @{ $self->{DOES} }
86 3 50       14 : ( $self->{DOES} );
87             my @isa_and_does = (
88             (
89             ref $self->{isa} eq 'ARRAY'
90 3         8 ? @{ $self->{isa} }
91             : ( $self->{isa} )
92 3 50       10 ),
93             @does,
94             );
95 3 100 66     16 if ( $self->{is_built} || ! @isa_and_does ) {
96 1         3 my $built_object = $build_object->($self, \$_[0]);
97 1         10 return $built_object->DOES($class2check);
98             }
99 2         5 CLASS: for my $class (@does) {
100 3 100       28 $class->DOES($class2check) and return 1;
101             }
102 0         0 my %isa_and_does = map { ($_ => undef) } @isa_and_does;
  0         0  
103            
104 0         0 return exists $isa_and_does{$class2check};
105             }
106            
107             sub can { ## no critic (ArgUnpacking)
108 3     3 1 125 my ($self, $method) = @_;
109            
110 3         12 my $built_object = $build_object->($self, \$_[0]);
111            
112 3         40 return $built_object->can($method);
113             }
114            
115             sub VERSION { ## no critic (ArgUnpacking)
116 3     3 1 19 my ($self, @version) = @_;
117            
118 3 50       11 if ( ! $self->{is_built} ) {
119 3 100       8 if ( defined $self->{VERSION} ) {
120 1         3 $Object::Lazy::Version::VERSION = $self->{VERSION};
121 1         18 return +( bless {}, 'Object::Lazy::Version' )->VERSION(@version);
122             }
123 2 100       8 if ( $self->{version_from} ) {
124 1         18 return +( bless {}, $self->{version_from} )->VERSION(@version);
125             }
126             }
127 1         4 my $built_object = $build_object->($self, \$_[0]);
128            
129 1         18 return $built_object->VERSION(@version);
130             }
131            
132             # $Id$
133            
134             1;
135            
136             __END__