1 package MooseX::TypeLibrary::Base;
6 use Sub::Install qw( install_sub );
8 use Moose::Util::TypeConstraints;
11 my $UndefMsg = q{Unable to find type '%s' in library '%s'};
14 my ($class, @types) = @_;
17 @types = map { $_ eq ':all' ? $class->type_names : $_ } @types;
20 # export all requested types
21 for my $type (@types) {
22 $class->export_type_into(
23 scalar(caller), $type, sprintf $UndefMsg, $type, $class );
28 sub export_type_into {
29 my ($class, $target, $type, $undef_msg, %args) = @_;
31 # the real type name and its type object
32 my $full = $class->get_type($type);
33 my $tobj = find_type_constraint($full);
36 # install Type name constant
38 code => MooseX::TypeLibrary->type_export_generator($type, $full),
43 # install is_Type test function
45 code => MooseX::TypeLibrary
46 ->check_export_generator($type, $full, $undef_msg),
51 # only install to_Type coercion handler if type can coerce
52 return 1 unless $args{ -full } or $tobj->has_coercion;
54 # install to_Type coercion handler
56 code => MooseX::TypeLibrary
57 ->coercion_export_generator($type, $full, $undef_msg),
66 my ($class, $type) = @_;
68 # useful message if the type couldn't be found
69 croak "Unknown type '$type' in library '$class'"
70 unless $class->has_type($type);
72 # return real name of the type
73 return $class->type_storage->{ $type };
79 # return short names of all stored types
80 return keys %{ $class->type_storage };
84 my ($class, $type) = @_;
86 # store type with library prefix as real name
87 $class->type_storage->{ $type } = "${class}::${type}";
91 my ($class, $type) = @_;
93 # check if we stored a type under that name
94 return ! ! $class->type_storage->{ $type };
100 # return a reference to the storage in ourself
102 return \%{ $class . '::__MOOSEX_TYPELIBRARY_STORAGE' };