1 package MooseX::Types::Base;
5 MooseX::Types::Base - Type library base class
12 use Sub::Install qw( install_sub );
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 my $tobj = find_type_constraint($full);
81 # a possible wrapper around library functionality
82 my $wrap = $args{ -wrapper } || 'MooseX::Types';
84 # install Type name constant
86 code => $wrap->type_export_generator($type, $full),
91 # install is_Type test function
93 code => $wrap->check_export_generator($type, $full, $undef_msg),
98 # only install to_Type coercion handler if type can coerce
99 # or if we want to provide them anyway, e.g. declarations
100 if ($args{ -full } or $tobj->has_coercion) {
102 # install to_Type coercion handler
104 code => $wrap->coercion_export_generator($type, $full, $undef_msg),
115 This returns a type from the library's store by its name.
120 my ($class, $type) = @_;
122 # useful message if the type couldn't be found
123 croak "Unknown type '$type' in library '$class'"
124 unless $class->has_type($type);
126 # return real name of the type
127 return $class->type_storage->{ $type };
132 Returns a list of all known types by their name.
139 # return short names of all stored types
140 return keys %{ $class->type_storage };
145 Adds a new type to the library.
150 my ($class, $type) = @_;
152 # store type with library prefix as real name
153 $class->type_storage->{ $type } = "${class}::${type}";
158 Returns true or false depending on if this library knows a type by that
164 my ($class, $type) = @_;
166 # check if we stored a type under that name
167 return ! ! $class->type_storage->{ $type };
172 Returns the library's type storage hash reference. You shouldn't use this
173 method directly unless you know what you are doing. It is not an internal
174 method because overriding it makes virtual libraries very easy.
181 # return a reference to the storage in ourself
183 return \%{ $class . '::__MOOSEX_TYPELIBRARY_STORAGE' };
189 L<MooseX::Types::Moose>
191 =head1 AUTHOR AND COPYRIGHT
193 Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>, with many thanks to
194 the C<#moose> cabal on C<irc.perl.org>.
198 This program is free software; you can redistribute it and/or modify
199 it under the same terms as perl itself.