10 use Scalar::Util 'blessed';
15 my $policy = shift || return;
17 unless (Moose::_is_class_already_loaded($policy)) {
18 ($policy->require) or confess "Could not load policy module " .
19 "'$policy' because : $UNIVERSAL::require::ERROR";
22 my $package = caller();
23 $package->can('meta') and
24 croak("'$package' already has a meta() method");
26 my $metaclass = 'Moose::Meta::Class';
27 $metaclass = $policy->metaclass($package)
28 if $policy->can('metaclass');
32 # build options out of policy's constants
33 $policy->can($_) and $options{":$_"} = $policy->$_($package)
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)
58 Moose::Policy - moose-mounted police
64 use Moose::Policy 'My::MooseBestPractice';
67 has 'bar' => (is => 'rw', default => 'Foo::bar');
68 has 'baz' => (is => 'ro', default => 'Foo::baz');
72 This class allows you to specify your project-wide or company-wide Moose
73 meta policy in one location.
81 use Moose::Policy 'My::Policy';
91 The argument to C<import()> is a package name. This package is
92 require()'d and queried for the following constants:
98 Defaults to C<'Moose::Meta::Class'>.
100 =item attribute_metaclass
102 =item instance_metaclass
104 =item method_metaclass
108 These values are then used to setup your $package->meta object.
110 Your policy package could be simply a list of constants.
113 use constant attribute_metaclass => 'My::Moose::Meta::Attribute';
115 But the methods are told what package is using the policy, so they could
116 concievably give different answers.
118 package My::FancyPolicy;
120 sub attribute_metaclass {
122 my ($user_package) = @_;
123 return('Our::Attributes::Stricter')
124 if $user_package =~ m/^Private::Banking::Money/;
125 return('Our::Attributes');
130 L<Moose>, L<Moose::Meta::Class>
134 All complex software has bugs lurking in it, and this module is no
135 exception. If you find a bug please either email me, or add the bug
140 Stevan Little E<lt>stevan@iinteractive.comE<gt>
142 Eric Wilhelm E<lt>...E<gt>
144 =head1 COPYRIGHT AND LICENSE
146 Copyright 2006 by Infinity Interactive, Inc.
148 L<http://www.iinteractive.com>
150 This library is free software; you can redistribute it and/or modify
151 it under the same terms as Perl itself.