File Coverage

blib/lib/Object/Lazy.pm
Criterion Covered Total %
statement 59 63 93.6
branch 19 24 79.1
condition 5 6 83.3
subroutine 11 12 91.6
pod 5 5 100.0
total 99 110 90.0


line stmt bran cond sub pod time code
1             package Object::Lazy; ## no critic (TidyCode)
2            
3 14     14   449642 use strict;
  14         36  
  14         603  
4 14     14   78 use warnings;
  14         29  
  14         650  
5            
6             our $VERSION = '0.14';
7            
8 14     14   77 use Carp qw(confess);
  14         43  
  14         766  
9 14     14   13179 use Try::Tiny;
  14         23892  
  14         820  
10 14     14   8492 use Object::Lazy::Validate;
  14         51  
  14         14531  
11            
12             sub new { ## no critic (ArgUnpacking)
13 17     17 1 4159 my ($class, $params) = Object::Lazy::Validate::validate_new(@_);
14            
15 17         181 $params = Object::Lazy::Validate::init($params);
16 16         215 my $self = bless $params, $class;
17 16 100       126 if ( exists $params->{ref} ) {
18 1         5 Object::Lazy::Ref::register($self);
19             }
20            
21 16         64 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     0   0 sub DESTROY {} # is not AUTOLOAD
48            
49             sub AUTOLOAD { ## no critic (Autoloading ArgUnpacking)
50 5     5   4339 my ($self, @params) = @_;
51            
52 5         18 my $method = substr our $AUTOLOAD, 2 + length __PACKAGE__;
53 5         27 my $built_object = $build_object->($self, \$_[0]);
54            
55 5         46 return $built_object->$method(@params);
56             }
57            
58             sub isa { ## no critic (ArgUnpacking)
59 5     5 1 2157 my ($self, $class2check) = @_;
60            
61             my @isa
62 5         18 = ref $self->{isa} eq 'ARRAY'
63 5 50       25 ? @{ $self->{isa} }
64             : ( $self->{isa} );
65 5 100 100     45 if ( $self->{is_built} || ! @isa ) {
66 3         13 my $built_object = $build_object->($self, \$_[0]);
67 3         33 return $built_object->isa($class2check);
68             }
69 2         4 CLASS: for my $class (@isa) {
70 4 100       65 $class->isa($class2check) and return 1;
71             }
72 1         4 my %isa = map { ($_ => undef) } @isa;
  2         8  
73            
74 1         37 return exists $isa{$class2check};
75             }
76            
77             sub DOES { ## no critic (ArgUnpacking)
78 3     3 1 1403 my ($self, $class2check) = @_;
79            
80 3 50       29 UNIVERSAL->can('DOES')
81             or confess 'UNIVERSAL 1.04 (Perl 5.10) required for method DOES';
82            
83             my @does
84 3         30 = ref $self->{DOES} eq 'ARRAY'
85 3 50       17 ? @{ $self->{DOES} }
86             : ( $self->{DOES} );
87 3         10 my @isa_and_does = (
88             (
89             ref $self->{isa} eq 'ARRAY'
90 3 50       15 ? @{ $self->{isa} }
91             : ( $self->{isa} )
92             ),
93             @does,
94             );
95 3 100 66     22 if ( $self->{is_built} || ! @isa_and_does ) {
96 1         4 my $built_object = $build_object->($self, \$_[0]);
97 1         14 return $built_object->DOES($class2check);
98             }
99 2         5 CLASS: for my $class (@does) {
100 3 100       43 $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 174 my ($self, $method) = @_;
109            
110 3         14 my $built_object = $build_object->($self, \$_[0]);
111            
112 3         62 return $built_object->can($method);
113             }
114            
115             sub VERSION { ## no critic (ArgUnpacking)
116 3     3 1 20 my ($self, @version) = @_;
117            
118 3 50       18 if ( ! $self->{is_built} ) {
119 3 100       13 if ( defined $self->{VERSION} ) {
120 1         3 $Object::Lazy::Version::VERSION = $self->{VERSION};
121 1         13 return +( bless {}, 'Object::Lazy::Version' )->VERSION(@version);
122             }
123 2 100       9 if ( $self->{version_from} ) {
124 1         15 return +( bless {}, $self->{version_from} )->VERSION(@version);
125             }
126             }
127 1         6 my $built_object = $build_object->($self, \$_[0]);
128            
129 1         26 return $built_object->VERSION(@version);
130             }
131            
132             # $Id$
133            
134             1;
135            
136             __END__