File Coverage

blib/lib/MooX/LazyRequire.pm
Criterion Covered Total %
statement 28 30 93.3
branch 6 8 75.0
condition 2 6 33.3
subroutine 7 7 100.0
pod n/a
total 43 51 84.3


line stmt bran cond sub pod time code
1             package MooX::LazyRequire;
2 1     1   62825 use strict;
  1         2  
  1         34  
3 1     1   5 use warnings;
  1         2  
  1         47  
4              
5             our $VERSION = '0.001002';
6             $VERSION = eval $VERSION;
7              
8 1     1   4 use Carp;
  1         6  
  1         91  
9 1     1   1104 use Sub::Quote;
  1         3636  
  1         77  
10 1     1   1251 use Class::Method::Modifiers qw(install_modifier);
  1         2003  
  1         265  
11              
12             sub import {
13 2     2   12969 my ($class) = @_;
14 2         6 my $target = caller;
15              
16             install_modifier $target, 'around', 'has', sub {
17 6     6   13015 my $orig = shift;
18 6         22 my ($attrs, %opts) = @_;
19 6 100       21 my @attrs = ref $attrs ? @$attrs : $attrs;
20 6 100       14 if ($opts{lazy_required}) {
21 4 50 33     34 if (exists $opts{lazy} && !$opts{lazy}) {
    50 33        
22 0         0 croak "LazyRequire can't be used with lazy => 0";
23             }
24             elsif (exists $opts{default} || exists $opts{builder}) {
25 0         0 croak "You may not use both a builder or a default and lazy_required for one attribute ("
26             . (join ', ', @attrs) . ")";
27             }
28 4         6 $opts{lazy} = 1;
29 4         9 for my $attr (@attrs) {
30 6         583 my $opts = {
31             default => quote_sub(qq{
32             Carp::croak("Attribute '\Q$attr\E' must be provided before calling reader");
33             }),
34             %opts,
35             };
36 6         335 $orig->($attr, %$opts);
37             }
38             }
39             else {
40 2         13 $orig->($attrs, %opts);
41             }
42             }
43 2         19 }
44              
45             1;
46              
47             __END__