| 1 | package MooseX::APIRole; | ||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | $MooseX::APIRole::VERSION = '0.01'; | |||||||||
| 4 | } | ||||||||||
| 5 | # ABSTRACT: automatically create API roles for your classes and roles | ||||||||||
| 6 | 1 | ▶ | use Moose (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Moose::Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Carp qw(confess); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use true; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | Moose::Exporter->setup_import_methods( | |||||||||
| 12 | with_meta => [qw/set_api_role_name apply_api_role make_api_role/], | ||||||||||
| 13 | class_metaroles => { class => ['MooseX::APIRole::Meta'] }, | ||||||||||
| 14 | role_metaroles => { role => ['MooseX::APIRole::Meta'] }, | ||||||||||
| 15 | ); | ||||||||||
| 16 | |||||||||||
| 17 | 4 | ▶ | sub set_api_role_name { | ||||||||
|
Subroutine
set_api_role_name: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 18 | 4 | my ($meta, $name) = @_; | |||||||||
| 19 | 4 | ▶ | confess 'if you want a named api role, you should supply the name (retardo)' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | if !$name; | ||||||||||
| 21 | |||||||||||
| 22 | 4 | $meta->set_api_role_name($name); | |||||||||
| 23 | 4 | return $meta; | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 2 | ▶ | sub apply_api_role { | ||||||||
|
Subroutine
apply_api_role: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 27 | 2 | my ($meta) = @_; | |||||||||
| 28 | 2 | $meta->get_api_role->apply($meta); # meta. | |||||||||
| 29 | 2 | return $meta; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 2 | ▶ | sub make_api_role { | ||||||||
|
Subroutine
make_api_role: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 33 | 2 | my ($meta, $name) = @_; | |||||||||
| 34 | 2 | set_api_role_name($meta, $name); | |||||||||
| 35 | 2 | apply_api_role($meta); | |||||||||
| 36 | 2 | return $meta; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | =pod | ||||||||||
| 42 | |||||||||||
| 43 | =head1 NAME | ||||||||||
| 44 | |||||||||||
| 45 | MooseX::APIRole - automatically create API roles for your classes and roles | ||||||||||
| 46 | |||||||||||
| 47 | =head1 VERSION | ||||||||||
| 48 | |||||||||||
| 49 | version 0.01 | ||||||||||
| 50 | |||||||||||
| 51 | =head1 SYNOPSIS | ||||||||||
| 52 | |||||||||||
| 53 | If you write a Moose class like this: | ||||||||||
| 54 | |||||||||||
| 55 | package Class; | ||||||||||
| 56 | use Moose; | ||||||||||
| 57 | use MooseX::APIRole; | ||||||||||
| 58 | use true; | ||||||||||
| 59 | use namespace::autoclean; | ||||||||||
| 60 | |||||||||||
| 61 | sub foo {} | ||||||||||
| 62 | |||||||||||
| 63 | make_api_role 'Class::API'; | ||||||||||
| 64 | |||||||||||
| 65 | __PACKAGE__->meta->make_immutable; | ||||||||||
| 66 | |||||||||||
| 67 | C<MooseX::APIRole> will automatically create an API role like this: | ||||||||||
| 68 | |||||||||||
| 69 | package Class::API; | ||||||||||
| 70 | use Moose::Role; | ||||||||||
| 71 | |||||||||||
| 72 | requires 'foo'; | ||||||||||
| 73 | |||||||||||
| 74 | And apply it to your class. | ||||||||||
| 75 | |||||||||||
| 76 | If you forget what you called the API role, or don't want the API role to have a name, | ||||||||||
| 77 | you can get at it via the metaclass: | ||||||||||
| 78 | |||||||||||
| 79 | my $role = Class->meta->get_api_role; | ||||||||||
| 80 | |||||||||||
| 81 | You can then treat C<$role> like you would any other role. | ||||||||||
| 82 | |||||||||||
| 83 | You can also create API roles for roles: | ||||||||||
| 84 | |||||||||||
| 85 | package Role; | ||||||||||
| 86 | use Moose::Role; | ||||||||||
| 87 | use MooseX::APIRole; | ||||||||||
| 88 | use true; | ||||||||||
| 89 | use namespace::autoclean; | ||||||||||
| 90 | |||||||||||
| 91 | sub foo {} | ||||||||||
| 92 | requires 'bar'; | ||||||||||
| 93 | |||||||||||
| 94 | make_api_role 'Role::API'; | ||||||||||
| 95 | |||||||||||
| 96 | This results in the following role: | ||||||||||
| 97 | |||||||||||
| 98 | package Role::API; | ||||||||||
| 99 | use Moose::Role; | ||||||||||
| 100 | |||||||||||
| 101 | requires 'foo'; | ||||||||||
| 102 | requires 'bar'; | ||||||||||
| 103 | |||||||||||
| 104 | If you do not call C<make_api_role> or C<apply_api_role>, you can | ||||||||||
| 105 | still get the lazily-built anonymous API role via the metaclass. But | ||||||||||
| 106 | the class won't C<does_role> the role, which could be confusing. | ||||||||||
| 107 | |||||||||||
| 108 | =head1 DESCRIPTION | ||||||||||
| 109 | |||||||||||
| 110 | Inheritance is handled such that if C<Subclass> extends C<Class> with | ||||||||||
| 111 | API role C<APIRole>, C<Subclass> will also do the C<APIRole>. The | ||||||||||
| 112 | same applies to roles; if C<Role> does C<RoleAPI> and C<AnotherRole> | ||||||||||
| 113 | consumes C<Role>, then C<AnotherRole> will also do C<RoleAPI>. | ||||||||||
| 114 | |||||||||||
| 115 | Wunderbar. | ||||||||||
| 116 | |||||||||||
| 117 | =head1 FUNCTIONS | ||||||||||
| 118 | |||||||||||
| 119 | You can control the behavior of this module by calling these imported | ||||||||||
| 120 | functions: | ||||||||||
| 121 | |||||||||||
| 122 | =head2 set_api_role_name(ClassName) | ||||||||||
| 123 | |||||||||||
| 124 | This is the namespace that you want the API role to be in. The | ||||||||||
| 125 | results of using an existing class name are undefined. It's likely | ||||||||||
| 126 | that demons will come out of your nose and all your plants will die. | ||||||||||
| 127 | So come up with a unique name. | ||||||||||
| 128 | |||||||||||
| 129 | =head2 apply_api_role | ||||||||||
| 130 | |||||||||||
| 131 | If you want the API role to be applied to your class or role, call this function. | ||||||||||
| 132 | |||||||||||
| 133 | You almost always want to do this, but it can't be done automatically | ||||||||||
| 134 | for the same reason that C<< Class->meta->make_immutable >> can't be | ||||||||||
| 135 | done automatically. | ||||||||||
| 136 | |||||||||||
| 137 | =head2 make_api_role(ClassName) | ||||||||||
| 138 | |||||||||||
| 139 | Works like C<set_api_role_name> followed by C<apply_api_role>. | ||||||||||
| 140 | |||||||||||
| 141 | =head1 SEE ALSO | ||||||||||
| 142 | |||||||||||
| 143 | See L<MooseX::APIRole::Meta> for the metaclass attributes you get. | ||||||||||
| 144 | |||||||||||
| 145 | =head1 AUTHOR | ||||||||||
| 146 | |||||||||||
| 147 | Jonathan Rockway <jrockway@cpan.org> | ||||||||||
| 148 | |||||||||||
| 149 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 150 | |||||||||||
| 151 | This software is copyright (c) 2010 by Jonathan Rockway. | ||||||||||
| 152 | |||||||||||
| 153 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 154 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 155 | |||||||||||
| 156 | =cut | ||||||||||
| 157 | |||||||||||
| 158 | |||||||||||
| 159 | __END__ | ||||||||||