From: Stevan Little Date: Mon, 23 Oct 2006 15:05:08 +0000 (+0000) Subject: adding unimport to Moose::Util::TypeConstraints X-Git-Tag: 0_15~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=571dd39fd189fcc5bf998f721a3db8219f294cfb;p=gitmo%2FMoose.git adding unimport to Moose::Util::TypeConstraints --- diff --git a/Changes b/Changes index 60118f5..ef0d9fb 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,12 @@ Revision history for Perl extension Moose 0.15 + + * Moose::Util::TypeConstraints + - added &unimport so that you can clean out + your class namespace of these exported + keywords + * Moose::Meta::Class - fixed minor issue which occasionally comes up during global destruction diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index 056d052..3df5fba 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -6,23 +6,45 @@ use warnings; use Carp 'confess'; use Scalar::Util 'blessed'; +use B 'svref_2object'; +use Sub::Exporter; -our $VERSION = '0.08'; +our $VERSION = '0.09'; use Moose::Meta::TypeConstraint; use Moose::Meta::TypeCoercion; -use Sub::Exporter -setup => { - exports => [qw/ - type subtype as where message - coerce from via - enum - find_type_constraint - /], - groups => { - default => [':all'] +my @exports = qw/ + type subtype as where message + coerce from via + enum + find_type_constraint +/; + +Sub::Exporter::setup_exporter({ + exports => \@exports, + groups => { default => [':all'] } +}); + +sub unimport { + no strict 'refs'; + my $class = caller(); + # loop through the exports ... + foreach my $name (@exports) { + # if we find one ... + if (defined &{$class . '::' . $name}) { + my $keyword = \&{$class . '::' . $name}; + + # make sure it is from Moose + my $pkg_name = eval { svref_2object($keyword)->GV->STASH->NAME }; + next if $@; + next if $pkg_name ne 'Moose::Util::TypeConstraints'; + + # and if it is from Moose then undef the slot + delete ${$class . '::'}{$name}; + } } -}; +} { my %TYPES; @@ -351,6 +373,17 @@ This is just sugar for the type coercion construction syntax. =back +=head2 Namespace Management + +=over 4 + +=item B + +This will remove all the type constraint keywords from the +calling class namespace. + +=back + =head1 BUGS All complex software has bugs lurking in it, and this module is no diff --git a/t/018_import_unimport.t b/t/018_import_unimport.t index 39c45ad..d49d82c 100644 --- a/t/018_import_unimport.t +++ b/t/018_import_unimport.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 23; +use Test::More tests => 45; BEGIN { use_ok('Moose'); @@ -43,3 +43,32 @@ ok(!$@, '... Moose succesfully un-exported from Foo'); ok(!Foo->can($_), '... Foo can no longer do ' . $_) for @moose_exports; can_ok('Foo', $_) for @moose_not_unimported; +# and check the type constraints as well + +my @moose_type_constraint_exports = qw( + type subtype as where message + coerce from via + enum + find_type_constraint +); + +{ + package Bar; +} + +eval q{ + package Bar; + use Moose::Util::TypeConstraints; +}; +ok(!$@, '... Moose::Util::TypeConstraints succesfully exported into Bar'); + +can_ok('Bar', $_) for @moose_type_constraint_exports; + +eval q{ + package Bar; + no Moose::Util::TypeConstraints; +}; +ok(!$@, '... Moose::Util::TypeConstraints succesfully un-exported from Bar'); + +ok(!Bar->can($_), '... Bar can no longer do ' . $_) for @moose_type_constraint_exports; +