File Coverage

blib/lib/MooX/StrictHas.pm
Criterion Covered Total %
statement 28 28 100.0
branch 7 10 70.0
condition n/a
subroutine 14 14 100.0
pod n/a
total 49 52 94.2


line stmt bran cond sub pod time code
1             package MooX::StrictHas;
2              
3             our $VERSION = '0.04';
4              
5             # this bit would be MooX::Utils but without initial _ on func name
6 1     1   13560 use strict;
  1         2  
  1         28  
7 1     1   5 use warnings;
  1         3  
  1         21  
8 1     1   5 use Moo ();
  1         2  
  1         12  
9 1     1   451 use Moo::Role ();
  1         8571  
  1         29  
10 1     1   7 use Carp qw(croak);
  1         2  
  1         326  
11             #use base qw(Exporter);
12             #our @EXPORT = qw(override_function);
13             sub _override_function {
14 5     5   15 my ($target, $name, $func) = @_;
15 5 50       60 my $orig = $target->can($name) or croak "Override '$target\::$name': not found";
16 5 50       20 my $install_tracked = Moo::Role->is_role($target) ? \&Moo::Role::_install_tracked : \&Moo::_install_tracked;
17 5     5   167 $install_tracked->($target, $name, sub { $func->($orig, @_) });
  5     5   372  
        5      
        5      
        5      
        5      
18             }
19             # end MooX::Utils;
20              
21             my %ATTR2MESSAGE = (
22             auto_deref => q{just dereference in your using code},
23             lazy_build => q{Use "is => 'lazy'" instead},
24             does => q{Unsupported; use "isa" instead},
25             );
26             sub import {
27 5     5   26744 my $target = scalar caller;
28             _override_function($target, 'has', sub {
29 5     5   21 my ($orig, $namespec, %opts) = @_;
30 5 50       16 $namespec = "[@$namespec]" if ref $namespec;
31 5         9 my @messages;
32             push @messages, exists($opts{$_})
33             ? "$_ detected on $namespec: $ATTR2MESSAGE{$_}"
34             : ()
35 5 100       54 for sort keys %ATTR2MESSAGE;
36 5 100       596 croak join "\n", @messages if @messages;
37 1         4 $orig->($namespec, %opts);
38 5         42 });
39             }
40              
41             =head1 NAME
42              
43             MooX::StrictHas - Forbid "has" attributes lazy_build and auto_deref
44              
45             =begin markdown
46              
47             # PROJECT STATUS
48              
49             | OS | Build status |
50             |:-------:|--------------:|
51             | Linux | [![Build Status](https://travis-ci.com/mohawk2/moox-stricthas.svg?branch=master)](https://travis-ci.org/mohawk2/moox-stricthas) |
52              
53             [![CPAN version](https://badge.fury.io/pl/moox-stricthas.svg)](https://metacpan.org/pod/MooX::StrictHas) [![Coverage Status](https://coveralls.io/repos/github/mohawk2/moox-stricthas/badge.svg?branch=master)](https://coveralls.io/github/mohawk2/moox-stricthas?branch=master)
54              
55             =end markdown
56              
57             =head1 SYNOPSIS
58              
59             package MyMod;
60             use Moo;
61             use MooX::StrictHas;
62             has attr => (
63             is => 'ro',
64             auto_deref => 1, # blows up, not implemented in Moo
65             );
66             has attr2 => (
67             is => 'ro',
68             lazy_build => 1, # blows up, not implemented in Moo
69             );
70             has attr2 => (
71             is => 'ro',
72             does => "Thing", # blows up, not implemented in Moo
73             );
74              
75             =head1 DESCRIPTION
76              
77             This is a L extension, intended to aid those porting modules from
78             L to Moo. It forbids two attributes for L, which Moo
79             does not implement, but silently accepts:
80              
81             =over
82              
83             =item auto_deref
84              
85             This is not considered best practice - just dereference in your using code.
86              
87             =item does
88              
89             Unsupported; use C instead.
90              
91             =item lazy_build
92              
93             Use C 'lazy'> instead.
94              
95             =back
96              
97             =head1 AUTHOR
98              
99             Ed J
100              
101             =head1 LICENCE
102              
103             The same terms as Perl itself.
104              
105             =cut
106              
107             1;