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;
21 You normally won't need to interact with this class by yourself. It is
22 merely a collection of functionality that type libraries need to
23 interact with moose and the rest of the L<MooseX::TypeLibrary> module.
27 my $UndefMsg = q{Unable to find type '%s' in library '%s'};
35 Provides the import mechanism for your library. See
36 L<MooseX::TypeLibrary/"LIBRARY USAGE"> for syntax details on this.
41 my ($class, @args) = @_;
43 # separate tags from types and possible options
44 my ($options) = grep { ref $_ eq 'HASH' } @args;
47 grep { ref $_ ne 'HASH' }
49 my $callee = ($options && $options->{ -into } || scalar(caller));
51 # :all replaces types with full list
52 @$types = $class->type_names if $tags->{all};
55 # export all requested types
56 for my $type (@$types) {
57 $class->export_type_into(
60 sprintf($UndefMsg, $type, $class),
61 ($options ? %$options : ()),
67 =head2 export_type_into
69 Exports one specific type into a target package.
73 sub export_type_into {
74 my ($class, $target, $type, $undef_msg, %args) = @_;
76 # the real type name and its type object
77 my $full = $class->get_type($type);
78 my $tobj = find_type_constraint($full);
80 # a possible wrapper around library functionality
81 my $wrap = $args{ -wrapper } || 'MooseX::TypeLibrary';
83 # install Type name constant
85 code => $wrap->type_export_generator($type, $full),
90 # install is_Type test function
92 code => $wrap->check_export_generator($type, $full, $undef_msg),
97 # only install to_Type coercion handler if type can coerce
98 # or if we want to provide them anyway, e.g. declarations
99 if ($args{ -full } or $tobj->has_coercion) {
101 # install to_Type coercion handler
103 code => $wrap->coercion_export_generator($type, $full, $undef_msg),
114 This returns a type from the library's store by its name.
119 my ($class, $type) = @_;
121 # useful message if the type couldn't be found
122 croak "Unknown type '$type' in library '$class'"
123 unless $class->has_type($type);
125 # return real name of the type
126 return $class->type_storage->{ $type };
131 Returns a list of all known types by their name.
138 # return short names of all stored types
139 return keys %{ $class->type_storage };
144 Adds a new type to the library.
149 my ($class, $type) = @_;
151 # store type with library prefix as real name
152 $class->type_storage->{ $type } = "${class}::${type}";
157 Returns true or false depending on if this library knows a type by that
163 my ($class, $type) = @_;
165 # check if we stored a type under that name
166 return ! ! $class->type_storage->{ $type };
171 Returns the library's type storage hash reference. You shouldn't use this
172 method directly unless you know what you are doing. It is not an internal
173 method because overriding it makes virtual libraries very easy.
180 # return a reference to the storage in ourself
182 return \%{ $class . '::__MOOSEX_TYPELIBRARY_STORAGE' };
188 L<MooseX::TypeLibrary::Moose>
190 =head1 AUTHOR AND COPYRIGHT
192 Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>, with many thanks to
193 the C<#moose> cabal on C<irc.perl.org>.
197 This program is free software; you can redistribute it and/or modify
198 it under the same terms as perl itself.