use Class::MOP;
use Carp ();
+use Data::OptList;
use List::Util qw( first );
use List::MoreUtils qw( any all uniq first_index );
use Scalar::Util 'weaken', 'blessed';
-our $VERSION = '0.93_01';
+our $VERSION = '1.02';
$VERSION = eval $VERSION;
our $AUTHORITY = 'cpan:STEVAN';
);
}
-sub reinitialize {
- my $self = shift;
- my $pkg = shift;
-
- my $meta = blessed $pkg ? $pkg : Class::MOP::class_of($pkg);
-
- my %existing_classes;
- if ($meta) {
- %existing_classes = map { $_ => $meta->$_() } qw(
- attribute_metaclass
- method_metaclass
- wrapped_method_metaclass
- instance_metaclass
- constructor_class
- destructor_class
- error_class
- );
- }
-
- return $self->SUPER::reinitialize(
- $pkg,
- %existing_classes,
- @_,
- );
-}
-
sub _immutable_options {
my ( $self, @args ) = @_;
my $cache_ok = delete $options{cache};
- # something like Super::Class|Super::Class::2=Role|Role::1
- my $cache_key = join '=' => (
- join('|', @{$options{superclasses} || []}),
- join('|', sort @{$options{roles} || []}),
- );
+ my $cache_key
+ = _anon_cache_key( $options{superclasses}, $options{roles} );
if ($cache_ok && defined $ANON_CLASSES{$cache_key}) {
return $ANON_CLASSES{$cache_key};
return $new_class;
}
+sub _anon_cache_key {
+ # Makes something like Super::Class|Super::Class::2=Role|Role::1
+ return join '=' => (
+ join( '|', @{ $_[0] || [] } ),
+ join( '|', sort @{ $_[1] || [] } ),
+ );
+}
+
+sub reinitialize {
+ my $self = shift;
+ my $pkg = shift;
+
+ my $meta = blessed $pkg ? $pkg : Class::MOP::class_of($pkg);
+
+ my $cache_key;
+
+ my %existing_classes;
+ if ($meta) {
+ %existing_classes = map { $_ => $meta->$_() } qw(
+ attribute_metaclass
+ method_metaclass
+ wrapped_method_metaclass
+ instance_metaclass
+ constructor_class
+ destructor_class
+ error_class
+ );
+
+ $cache_key = _anon_cache_key(
+ [ $meta->superclasses ],
+ [ map { $_->name } @{ $meta->roles } ],
+ ) if $meta->is_anon_class;
+ }
+
+ my $new_meta = $self->SUPER::reinitialize(
+ $pkg,
+ %existing_classes,
+ @_,
+ );
+
+ return $new_meta unless defined $cache_key;
+
+ my $new_cache_key = _anon_cache_key(
+ [ $meta->superclasses ],
+ [ map { $_->name } @{ $meta->roles } ],
+ );
+
+ delete $ANON_CLASSES{$cache_key};
+ $ANON_CLASSES{$new_cache_key} = $new_meta;
+
+ return $new_meta;
+}
+
sub add_role {
my ($self, $role) = @_;
(blessed($role) && $role->isa('Moose::Meta::Role'))
sub superclasses {
my $self = shift;
- my @supers = @_;
- foreach my $super (@supers) {
- Class::MOP::load_class($super);
- my $meta = Class::MOP::class_of($super);
- $self->throw_error("You cannot inherit from a Moose Role ($super)")
+ my $supers = Data::OptList::mkopt(\@_);
+ foreach my $super (@{ $supers }) {
+ my ($name, $opts) = @{ $super };
+ Class::MOP::load_class($name, $opts);
+ my $meta = Class::MOP::class_of($name);
+ $self->throw_error("You cannot inherit from a Moose Role ($name)")
if $meta && $meta->isa('Moose::Meta::Role')
}
- return $self->SUPER::superclasses(@supers);
+ return $self->SUPER::superclasses(map { $_->[0] } @{ $supers });
}
### ---------------------------------------------
cache => 1,
);
+Each entry in both the C<superclasses> and the C<roles> option can be
+followed by a hash reference with arguments. The C<supperclasses>
+option can be supplied with a L<-version|Class::MOP/Class Loading
+Options> option that ensures the loaded superclass satisfies the
+required version. The C<role> option also takes the C<-version> as an
+argument, but the option hash reference can also contain any other
+role relevant values like exclusions or parameterized role arguments.
+
=item B<< $metaclass->make_immutable(%options) >>
This overrides the parent's method to add a few options. Specifically,
This overrides the parent's method in order to add support for
attribute triggers.
+=item B<< $metaclass->superclasses(@superclasses) >>
+
+This is the accessor allowing you to read or change the parents of
+the class.
+
+Each superclass can be followed by a hash reference containing a
+L<-version|Class::MOP/Class Loading Options> value. If the version
+requirement is not satisfied an error will be thrown.
+
=item B<< $metaclass->add_override_method_modifier($name, $sub) >>
This adds an C<override> method modifier to the package.
actually apply any role to the class; it is only for tracking role
applications.
-=item B<< $metaclass->does_role($role_name) >>
+=item B<< $metaclass->does_role($role) >>
-This returns a boolean indicating whether or not the class does the
-specified role. This tests both the class and its parents.
+This returns a boolean indicating whether or not the class does the specified
+role. The role provided can be either a role name or a L<Moose::Meta::Role>
+object. This tests both the class and its parents.
=item B<< $metaclass->excludes_role($role_name) >>