File Coverage

blib/lib/MooseX/RoleFor.pm
Criterion Covered Total %
statement 12 14 85.7
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 17 19 89.4


line stmt bran cond sub pod time code
1             package MooseX::RoleFor;
2              
3 1     1   27110 use 5.010;
  1         4  
  1         49  
4 1     1   6 use strict;
  1         1  
  1         34  
5 1     1   880 use utf8;
  1         12  
  1         4  
6              
7             BEGIN
8             {
9 1     1   45 $MooseX::RoleFor::AUTHORITY = 'cpan:TOBYINK';
10 1         15 $MooseX::RoleFor::VERSION = '0.003';
11             }
12              
13 1     1   358 use Moose::Exporter;
  0            
  0            
14              
15             Moose::Exporter->setup_import_methods(
16             with_meta => [qw/role_for/],
17             role_metaroles => { role => ['MooseX::RoleFor::Meta::Role::Trait::RoleFor'] },
18             );
19              
20             sub role_for
21             {
22             my ($meta, $is_for, $consequence) = @_;
23             $meta->role_is_for($is_for);
24             $meta->role_misapplication_consequence($consequence)
25             if defined $consequence;
26             }
27              
28             'Yay!';
29              
30             __END__
31              
32             =head1 NAME
33              
34             MooseX::RoleFor - limit the applicability of a Moose::Role
35              
36             =head1 SYNOPSIS
37              
38             package Watchdog;
39            
40             use Moose::Role;
41             use MooseX::RoleFor;
42            
43             role_for 'Dog';
44             requires 'make_noise';
45            
46             sub hear_intruder
47             {
48             my ($self) = @_;
49             $self->make_noise;
50             }
51            
52             1;
53              
54             =head1 DESCRIPTION
55              
56             This package allows your Moose roles to limit what classes and objects
57             they may be composed with. This is often not a good idea - one of the
58             advantages of roles is that they can be reused in such different contexts.
59              
60             However, if you search CPAN for "TraitFor" you'll see that it's quite a
61             common desire to indicate that a role should only be applies to certain
62             classes.
63              
64             =head2 C<< role_for $class, $consequence >>
65              
66             C<< $class >> is a string (class name) or arrayref of strings indicating
67             which classes this role may be composed with. Inheritance is respected.
68              
69             C<< $consequence >> is either "carp" (the default) or "croak".
70              
71             =head2 How it works
72              
73             Adding C<< use MooseX::RoleFor >> to your role imports the C<role_for>
74             function to your class, and applies the
75             C<MooseX::RoleFor::Meta::Role::Trait::RoleFor> role to your role's metaclass.
76              
77             The C<role_for> function is basically:
78              
79             sub role_for
80             {
81             __PACKAGE__->meta->role_is_for($_[0])
82             __PACKAGE__->meta->role_misapplication_consequence($_[1])
83             if defined $_[1];
84             }
85              
86             C<MooseX::RoleFor::Meta::Role::Trait::RoleFor> hooks onto
87             C<Moose::Meta::Role>'s C<apply> method to enforce your restriction.
88              
89             =head1 BUGS
90              
91             Please report any bugs to
92             L<http://rt.cpan.org/Dist/Display.html?Queue=MooseX-RoleFor>.
93              
94             =head2 Known
95              
96             When misapplying a role to an instance (rather than a class), you get not
97             one warning, but two: one for the object, and one for its metaclass.
98              
99             =head1 SEE ALSO
100              
101             L<Moose>,
102             L<Moose::Meta::Role>.
103              
104             L<MooseX::RoleFor::Meta::Role::Trait::RoleFor> - internals.
105              
106             =head1 AUTHOR
107              
108             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
109              
110             =head1 COPYRIGHT AND LICENCE
111              
112             This software is copyright (c) 2011-2012 by Toby Inkster.
113              
114             This is free software; you can redistribute it and/or modify it under
115             the same terms as the Perl 5 programming language system itself.
116              
117             =head1 DISCLAIMER OF WARRANTIES
118              
119             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
120             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
121             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
122