File Coverage

blib/lib/MooX/StrictConstructor.pm
Criterion Covered Total %
statement 27 28 96.4
branch 3 4 75.0
condition 1 3 33.3
subroutine 9 9 100.0
pod n/a
total 40 44 90.9


line stmt bran cond sub pod time code
1 1     1   35833 use strict; # redundant, but quiets perlcritic
  1         1  
  1         38  
2             package MooX::StrictConstructor;
3             $MooX::StrictConstructor::VERSION = '0.008';
4             # ABSTRACT: Make your Moo-based object constructors blow up on unknown attributes.
5              
6              
7 1     1   3 use Moo 1.001000 (); # $Moo::MAKERS support
  1         16  
  1         11  
8 1     1   330 use Moo::Role ();
  1         16098  
  1         20  
9              
10 1     1   489 use Class::Method::Modifiers qw(install_modifier);
  1         953  
  1         52  
11              
12 1     1   4 use strictures 1;
  1         5  
  1         22  
13              
14             use constant
15 1     1   45 CON_ROLE => 'Method::Generate::Constructor::Role::StrictConstructor';
  1         1  
  1         147  
16              
17             #
18             # The gist of this code was copied directly from Graham Knop (HAARG)'s
19             # MooX::InsideOut, specifically its import sub. It has diverged a bit to
20             # handle goal specific differences.
21             #
22             sub import {
23 6     6   2371 my $class = shift;
24 6         10 my $target = caller;
25 6 50 33     28 unless ( $Moo::MAKERS{$target} && $Moo::MAKERS{$target}{is_class} ) {
26 0         0 die "MooX::StrictConstructor can only be used on Moo classes.";
27             }
28              
29 6         8 _apply_role($target);
30              
31             install_modifier($target, 'after', 'extends', sub {
32 2     2   2757 _apply_role($target);
33 6         1062 });
34             }
35              
36             sub _apply_role {
37 8     8   8 my $target = shift;
38 8         19 my $con = Moo->_constructor_maker_for($target);
39 8 100       31818 Moo::Role->apply_roles_to_object($con, CON_ROLE)
40             unless Role::Tiny::does_role($con, CON_ROLE);
41             }
42              
43              
44             1;
45              
46             __END__