From: gfx Date: Thu, 1 Oct 2009 06:12:44 +0000 (+0900) Subject: Remove some private or useless methods/functions from Mouse::Meta::Module X-Git-Tag: 0.37_01~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=739525d0421188856c45329c8f001e9fbe0b30b2;p=gitmo%2FMouse.git Remove some private or useless methods/functions from Mouse::Meta::Module --- diff --git a/lib/Mouse/Meta/Class.pm b/lib/Mouse/Meta/Class.pm index 134189d..ecce4a3 100644 --- a/lib/Mouse/Meta/Class.pm +++ b/lib/Mouse/Meta/Class.pm @@ -103,7 +103,7 @@ sub add_attribute { my $inherited_attr; foreach my $class($self->linearized_isa){ - my $meta = Mouse::Meta::Module::get_metaclass_by_name($class) or next; + my $meta = Mouse::Util::get_metaclass_by_name($class) or next; $inherited_attr = $meta->get_attribute($name) and last; } @@ -409,7 +409,7 @@ sub does_role { || $self->throw_error("You must supply a role name to look for"); for my $class ($self->linearized_isa) { - my $meta = Mouse::Meta::Module::get_metaclass_by_name($class); + my $meta = Mouse::Util::get_metaclass_by_name($class); next unless $meta && $meta->can('roles'); for my $role (@{ $meta->roles }) { diff --git a/lib/Mouse/Meta/Module.pm b/lib/Mouse/Meta/Module.pm index c1364bc..aac7e81 100755 --- a/lib/Mouse/Meta/Module.pm +++ b/lib/Mouse/Meta/Module.pm @@ -7,57 +7,59 @@ use Scalar::Util qw/blessed weaken/; use Mouse::Util qw/:meta get_code_package not_supported load_class/; -{ - my %METACLASS_CACHE; - - # because Mouse doesn't introspect existing classes, we're forced to - # only pay attention to other Mouse classes - sub _metaclass_cache { - my($class, $name) = @_; - return $METACLASS_CACHE{$name}; - } - sub initialize { - my($class, $package_name, @args) = @_; +my %METACLASS_CACHE; - ($package_name && !ref($package_name)) - || $class->throw_error("You must pass a package name and it cannot be blessed"); +# because Mouse doesn't introspect existing classes, we're forced to +# only pay attention to other Mouse classes +sub _metaclass_cache { + my($class, $name) = @_; + return $METACLASS_CACHE{$name}; +} - return $METACLASS_CACHE{$package_name} - ||= $class->_construct_meta(package => $package_name, @args); - } +sub initialize { + my($class, $package_name, @args) = @_; - sub class_of{ - my($class_or_instance) = @_; - return undef unless defined $class_or_instance; - return $METACLASS_CACHE{ blessed($class_or_instance) || $class_or_instance }; - } + ($package_name && !ref($package_name)) + || $class->throw_error("You must pass a package name and it cannot be blessed"); - # Means of accessing all the metaclasses that have - # been initialized thus far - sub get_all_metaclasses { %METACLASS_CACHE } - sub get_all_metaclass_instances { values %METACLASS_CACHE } - sub get_all_metaclass_names { keys %METACLASS_CACHE } - sub get_metaclass_by_name { $METACLASS_CACHE{$_[0]} } - sub store_metaclass_by_name { $METACLASS_CACHE{$_[0]} = $_[1] } - sub weaken_metaclass { weaken($METACLASS_CACHE{$_[0]}) } - sub does_metaclass_exist { defined $METACLASS_CACHE{$_[0]} } - sub remove_metaclass_by_name { delete $METACLASS_CACHE{$_[0]} } + return $METACLASS_CACHE{$package_name} + ||= $class->_construct_meta(package => $package_name, @args); +} +sub class_of{ + my($class_or_instance) = @_; + return undef unless defined $class_or_instance; + return $METACLASS_CACHE{ blessed($class_or_instance) || $class_or_instance }; } +# Means of accessing all the metaclasses that have +# been initialized thus far +#sub get_all_metaclasses { %METACLASS_CACHE } +sub get_all_metaclass_instances { values %METACLASS_CACHE } +sub get_all_metaclass_names { keys %METACLASS_CACHE } +sub get_metaclass_by_name { $METACLASS_CACHE{$_[0]} } +#sub store_metaclass_by_name { $METACLASS_CACHE{$_[0]} = $_[1] } +#sub weaken_metaclass { weaken($METACLASS_CACHE{$_[0]}) } +#sub does_metaclass_exist { defined $METACLASS_CACHE{$_[0]} } +#sub remove_metaclass_by_name { delete $METACLASS_CACHE{$_[0]} } + + + sub name { $_[0]->{package} } -sub version { no strict 'refs'; ${shift->name.'::VERSION'} } -sub authority { no strict 'refs'; ${shift->name.'::AUTHORITY'} } -sub identifier { - my $self = shift; - return join '-' => ( - $self->name, - ($self->version || ()), - ($self->authority || ()), - ); -} +# The followings are Class::MOP specific methods + +#sub version { no strict 'refs'; ${shift->name.'::VERSION'} } +#sub authority { no strict 'refs'; ${shift->name.'::AUTHORITY'} } +#sub identifier { +# my $self = shift; +# return join '-' => ( +# $self->name, +# ($self->version || ()), +# ($self->authority || ()), +# ); +#} # add_attribute is an abstract method @@ -218,7 +220,7 @@ sub get_method_list { )}; my $meta = $class->initialize( $package_name, %initialize_options, @extra_options); - Mouse::Meta::Module::weaken_metaclass($package_name) + weaken $METACLASS_CACHE{$package_name} if $mortal; # FIXME totally lame @@ -273,7 +275,7 @@ sub get_method_list { @{$self->{superclasses}} = () if exists $self->{superclasses}; %{$stash} = (); - Mouse::Meta::Module::remove_metaclass_by_name($self->name); + delete $METACLASS_CACHE{$self->name}; no strict 'refs'; delete ${$ANON_PREFIX}{ $serial_id . '::' }; diff --git a/lib/Mouse/Util.pm b/lib/Mouse/Util.pm index 835db2a..123d5ff 100644 --- a/lib/Mouse/Util.pm +++ b/lib/Mouse/Util.pm @@ -35,16 +35,25 @@ our %EXPORT_TAGS = ( meta => [qw(does meta dump _MOUSE_VERBOSE)], ); +# aliases as public APIs + +BEGIN{ + *class_of = \&Mouse::Meta::Module::class_of; + *get_metaclass_by_name = \&Mouse::Meta::Module::get_metaclass_by_name; + *get_all_metaclass_instances = \&Mouse::Meta::Module::get_all_metaclass_instances; + *get_all_metaclass_names = \&Mouse::Meta::Module::get_all_metaclass_names; +} + # Moose::Util compatible utilities sub find_meta{ - return Mouse::Meta::Module::class_of( $_[0] ); + return class_of( $_[0] ); } sub does_role{ my ($class_or_obj, $role_name) = @_; - my $meta = Mouse::Meta::Module::class_of($class_or_obj); + my $meta = class_of($class_or_obj); (defined $role_name) || ($meta || 'Mouse::Meta::Class')->throw_error("You must supply a role name to does()"); @@ -52,8 +61,6 @@ sub does_role{ return defined($meta) && $meta->does_role($role_name); } - - BEGIN { my $impl; if ($] >= 5.009_005) { @@ -151,6 +158,9 @@ BEGIN { } } +# Utilities from Class::MOP + + # taken from Class/MOP.pm sub is_valid_class_name { my $class = shift; @@ -358,10 +368,18 @@ locally-defined method. =head3 C<< load_class(ClassName) >> -This will load a given C (or die if it's not loadable). +This will load a given C (or die if it is not loadable). This function can be used in place of tricks like C or using C. +=head2 C<< Mouse::Util::class_of(ClassName) >> + +The counterpart of C. This is not exportable. + +=head2 C<< Mouse::Util::get_metaclass_by_name(ClassName) >> + +The counterpart of C. This is not exportable. + =head2 MRO::Compat =head3 C diff --git a/t/030_roles/001_meta_role.t b/t/030_roles/001_meta_role.t index b6acf2b..f18af27 100755 --- a/t/030_roles/001_meta_role.t +++ b/t/030_roles/001_meta_role.t @@ -6,6 +6,8 @@ use warnings; use Test::More tests => 26; use Test::Exception; +use lib 't/lib'; +use Test::Mouse; # Mouse::Meta::Module->version use Mouse::Meta::Role; { diff --git a/t/030_roles/002_role.t b/t/030_roles/002_role.t index 2501185..67dcf44 100755 --- a/t/030_roles/002_role.t +++ b/t/030_roles/002_role.t @@ -6,6 +6,9 @@ use warnings; use Test::More tests => 40; use Test::Exception; +use lib 't/lib'; +use Test::Mouse; # Mouse::Meta::Module->version + =pod NOTE: diff --git a/t/300_immutable/001_immutable_moose.t b/t/300_immutable/001_immutable_moose.t index 1c561ae..2c89b94 100644 --- a/t/300_immutable/001_immutable_moose.t +++ b/t/300_immutable/001_immutable_moose.t @@ -6,6 +6,8 @@ use warnings; use Test::More tests => 15; use Test::Exception; +use lib 't/lib'; +use Test::Mouse; # Mouse::Meta::Module->version use Mouse::Meta::Role; diff --git a/t/lib/Test/Mouse.pm b/t/lib/Test/Mouse.pm index 80e754f..84f1973 100644 --- a/t/lib/Test/Mouse.pm +++ b/t/lib/Test/Mouse.pm @@ -55,6 +55,20 @@ sub has_attribute_ok ($$;$) { # Moose compatible methods/functions +package Mouse::Meta::Module; + +sub version { no strict 'refs'; ${shift->name.'::VERSION'} } +sub authority { no strict 'refs'; ${shift->name.'::AUTHORITY'} } +sub identifier { + my $self = shift; + return join '-' => ( + $self->name, + ($self->version || ()), + ($self->authority || ()), + ); +} + + package Mouse::Util::TypeConstraints; use Mouse::Util::TypeConstraints ();