-#!/usr/bin/env perl
package Mouse::Meta::Class;
use strict;
use warnings;
my $pkg = $self->name;
no strict 'refs';
+ no warnings 'redefine';
$self->{'methods'}->{$name}++; # Moose stores meta object here.
*{ $pkg . '::' . $name } = $code;
}
no strict 'refs';
# Get all the CODE symbol table entries
my @functions =
- grep !/(?:has|with|around|before|after|blessed|extends|confess)/,
+ grep !/^(?:has|with|around|before|after|blessed|extends|confess)$/,
grep { defined &{"${name}::$_"} }
keys %{"${name}::"};
push @functions, keys %{$self->{'methods'}->{$name}};
my %args = @_;
my $name = $self->name;
$self->{is_immutable}++;
- $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
+
+ if ($self->name->can('new') != Mouse::Object->can('new')) {
+ warn "Not inlining a constructor for ".$self->name." since it is not inheriting the default Mouse::Object constructor\n";
+ }
+ else {
+ $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self ));
+ }
+
if ($args{inline_destructor}) {
$self->add_method('DESTROY' => Mouse::Meta::Method::Destructor->generate_destructor_method_inline( $self ));
}
}
-sub make_mutable {
- Carp::croak "Mouse::Meta::Class->make_mutable does not supported by Mouse";
-}
+
+sub make_mutable { confess "Mouse does not currently support 'make_mutable'" }
+
sub is_immutable { $_[0]->{is_immutable} }
sub attribute_metaclass { "Mouse::Meta::Class" }
+sub _install_modifier {
+ my ( $self, $into, $type, $name, $code ) = @_;
+ if (eval "require Class::Method::Modifiers::Fast; 1") {
+ Class::Method::Modifiers::Fast::_install_modifier(
+ $into,
+ $type,
+ $name,
+ $code
+ );
+ }
+ else {
+ require Class::Method::Modifiers;
+ Class::Method::Modifiers::_install_modifier(
+ $into,
+ $type,
+ $name,
+ $code
+ );
+ }
+}
+
sub add_before_method_modifier {
- my ($self, $name, $code) = @_;
- require Class::Method::Modifiers;
- Class::Method::Modifiers::_install_modifier(
- $self->name,
- 'before',
- $name,
- $code,
- );
+ my ( $self, $name, $code ) = @_;
+ $self->_install_modifier( $self->name, 'before', $name, $code );
}
sub add_around_method_modifier {
- my ($self, $name, $code) = @_;
- require Class::Method::Modifiers;
- Class::Method::Modifiers::_install_modifier(
- $self->name,
- 'around',
- $name,
- $code,
- );
+ my ( $self, $name, $code ) = @_;
+ $self->_install_modifier( $self->name, 'around', $name, $code );
}
sub add_after_method_modifier {
- my ($self, $name, $code) = @_;
- require Class::Method::Modifiers;
- Class::Method::Modifiers::_install_modifier(
- $self->name,
- 'after',
- $name,
- $code,
- );
+ my ( $self, $name, $code ) = @_;
+ $self->_install_modifier( $self->name, 'after', $name, $code );
}
sub roles { $_[0]->{roles} }
sub does_role {
my ($self, $role_name) = @_;
+
(defined $role_name)
|| confess "You must supply a role name to look for";
+
for my $role (@{ $self->{roles} }) {
return 1 if $role->name eq $role_name;
}
+
return 0;
}
sub create {
- my ( $class, @args ) = @_;
-
- unshift @args, 'package' if @args % 2 == 1;
-
- my (%options) = @args;
- my $package_name = $options{package};
+ my ($self, $package_name, %options) = @_;
(ref $options{superclasses} eq 'ARRAY')
|| confess "You must pass an ARRAY ref of superclasses"
if exists $options{superclasses};
-
+
(ref $options{attributes} eq 'ARRAY')
|| confess "You must pass an ARRAY ref of attributes"
- if exists $options{attributes};
-
+ if exists $options{attributes};
+
(ref $options{methods} eq 'HASH')
|| confess "You must pass a HASH ref of methods"
- if exists $options{methods};
+ if exists $options{methods};
do {
- # XXX should I implement Mouse::Meta::Module?
- my $package_name = $options{package};
-
( defined $package_name && $package_name )
|| confess "You must pass a package name";
confess "creation of $package_name failed : $@" if $@;
};
- my (%initialize_options) = @args;
+ my %initialize_options = %options;
delete @initialize_options{qw(
package
superclasses
version
authority
)};
- my $meta = $class->initialize( $package_name => %initialize_options );
+ my $meta = $self->initialize( $package_name => %initialize_options );
# FIXME totally lame
$meta->add_method('meta' => sub {
- $class->initialize(ref($_[0]) || $_[0]);
+ $self->initialize(ref($_[0]) || $_[0]);
});
$meta->superclasses(@{$options{superclasses}})
Returns a mapping of attribute names to their corresponding
L<Mouse::Meta::Attribute> objects.
-=head2 has_attribute Name -> Boool
+=head2 has_attribute Name -> Bool
Returns whether we have a L<Mouse::Meta::Attribute> with the given name.