12 use Scalar::Util 'blessed';
16 Moose::Policy - moose-mounted police
20 This class allows you to specify your project-wide or company-wide Moose
21 meta policy in one location.
25 use Moose::Policy 'My::MooseBestPractice';
28 has 'bar' => (is => 'rw', default => 'Foo::bar');
29 has 'baz' => (is => 'ro', default => 'Foo::baz');
33 use Moose::Policy 'My::Policy';
42 use Moose::Policy 'My::Policy';
52 The argument to C<import()> is a package name. This package is
53 require()'d and queried for the following constants:
59 Defaults to C<'Moose::Meta::Class'>.
61 =item attribute_metaclass
63 =item instance_metaclass
65 =item method_metaclass
69 These values are then used to setup your $package->meta object.
71 Your policy package could be simply a list of constants.
74 use constant attribute_metaclass => 'My::Moose::Meta::Attribute';
76 But the methods are told what package is using the policy, so they could
77 concievably give different answers.
79 package My::FancyPolicy;
81 sub attribute_metaclass {
83 my ($user_package) = @_;
84 return('Our::Attributes::Stricter')
85 if $user_package =~ m/^Private::Banking::Money/;
86 return('Our::Attributes');
91 Stevan Little E<lt>stevan@iinteractive.comE<gt>
93 In response to a feature request by Eric Wilhelm and suggestions by Matt
96 Documentation and some code are Eric's fault.
98 =head1 COPYRIGHT AND LICENSE
104 L<Moose>, L<Moose::Meta::Class>
111 my $policy = shift || return;
113 unless (Moose::_is_class_already_loaded($policy)) {
114 ($policy->require) or confess "Could not load policy module " .
115 "'$policy' because : $UNIVERSAL::require::ERROR";
118 my $package = caller();
119 $package->can('meta') and
120 croak("'$package' already has a meta() method");
122 my $metaclass = 'Moose::Meta::Class';
123 $metaclass = $policy->metaclass($package)
124 if $policy->can('metaclass');
128 # build options out of policy's constants
129 $policy->can($_) and $options{":$_"} = $policy->$_($package)
136 # create a meta object so we can install &meta
137 my $meta = $metaclass->initialize($package => %options);
138 $meta->add_method('meta' => sub {
139 # we must re-initialize so that it works as expected in
140 # subclasses, since metaclass instances are singletons, this is
141 # not really a big deal anyway.
142 $metaclass->initialize((blessed($_[0]) || $_[0]) => %options)