1 package MooseX::Types::Base;
5 MooseX::Types::Base - Type library base class
12 use Sub::Install qw( install_sub );
13 use Carp::Clan qw( ^MooseX::Types );
14 use MooseX::Types::Util qw( filter_tags );
15 use Moose::Util::TypeConstraints;
18 use namespace::clean -except => [qw( meta )];
22 You normally won't need to interact with this class by yourself. It is
23 merely a collection of functionality that type libraries need to
24 interact with moose and the rest of the L<MooseX::Types> module.
28 my $UndefMsg = q{Unable to find type '%s' in library '%s'};
36 Provides the import mechanism for your library. See
37 L<MooseX::Types/"LIBRARY USAGE"> for syntax details on this.
42 my ($class, @args) = @_;
44 # separate tags from types and possible options
45 my ($options) = grep { ref $_ eq 'HASH' } @args;
48 grep { ref $_ ne 'HASH' }
50 my $callee = ($options && $options->{ -into } || scalar(caller));
52 # :all replaces types with full list
53 @$types = $class->type_names if $tags->{all};
56 # export all requested types
57 for my $type (@$types) {
58 $class->export_type_into(
61 sprintf($UndefMsg, $type, $class),
62 ($options ? %$options : ()),
68 =head2 export_type_into
70 Exports one specific type into a target package.
74 sub export_type_into {
75 my ($class, $target, $type, $undef_msg, %args) = @_;
77 # the real type name and its type object
78 my $full = $class->get_type($type)
79 or croak "No fully qualified type name stored for '$type'";
80 my $tobj = find_type_constraint($full);
82 # a possible wrapper around library functionality
83 my $wrap = $args{ -wrapper } || 'MooseX::Types';
85 # install Type name constant
87 code => $wrap->type_export_generator($type, $full),
92 # install is_Type test function
94 code => $wrap->check_export_generator($type, $full, $undef_msg),
99 # only install to_Type coercion handler if type can coerce
100 # or if we want to provide them anyway, e.g. declarations
101 if ($args{ -full } or $tobj and $tobj->has_coercion) {
103 # install to_Type coercion handler
105 code => $wrap->coercion_export_generator($type, $full, $undef_msg),
116 This returns a type from the library's store by its name.
121 my ($class, $type) = @_;
123 # useful message if the type couldn't be found
124 croak "Unknown type '$type' in library '$class'"
125 unless $class->has_type($type);
127 # return real name of the type
128 return $class->type_storage->{ $type };
133 Returns a list of all known types by their name.
140 # return short names of all stored types
141 return keys %{ $class->type_storage };
146 Adds a new type to the library.
151 my ($class, $type) = @_;
153 # store type with library prefix as real name
154 $class->type_storage->{ $type } = "${class}::${type}";
159 Returns true or false depending on if this library knows a type by that
165 my ($class, $type) = @_;
167 # check if we stored a type under that name
168 return ! ! $class->type_storage->{ $type };
173 Returns the library's type storage hash reference. You shouldn't use this
174 method directly unless you know what you are doing. It is not an internal
175 method because overriding it makes virtual libraries very easy.
182 # return a reference to the storage in ourself
184 return \%{ $class . '::__MOOSEX_TYPELIBRARY_STORAGE' };
190 L<MooseX::Types::Moose>
192 =head1 AUTHOR AND COPYRIGHT
194 Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>, with many thanks to
195 the C<#moose> cabal on C<irc.perl.org>.
199 This program is free software; you can redistribute it and/or modify
200 it under the same terms as perl itself.