X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FTypes%2FCombine.pm;h=cc858154bf3e396c3c6dfb498d719f8859d1e9e1;hb=4fe418ef8e65a7277d25459d5c80341ab76dce3f;hp=5269388e05ceb09068a481416e6e15712c790b23;hpb=6d7f2c07382702d3807060253671ac8572c7e33e;p=gitmo%2FMooseX-Types.git diff --git a/lib/MooseX/Types/Combine.pm b/lib/MooseX/Types/Combine.pm index 5269388..cc85815 100644 --- a/lib/MooseX/Types/Combine.pm +++ b/lib/MooseX/Types/Combine.pm @@ -1,15 +1,10 @@ -=head1 NAME - -MooseX::Types::Combine - Combine type libraries for exporting - -=cut - package MooseX::Types::Combine; -our $VERSION = "0.22"; + +# ABSTRACT: Combine type libraries for exporting use strict; use warnings; -use Class::MOP (); +use Module::Runtime 'use_module'; =head1 SYNOPSIS @@ -25,9 +20,9 @@ use Class::MOP (); =head1 DESCRIPTION -Allows you to export types from multiple type libraries. +Allows you to export types from multiple type libraries. -Libraries on the right side of the type libs passed to L +Libraries on the right end of the list passed to L take precedence over those on the left in case of conflicts. =cut @@ -36,20 +31,23 @@ sub import { my ($class, @types) = @_; my $caller = caller; - my @type_libs = $class->provide_types_from; - Class::MOP::load_class($_) for @type_libs; + my %types = $class->_provided_types; - my %types = map { - my $lib = $_; - map +($_ => $lib), $lib->type_names - } @type_libs; + if ( grep { $_ eq ':all' } @types ) { + $_->import( { -into => $caller }, q{:all} ) + for $class->provide_types_from; + return; + } my %from; for my $type (@types) { - die - "$caller asked for a type ($type) which is not found in any of the" - . " type libraries (@type_libs) combined by $class\n" - unless $types{$type}; + unless ($types{$type}) { + my @type_libs = $class->provide_types_from; + + die + "$caller asked for a type ($type) which is not found in any of the" + . " type libraries (@type_libs) combined by $class\n"; + } push @{ $from{ $types{$type} } }, $type; } @@ -72,18 +70,45 @@ sub provide_types_from { my $store = do { no strict 'refs'; \@{ "${class}::__MOOSEX_TYPELIBRARY_LIBRARIES" } }; - @$store = @libs if @libs; + if (@libs) { + $class->_check_type_lib($_) for @libs; + @$store = @libs; + + my %types = map { + my $lib = $_; + map +( $_ => $lib ), $lib->type_names + } @libs; + + $class->_provided_types(%types); + } @$store; } -=head1 SEE ALSO +sub _check_type_lib { + my ($class, $lib) = @_; -L + use_module($lib); + + die "Cannot use $lib in a combined type library, it does not provide any types" + unless $lib->can('type_names'); +} + +sub _provided_types { + my ($class, %types) = @_; + + my $types = + do { no strict 'refs'; \%{ "${class}::__MOOSEX_TYPELIBRARY_TYPES" } }; -=head1 AUTHOR + %$types = %types + if keys %types; -See L. + %$types; +} + +=head1 SEE ALSO + +L =head1 LICENSE