X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FClass.pm;h=07b54e7aa211cbdc3d96202ece664a071e63b545;hb=4f5b44a07ba66f7a5997e6531d38a5fbf2cb3593;hp=8d829289022198757ea5fd07d9aa8a2a867de860;hpb=1a72f15c46ec908587e38d2d606ece4f180e2595;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Class.pm b/lib/Mouse/Meta/Class.pm index 8d82928..07b54e7 100644 --- a/lib/Mouse/Meta/Class.pm +++ b/lib/Mouse/Meta/Class.pm @@ -63,23 +63,37 @@ sub add_method { my $pkg = $self->name; no strict 'refs'; + no warnings 'redefine'; $self->{'methods'}->{$name}++; # Moose stores meta object here. *{ $pkg . '::' . $name } = $code; } # copied from Class::Inspector -sub get_method_list { +my $get_methods_for_class = sub { my $self = shift; - my $name = $self->name; + my $name = shift; 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|override|super)$/, grep { defined &{"${name}::$_"} } keys %{"${name}::"}; - push @functions, keys %{$self->{'methods'}->{$name}}; + push @functions, keys %{$self->{'methods'}->{$name}} if $self; wantarray ? @functions : \@functions; +}; + +sub get_method_list { + my $self = shift; + $get_methods_for_class->($self, $self->name); +} + +sub get_all_method_names { + my $self = shift; + my %uniq; + return grep { $uniq{$_}++ == 0 } + map { $get_methods_for_class->(undef, $_) } + $self->linearized_isa; } sub add_attribute { @@ -144,13 +158,25 @@ sub clone_instance { sub make_immutable { my $self = shift; - my %args = @_; + my %args = ( + inline_constructor => 1, + @_, + ); + my $name = $self->name; $self->{is_immutable}++; - $self->add_method('new' => Mouse::Meta::Method::Constructor->generate_constructor_method_inline( $self )); + + if ($args{inline_constructor}) { + $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 )); } + + # Moose's make_immutable returns true allowing calling code to skip setting an explicit true value + # at the end of a source file. + return 1; } sub make_mutable { confess "Mouse does not currently support 'make_mutable'" } @@ -159,37 +185,53 @@ sub is_immutable { $_[0]->{is_immutable} } sub attribute_metaclass { "Mouse::Meta::Class" } +sub _install_modifier { + my ( $self, $into, $type, $name, $code ) = @_; + + # which is modifer class available? + my $modifier_class = do { + if (eval "require Class::Method::Modifiers::Fast; 1") { + 'Class::Method::Modifiers::Fast'; + } elsif (eval "require Class::Method::Modifiers; 1") { + 'Class::Method::Modifiers'; + } else { + Carp::croak("Method modifiers require the use of Class::Method::Modifiers or Class::Method::Modifiers::Fast. Please install it from CPAN and file a bug report with this application."); + } + }; + my $modifier = $modifier_class->can('_install_modifier'); + + # replace this method itself :) + { + no strict 'refs'; + no warnings 'redefine'; + *{__PACKAGE__ . '::_install_modifier'} = sub { + my ( $self, $into, $type, $name, $code ) = @_; + $modifier->( + $into, + $type, + $name, + $code + ); + }; + } + + # call me. for first time. + $self->_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} } @@ -236,7 +278,8 @@ sub create { confess "creation of $package_name failed : $@" if $@; }; - delete @options{qw( + my %initialize_options = %options; + delete @initialize_options{qw( package superclasses attributes @@ -244,7 +287,7 @@ sub create { version authority )}; - my $meta = $self->initialize( $package_name => %options ); + my $meta = $self->initialize( $package_name => %initialize_options ); # FIXME totally lame $meta->add_method('meta' => sub { @@ -323,7 +366,7 @@ this class and its superclasses. Returns a mapping of attribute names to their corresponding L objects. -=head2 has_attribute Name -> Boool +=head2 has_attribute Name -> Bool Returns whether we have a L with the given name.