| 1 | package MooX::StrictHas; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $VERSION = '0.04'; | |||||||||
| 4 | |||||||||||
| 5 | # this bit would be MooX::Utils but without initial _ on func name | ||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Moo (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Moo::Role (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | #use base qw(Exporter); | ||||||||||
| 12 | #our @EXPORT = qw(override_function); | ||||||||||
| 13 | 5 | ▶ | sub _override_function { | ||||||||
|
Subroutine
_override_function: called CC=3 SCAR=11.3
| |||||||||||
| 14 | 5 | my ($target, $name, $func) = @_; | |||||||||
| 15 | 5 | ▶ | my $orig = $target->can($name) or croak "Override '$target\::$name': not found"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | 5 | ▶ | my $install_tracked = Moo::Role->is_role($target) ? \&Moo::Role::_install_tracked : \&Moo::_install_tracked; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | 5 | ▶ | $install_tracked->($target, $name, sub { $func->($orig, @_) }); | ||||||||
|
Subroutine
has: called CC=1 SCAR=0.0
| |||||||||||
| 18 | } | ||||||||||
| 19 | # end MooX::Utils; | ||||||||||
| 20 | |||||||||||
| 21 | 1 | 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 | 5 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 5 | my $target = scalar caller; | |||||||||
| 28 | 5 | ▶ | _override_function($target, 'has', sub { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||
| 29 | 5 | my ($orig, $namespec, %opts) = @_; | |||||||||
| 30 | 5 | ▶ | $namespec = "[@$namespec]" if ref $namespec; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 5 | my @messages; | |||||||||
| 32 | push @messages, exists($opts{$_}) | ||||||||||
| 33 | ? "$_ detected on $namespec: $ATTR2MESSAGE{$_}" | ||||||||||
| 34 | : () | ||||||||||
| 35 | 5 | ▶ | for sort keys %ATTR2MESSAGE; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 5 | ▶ | croak join "\n", @messages if @messages; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 1 | $orig->($namespec, %opts); | |||||||||
| 38 | 5 | }); | |||||||||
| 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 | [](https://travis-ci.org/mohawk2/moox-stricthas) | | ||||||||||
| 52 | |||||||||||
| 53 | [](https://metacpan.org/pod/MooX::StrictHas) [](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<Moo> extension, intended to aid those porting modules from | ||||||||||
| 78 | L<Moose> to Moo. It forbids two attributes for L<Moo/has>, 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<isa> instead. | ||||||||||
| 90 | |||||||||||
| 91 | =item lazy_build | ||||||||||
| 92 | |||||||||||
| 93 | Use C<is =E<gt> '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 | 1; | |||||||||