File Coverage

blib/lib/MooX/NewDefaults.pm
Criterion Covered Total %
statement 20 25 80.0
branch 4 6 66.6
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 30 37 81.0


line stmt bran cond sub pod time code
1 4     4   77158 use 5.006;
  4         11  
2 4     4   16 use strict;
  4         4  
  4         70  
3 4     4   20 use warnings;
  4         3  
  4         1801  
4              
5             package MooX::NewDefaults;
6              
7             our $VERSION = '0.001002';
8              
9             # ABSTRACT: Alter attribute defaults with less pain
10              
11             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12              
13 4         808 use Sub::Exporter::Progressive -setup => {
14             exports => [qw( default_for )],
15             groups => {
16             default => [qw( default_for )],
17             },
18 4     4   405 };
  4         761  
19              
20             sub default_for {
21 3     3 1 46014 my ( $name_proto, @args ) = @_;
22 3         8 my $target = caller;
23              
24 3 100       12 my (@name_proto) = 'ARRAY' eq ref $name_proto ? @{$name_proto} : $name_proto;
  1         3  
25              
26 3 50       13 if ( @args != 1 ) {
27 0         0 require Carp;
28             Carp::croak(
29             sprintf q[Invalid options for %s default: Single argument expected, got %s],
30 0         0 join( ', ', map { "'$_'" } @name_proto ),
  0         0  
31             scalar @args,
32             );
33             }
34 3         6 my $coderef;
35 3 50       30 if ( not $coderef = $target->can('has') ) {
36 0         0 require Carp;
37 0         0 Carp::croak( sprintf q[Calling class %s cannot "has". Did you forget to "use Moo"?], $target, );
38             }
39              
40             # Calling '->has()' directly of course doesn't work, because it doesn't expect
41             # $_[0] to be a class, but the attribute name.
42             #
43             # The class itself is baked into $target::has during `use Moo`
44 3         9 return $coderef->( [ map { "+$_" } @name_proto ], default => @args );
  4         21  
45             }
46              
47             1;
48              
49             __END__