File Coverage

blib/lib/MooseX/Extended/Role/Custom.pm
Criterion Covered Total %
statement 31 31 100.0
branch 1 2 50.0
condition n/a
subroutine 9 9 100.0
pod 0 1 0.0
total 41 43 95.3


line stmt bran cond sub pod time code
1              
2             # ABSTRACT: Build a custom Moose::Role, just for you.
3              
4             use 5.20.0;
5 1     1   5270 use strict;
  1         3  
6 1     1   5 use warnings;
  1         5  
  1         17  
7 1     1   4 use true;
  1         1  
  1         23  
8 1     1   4 use MooseX::Extended::Core qw(
  1         1  
  1         4  
9 1         81 _enabled_features
10             _disabled_warnings
11             );
12 1     1   785 use MooseX::Extended::Role ();
  1         1  
13 1     1   405 use namespace::autoclean;
  1         3  
  1         19  
14 1     1   6  
  1         2  
  1         5  
15             our $VERSION = '0.33';
16              
17             my @caller = caller(0);
18             my $custom_moose = $caller[0]; # this is our custom Moose definition
19 1     1   7 true->import::into($custom_moose) unless $caller[1] =~ /^\(eval/;
20 1         25 strict->import::into($custom_moose);
21 1 50       7 warnings->import::into($custom_moose);
22 1         1005 namespace::autoclean->import::into($custom_moose);
23 1         167 feature->import( _enabled_features() );
24 1         144 warnings->unimport(_disabled_warnings);
25 1         172 }
26 1         4  
27             my ( $class, %args ) = @_;
28             my $target_class = caller(1); # this is the class consuming our custom Moose
29             MooseX::Extended::Role->import(
30 1     1 0 194 %args,
31 1         3 call_level => 1,
32 1         21 for_class => $target_class,
33             );
34             }
35              
36             1;
37              
38              
39             =pod
40              
41             =encoding UTF-8
42              
43             =head1 NAME
44              
45             MooseX::Extended::Role::Custom - Build a custom Moose::Role, just for you.
46              
47             =head1 VERSION
48              
49             version 0.33
50              
51             =head1 SYNOPSIS
52              
53             Define your own version of L<MooseX::Extended>:
54              
55             package My::Moose::Role {
56             use MooseX::Extended::Role::Custom;
57              
58             sub import {
59             my ( $class, %args ) = @_;
60             MooseX::Extended::Role::Custom->create(
61             excludes => [qw/ carp /],
62             includes => ['multi'],
63             %args # you need this to allow customization of your customization
64             );
65             }
66             }
67              
68             # no need for a true value
69              
70             And then use it:
71              
72             package Some::Class::Role {
73             use My::Moose::Role types => [qw/ArrayRef Num/];
74              
75             param numbers => ( isa => ArrayRef[Num] );
76              
77             multi sub foo ($self) { ... }
78             multi sub foo ($self, $bar) { ... }
79             }
80              
81             =head1 DESCRIPTION
82              
83             I hate boilerplate, so let's get rid of it. Let's say you don't want warnings
84             on classes implicitly overriding role methods, L<namespace::autoclean> or
85             C<carp>, but you do want C<multi>. Plus, you have custom versions of C<carp>
86             and C<croak>:
87              
88             package Some::Class {
89             use MooseX::Extended
90             excludes => [qw/ WarnOnConflict autoclean carp /],
91             includes => ['multi'];
92             use My::Carp q(carp croak);
93              
94             ... my code here
95             }
96              
97             You probably get tired of typing that every time. Now you don't have to.
98              
99             package My::Moose {
100             use MooseX::Extended::Custom;
101             use My::Carp ();
102             use Import::Into;
103              
104             sub import {
105             my ( $class, %args ) = @_;
106             my $target_class = caller;
107             MooseX::Extended::Custom->create(
108             excludes => [qw/ autoclean carp /],
109             includes => ['multi'],
110             %args # you need this to allow customization of your customization
111             );
112             My::Carp->import::into($target_class, qw(carp croak));
113             }
114             }
115              
116             And then when you use C<My::Moose>, that's all set up for you.
117              
118             If you need to change this on a "per class" basis:
119              
120             use My::Moose
121             excludes => ['carp'],
122             types => [qw/ArrayRef Num/];
123              
124             The above changes your C<excludes> and adds C<types>, but doesn't change your C<includes>.
125              
126             =head1 AUTHOR
127              
128             Curtis "Ovid" Poe <curtis.poe@gmail.com>
129              
130             =head1 COPYRIGHT AND LICENSE
131              
132             This software is Copyright (c) 2022 by Curtis "Ovid" Poe.
133              
134             This is free software, licensed under:
135              
136             The Artistic License 2.0 (GPL Compatible)
137              
138             =cut