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