1 package MooseX::TypeLibrary::Base;
5 MooseX::TypeLibrary::Base - Type library base class
12 use Sub::Install qw( install_sub );
14 use MooseX::TypeLibrary::Util qw( filter_tags );
15 use Moose::Util::TypeConstraints;
20 You normally won't need to interact with this class by yourself. It is
21 merely a collection of functionality that type libraries need to
22 interact with moose and the rest of the L<MooseX::TypeLibrary> module.
26 my $UndefMsg = q{Unable to find type '%s' in library '%s'};
34 Provides the import mechanism for your library. See
35 L<MooseX::TypeLibrary/"LIBRARY USAGE"> for syntax details on this.
40 my ($class, @args) = @_;
42 # separate tags from types and possible options
43 my ($options) = grep { ref $_ eq 'HASH' } @args;
46 grep { ref $_ ne 'HASH' }
48 my $callee = ($options && $options->{ -into } || scalar(caller));
50 # :all replaces types with full list
51 @$types = $class->type_names if $tags->{all};
54 # export all requested types
55 for my $type (@$types) {
56 $class->export_type_into(
59 sprintf($UndefMsg, $type, $class),
60 ($options ? %$options : ()),
66 =head2 export_type_into
68 Exports one specific type into a target package.
72 sub export_type_into {
73 my ($class, $target, $type, $undef_msg, %args) = @_;
75 # the real type name and its type object
76 my $full = $class->get_type($type);
77 my $tobj = find_type_constraint($full);
79 # a possible wrapper around library functionality
80 my $wrap = $args{ -wrapper } || 'MooseX::TypeLibrary';
82 # install Type name constant
84 code => $wrap->type_export_generator($type, $full),
89 # install is_Type test function
91 code => $wrap->check_export_generator($type, $full, $undef_msg),
96 # only install to_Type coercion handler if type can coerce
97 # or if we want to provide them anyway, e.g. declarations
98 if ($args{ -full } or $tobj->has_coercion) {
100 # install to_Type coercion handler
102 code => $wrap->coercion_export_generator($type, $full, $undef_msg),
113 This returns a type from the library's store by its name.
118 my ($class, $type) = @_;
120 # useful message if the type couldn't be found
121 croak "Unknown type '$type' in library '$class'"
122 unless $class->has_type($type);
124 # return real name of the type
125 return $class->type_storage->{ $type };
130 Returns a list of all known types by their name.
137 # return short names of all stored types
138 return keys %{ $class->type_storage };
143 Adds a new type to the library.
148 my ($class, $type) = @_;
150 # store type with library prefix as real name
151 $class->type_storage->{ $type } = "${class}::${type}";
156 Returns true or false depending on if this library knows a type by that
162 my ($class, $type) = @_;
164 # check if we stored a type under that name
165 return ! ! $class->type_storage->{ $type };
170 Returns the library's type storage hash reference. You shouldn't use this
171 method directly unless you know what you are doing. It is not an internal
172 method because overriding it makes virtual libraries very easy.
179 # return a reference to the storage in ourself
181 return \%{ $class . '::__MOOSEX_TYPELIBRARY_STORAGE' };
187 L<MooseX::TypeLibrary::Moose>
189 =head1 AUTHOR AND COPYRIGHT
191 Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>, with many thanks to
192 the C<#moose> cabal on C<irc.perl.org>.
196 This program is free software; you can redistribute it and/or modify
197 it under the same terms as perl itself.