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, @orig_types) = @_;
42 # separate tags from types
43 my ($tags, $types) = filter_tags @orig_types;
45 # :all replaces types with full list
46 @$types = $class->type_names if $tags->{all};
49 # export all requested types
50 for my $type (@$types) {
51 $class->export_type_into(
54 sprintf($UndefMsg, $type, $class),
60 =head2 export_type_into
62 Exports one specific type into a target package.
66 sub export_type_into {
67 my ($class, $target, $type, $undef_msg, %args) = @_;
69 # the real type name and its type object
70 my $full = $class->get_type($type);
71 my $tobj = find_type_constraint($full);
74 # install Type name constant
76 code => MooseX::TypeLibrary->type_export_generator($type, $full),
81 # install is_Type test function
83 code => MooseX::TypeLibrary
84 ->check_export_generator($type, $full, $undef_msg),
89 # only install to_Type coercion handler if type can coerce
90 # or if we want to provide them anyway, e.g. declarations
91 if ($args{ -full } or $tobj->has_coercion) {
93 # install to_Type coercion handler
95 code => MooseX::TypeLibrary->coercion_export_generator(
96 $type, $full, $undef_msg ),
107 This returns a type from the library's store by its name.
112 my ($class, $type) = @_;
114 # useful message if the type couldn't be found
115 croak "Unknown type '$type' in library '$class'"
116 unless $class->has_type($type);
118 # return real name of the type
119 return $class->type_storage->{ $type };
124 Returns a list of all known types by their name.
131 # return short names of all stored types
132 return keys %{ $class->type_storage };
137 Adds a new type to the library.
142 my ($class, $type) = @_;
144 # store type with library prefix as real name
145 $class->type_storage->{ $type } = "${class}::${type}";
150 Returns true or false depending on if this library knows a type by that
156 my ($class, $type) = @_;
158 # check if we stored a type under that name
159 return ! ! $class->type_storage->{ $type };
164 Returns the library's type storage hash reference. You shouldn't use this
165 method directly unless you know what you are doing. It is not an internal
166 method because overriding it makes virtual libraries very easy.
173 # return a reference to the storage in ourself
175 return \%{ $class . '::__MOOSEX_TYPELIBRARY_STORAGE' };
181 L<MooseX::TypeLibrary::Moose>
183 =head1 AUTHOR AND COPYRIGHT
185 Robert 'phaylon' Sedlacek C<E<lt>rs@474.atE<gt>>, with many thanks to
186 the C<#moose> cabal on C<irc.perl.org>.
190 This program is free software; you can redistribute it and/or modify
191 it under the same terms as perl itself.