From: gfx Date: Mon, 26 Oct 2009 07:33:42 +0000 (+0900) Subject: More _generate_class_type_for() X-Git-Tag: 0.40_01~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=commitdiff_plain;h=745220df2da2256a9bd2692ac585f39b35ed19df;hp=30518636145442894e3a55a06ef3a57db268c76d More _generate_class_type_for() --- diff --git a/lib/Mouse/Meta/Attribute.pm b/lib/Mouse/Meta/Attribute.pm index c5482aa..77be3b4 100644 --- a/lib/Mouse/Meta/Attribute.pm +++ b/lib/Mouse/Meta/Attribute.pm @@ -353,7 +353,9 @@ sub _canonicalize_handles { my $meta = Mouse::Meta::Class->initialize("$class_or_role"); # "" for stringify return map { $_ => $_ } grep { $_ ne 'meta' && !Mouse::Object->can($_) && $_ =~ $handles } - $meta->isa('Mouse::Meta::Class') ? $meta->get_all_method_names : $meta->get_method_list; + Mouse::Util::TypeConstraints::_is_a_metarole($meta) + ? $meta->get_method_list + : $meta->get_all_method_names; } else { $self->throw_error("Unable to canonicalize the 'handles' option with $handles"); diff --git a/lib/Mouse/Meta/Class.pm b/lib/Mouse/Meta/Class.pm index 3827636..ba6acad 100644 --- a/lib/Mouse/Meta/Class.pm +++ b/lib/Mouse/Meta/Class.pm @@ -56,7 +56,7 @@ sub superclasses { foreach my $super(@_){ Mouse::Util::load_class($super); my $meta = Mouse::Util::get_metaclass_by_name($super); - if($meta && $meta->isa('Mouse::Meta::Role')){ + if(Mouse::Util::TypeConstraints::_is_a_metarole($meta)){ $self->throw_error("You cannot inherit from a Mouse Role ($super)"); } } diff --git a/lib/Mouse/Meta/Module.pm b/lib/Mouse/Meta/Module.pm index 132e4af..9dbe85d 100755 --- a/lib/Mouse/Meta/Module.pm +++ b/lib/Mouse/Meta/Module.pm @@ -1,6 +1,8 @@ package Mouse::Meta::Module; use Mouse::Util qw/:meta get_code_package get_code_ref load_class not_supported/; # enables strict and warnings +use Mouse::Util::TypeConstraints (); + use Carp (); use Scalar::Util qw/blessed weaken/; @@ -164,7 +166,7 @@ sub get_method_list { my $superclasses; if(exists $options{superclasses}){ - if($self->isa('Mouse::Meta::Role')){ + if(Mouse::Util::TypeConstraints::_is_a_metarole($self)){ delete $options{superclasses}; } else{ diff --git a/lib/Mouse/Meta/Role.pm b/lib/Mouse/Meta/Role.pm index 5c42f14..d10023c 100644 --- a/lib/Mouse/Meta/Role.pm +++ b/lib/Mouse/Meta/Role.pm @@ -186,10 +186,10 @@ sub apply { my $instance; - if($applicant->isa('Mouse::Meta::Class')){ # Application::ToClass + if(Mouse::Util::TypeConstraints::_is_a_metaclass($applicant)){ # Application::ToClass $args{_to} = 'class'; } - elsif($applicant->isa('Mouse::Meta::Role')){ # Application::ToRole + elsif(Mouse::Util::TypeConstraints::_is_a_metarole($applicant)){ # Application::ToRole $args{_to} = 'role'; } else{ # Appplication::ToInstance diff --git a/lib/Mouse/Util.pm b/lib/Mouse/Util.pm index a2593cf..7ac2a08 100644 --- a/lib/Mouse/Util.pm +++ b/lib/Mouse/Util.pm @@ -252,8 +252,7 @@ sub apply_all_roles { my $role_name = $roles[-1][0]; load_class($role_name); - my $metarole = get_metaclass_by_name($role_name); - ( $metarole && $metarole->isa('Mouse::Meta::Role') ) + Mouse::Util::TypeConstraints::_is_a_metarole( get_metaclass_by_name($role_name) ) || $applicant->meta->throw_error("You can only consume roles, $role_name(".$role_name->meta.") is not a Mouse role"); } diff --git a/lib/Mouse/Util/TypeConstraints.pm b/lib/Mouse/Util/TypeConstraints.pm index ef0751d..b0db34b 100644 --- a/lib/Mouse/Util/TypeConstraints.pm +++ b/lib/Mouse/Util/TypeConstraints.pm @@ -74,6 +74,14 @@ BEGIN { sub list_all_type_constraints { keys %TYPE } } +# is-a predicates +BEGIN{ + _generate_class_type_for('Mouse::Meta::TypeConstraint' => '_is_a_type_constraint'); + _generate_class_type_for('Mouse::Meta::Class' => '_is_a_metaclass'); + _generate_class_type_for('Mouse::Meta::Role' => '_is_a_metarole'); +} + + sub _create_type{ my $mode = shift; @@ -222,7 +230,7 @@ sub _find_or_create_regular_type{ return; } - if($meta->isa('Mouse::Meta::Role')){ + if(_is_a_metarole($meta)){ return role_type($spec); } else{ @@ -371,7 +379,7 @@ sub _parse_type{ sub find_type_constraint { my($spec) = @_; - return $spec if blessed($spec) && $spec->isa('Mouse::Meta::TypeConstraint'); + return $spec if _is_a_type_constraint($spec); $spec =~ s/\s+//g; return $TYPE{$spec}; @@ -379,7 +387,7 @@ sub find_type_constraint { sub find_or_parse_type_constraint { my($spec) = @_; - return $spec if blessed($spec) && $spec->isa('Mouse::Meta::TypeConstraint'); + return $spec if _is_a_type_constraint($spec); $spec =~ s/\s+//g; return $TYPE{$spec} || do{