Mention test failures
[gitmo/Moose-Policy.git] / lib / Moose / Policy.pm
CommitLineData
82cfc049 1package Moose::Policy;
4fe62f68 2use Moose 'confess', 'blessed';
82cfc049 3
dcbf286b 4our $VERSION = '0.05';
4fe62f68 5our $AUTHORITY = 'cpan:STEVAN';
b9238462 6
2756232e 7sub import {
8 shift;
9
10 my $policy = shift || return;
11
95c5abfd 12 unless (Class::MOP::is_class_loaded($policy)) {
83d85197 13 # otherwise require it ...
4fe62f68 14 eval { Class::MOP::load_class($policy) };
15 confess "Could not load policy module '$policy' because : $@"
cdaa4ee1 16 if $@;
2756232e 17 }
18
19 my $package = caller();
20 $package->can('meta') and
09eff61e 21 croak("'$package' already has a meta() method, this is very problematic");
2756232e 22
23 my $metaclass = 'Moose::Meta::Class';
24 $metaclass = $policy->metaclass($package)
25 if $policy->can('metaclass');
26
27 my %options;
28
29 # build options out of policy's constants
4fe62f68 30 $policy->can($_) and $options{"$_"} = $policy->$_($package)
2756232e 31 for (qw(
32 attribute_metaclass
33 instance_metaclass
34 method_metaclass
35 ));
36
37 # create a meta object so we can install &meta
38 my $meta = $metaclass->initialize($package => %options);
39 $meta->add_method('meta' => sub {
40 # we must re-initialize so that it works as expected in
41 # subclasses, since metaclass instances are singletons, this is
42 # not really a big deal anyway.
43 $metaclass->initialize((blessed($_[0]) || $_[0]) => %options)
44 });
45}
46
471;
48
49__END__
50
51=pod
52
8e7059d6 53=head1 NAME
54
4fe62f68 55Moose::Policy - Moose-mounted police
8e7059d6 56
57=head1 SYNOPSIS
58
8e7059d6 59 package Foo;
60
09eff61e 61 use Moose::Policy 'Moose::Policy::FollowPBP';
8e7059d6 62 use Moose;
63
64 has 'bar' => (is => 'rw', default => 'Foo::bar');
65 has 'baz' => (is => 'ro', default => 'Foo::baz');
66
09eff61e 67 # Foo now has (get, set)_bar methods as well as get_baz
68
3d1dec0a 69=head1 DEPRECATION NOTICE
70
71B<Moose::Policy is deprecated>.
72
73L<MooseX::FollowPBP> replaces the L<Moose::Policy::FollowPBP> module. The
74other policies included in this distribution do not yet have standalone MooseX
75modules, as of November, 2010.
76
61b270e4 77This module has not passed its tests since Moose 1.05, and will probably not
78be fixed.
79
2756232e 80=head1 DESCRIPTION
8e7059d6 81
09eff61e 82This module allows you to specify your project-wide or even company-wide
83Moose meta-policy.
8e7059d6 84
09eff61e 85Most all of Moose's features can be customized through the use of custom
86metaclasses, however fiddling with the metaclasses can be hairy. Moose::Policy
87removes most of that hairiness and makes it possible to cleanly contain
88a set of meta-level customizations in one easy to use module.
2756232e 89
4fe62f68 90This is still an release of this module and it should not be considered to
1c037baf 91be complete by any means. It is very basic implemenation at this point and
09eff61e 92will likely get more feature-full over time, as people request features.
93So if you have a suggestion/need/idea, please speak up.
94
95=head2 What is a meta-policy?
96
97A meta-policy is a set of custom Moose metaclasses which can be used to
98implement a number of customizations and restrictions on a particular
99Moose class.
100
101For instance, L<Moose::Policy::SingleInheritence> enforces that all
bae6d4c3 102specified Moose classes can only use single inheritance. It does this
09eff61e 103by trapping the call to C<superclasses> on the metaclass and only allowing
104you to assign a single superclass.
105
106The L<Moose::Policy::FollowPBP> policy changes the default behavior of
107accessors to fit the recomendations found in Perl Best Practices.
108
109=head1 CAVEATS
8e7059d6 110
09eff61e 111=head2 Always load Moose::Policy first.
112
113You B<must> put the following line of code:
8e7059d6 114
115 use Moose::Policy 'My::Policy';
116
09eff61e 117before this line:
8e7059d6 118
119 use Moose;
120
09eff61e 121This is because Moose::Policy must be given the opportunity to set the
122custom metaclass before Moose has set it's default metaclass. In fact, if
123you try to set a Moose::Policy and there is a C<meta> method available,
124not only will kittens die, but your program will too.
125
bae6d4c3 126=head2 Policies are class scoped
09eff61e 127
bae6d4c3 128You must repeat the policy for each class you want to use it. It is B<not>
09eff61e 129inherited. This may change in the future, probably it will be a Moose::Policy
bae6d4c3 130itself to allow Moose policies to be inherited.
8e7059d6 131
09eff61e 132=head1 THE POLICY
8e7059d6 133
50e80884 134A Policy is set by passing C<Moose::Policy::import()> a package name. This
09eff61e 135package is then queried for what metaclasses it should use. The possible
136metaclass values are:
8e7059d6 137
138=over
139
09eff61e 140=item B<metaclass>
8e7059d6 141
09eff61e 142This defaults to C<Moose::Meta::Class>.
8e7059d6 143
09eff61e 144=item B<attribute_metaclass>
8e7059d6 145
09eff61e 146=item B<instance_metaclass>
8e7059d6 147
09eff61e 148=item B<method_metaclass>
8e7059d6 149
150=back
151
09eff61e 152For examples of what a Policy actually looks like see the examples in
153C<Moose::Policy::> and the test suite. More docs to come on this later (probably
154a cookbook or something).
8e7059d6 155
4fe62f68 156=head1 METHODS
157
158=over 4
159
160=item B<meta>
161
162=back
163
09eff61e 164=head1 FUTURE PLANS
8e7059d6 165
09eff61e 166As I said above, this is the first release and it is by no means feature complete.
167There are a number of thoughts on the future direction of this module. Here are
168some random thoughts on that, in no particular order.
8e7059d6 169
09eff61e 170=over 4
8e7059d6 171
09eff61e 172=item Make set of policy roles
8e7059d6 173
09eff61e 174Roles are an excellent way to combine sets of behaviors together into one, and
175custom metaclasses are actually better composed by roles then by inheritence.
176The ideal situation is that this module will provide a set of roles which can be
bae6d4c3 177used to compose your meta-policy with relative ease.
8e7059d6 178
09eff61e 179=back
8e7059d6 180
2756232e 181=head1 BUGS
8e7059d6 182
2756232e 183All complex software has bugs lurking in it, and this module is no
184exception. If you find a bug please either email me, or add the bug
185to cpan-RT.
8e7059d6 186
2756232e 187=head1 AUTHOR
8e7059d6 188
2756232e 189Stevan Little E<lt>stevan@iinteractive.comE<gt>
8e7059d6 190
83d85197 191Eric Wilhelm
8e7059d6 192
2756232e 193=head1 COPYRIGHT AND LICENSE
8e7059d6 194
4fe62f68 195Copyright 2006-2007 by Infinity Interactive, Inc.
b9238462 196
2756232e 197L<http://www.iinteractive.com>
82cfc049 198
2756232e 199This library is free software; you can redistribute it and/or modify
200it under the same terms as Perl itself.
82cfc049 201
2756232e 202=cut
bfacd619 203