X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMoose-Policy.git;a=blobdiff_plain;f=lib%2FMoose%2FPolicy.pm;h=ffe4b69130a71405f0db25804b03be5b8fbe298a;hp=ee4a6609e9341b4c17a30277dd62ccb44642d068;hb=b9238462c9d0e46830c5bd0ce94771826c1079a4;hpb=5d1afb587de9486c5740ff030d6a60b434634c4a diff --git a/lib/Moose/Policy.pm b/lib/Moose/Policy.pm index ee4a660..ffe4b69 100644 --- a/lib/Moose/Policy.pm +++ b/lib/Moose/Policy.pm @@ -4,6 +4,44 @@ package Moose::Policy; use strict; use warnings; +our $VERSION = '0.01'; + +use Moose (); +use Carp 'confess'; +use Scalar::Util 'blessed'; + +sub import { + shift; + + my $policy = shift || return; + + unless (Moose::_is_class_already_loaded($policy)) { + ($policy->require) + || confess "Could not load policy module '$policy' because : " . $UNIVERSAL::require::ERROR; + } + + my $metaclass = 'Moose::Meta::Class'; + $metaclass = $policy->metaclass if $policy->can('metaclass'); + + my %options; + + $options{':attribute_metaclass'} = $policy->attribute_metaclass + if $policy->can('attribute_metaclass'); + + my $package = caller(); + + # create a meta object so we can install &meta + my $meta = $metaclass->initialize($package => %options); + $meta->add_method('meta' => sub { + # we must re-initialize so that it + # works as expected in subclasses, + # since metaclass instances are + # singletons, this is not really a + # big deal anyway. + $metaclass->initialize((blessed($_[0]) || $_[0]) => %options) + }); +} + 1; __END__