bump version to 0.26
[gitmo/MooseX-Types.git] / lib / MooseX / Types / Util.pm
index 5bd5bcf..bf91020 100644 (file)
@@ -1,4 +1,5 @@
 package MooseX::Types::Util;
+our $VERSION = "0.26";
 
 =head1 NAME
 
@@ -8,6 +9,7 @@ MooseX::Types::Util - Common utility functions for the module
 
 use warnings;
 use strict;
+use Scalar::Util 'blessed';
 
 use base 'Exporter';
 
@@ -18,7 +20,7 @@ L<MooseX::Types> might need.
 
 =cut
 
-our @EXPORT_OK = qw( filter_tags );
+our @EXPORT_OK = qw( filter_tags has_available_type_export );
 
 =head1 FUNCTIONS
 
@@ -43,14 +45,63 @@ sub filter_tags {
     return \%tags, \@other;
 }
 
+=head2 has_available_type_export
+
+  TypeConstraint | Undef = has_available_type_export($package, $name);
+
+This function allows you to introspect if a given type export is available 
+I<at this point in time>. This means that the C<$package> must have imported
+a typeconstraint with the name C<$name>, and it must be still in its symbol
+table.
+
+Two arguments are expected:
+
+=over 4
+
+=item $package
+
+The name of the package to introspect.
+
+=item $name
+
+The name of the type export to introspect.
+
+=back
+
+B<Note> that the C<$name> is the I<exported> name of the type, not the declared
+one. This means that if you use L<Sub::Exporter>s functionality to rename an import
+like this:
+
+  use MyTypes Str => { -as => 'MyStr' };
+
+you would have to introspect this type like this:
+
+  has_available_type_export $package, 'MyStr';
+
+The return value will be either the type constraint that belongs to the export
+or an undefined value.
+
+=cut
+
+sub has_available_type_export {
+    my ($package, $name) = @_;
+
+    my $sub = $package->can($name)
+        or return undef;
+
+    return undef
+        unless blessed $sub && $sub->isa('MooseX::Types::EXPORTED_TYPE_CONSTRAINT');
+
+    return $sub->();
+}
+
 =head1 SEE ALSO
 
 L<MooseX::Types::Moose>, L<Exporter>
 
-=head1 AUTHOR AND COPYRIGHT
+=head1 AUTHOR
 
-Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>, with many thanks to
-the C<#moose> cabal on C<irc.perl.org>.
+See L<MooseX::Types/AUTHOR>.
 
 =head1 LICENSE