v0.29
[gitmo/MooseX-Types.git] / lib / MooseX / Types / Combine.pm
CommitLineData
ca9d7442 1package MooseX::Types::Combine;
ef8b7b7a 2
3# ABSTRACT: Combine type libraries for exporting
ca9d7442 4
5use strict;
6use warnings;
7use Class::MOP ();
8
9=head1 SYNOPSIS
10
11 package CombinedTypeLib;
12
e9c85115 13 use base 'MooseX::Types::Combine';
ca9d7442 14
15 __PACKAGE__->provide_types_from(qw/TypeLib1 TypeLib2/);
16
17 package UserClass;
18
19 use CombinedTypeLib qw/Type1 Type2 ... /;
20
21=head1 DESCRIPTION
22
23Allows you to export types from multiple type libraries.
24
25Libraries on the right side of the type libs passed to L</provide_types_from>
26take precedence over those on the left in case of conflicts.
27
28=cut
29
30sub import {
31 my ($class, @types) = @_;
32 my $caller = caller;
33
587387ef 34 my %types = $class->_provided_types;
ca9d7442 35
36 my %from;
bd47cf64 37 for my $type (@types) {
587387ef 38 unless ($types{$type}) {
39 my @type_libs = $class->provide_types_from;
40
41 die
42 "$caller asked for a type ($type) which is not found in any of the"
43 . " type libraries (@type_libs) combined by $class\n";
44 }
bd47cf64 45
46 push @{ $from{ $types{$type} } }, $type;
47 }
ca9d7442 48
49 $_->import({ -into => $caller }, @{ $from{ $_ } })
50 for keys %from;
51}
52
53=head1 CLASS METHODS
54
55=head2 provide_types_from
56
57Sets or returns a list of type libraries to re-export from.
58
59=cut
60
61sub provide_types_from {
62 my ($class, @libs) = @_;
63
64 my $store =
65 do { no strict 'refs'; \@{ "${class}::__MOOSEX_TYPELIBRARY_LIBRARIES" } };
66
587387ef 67 if (@libs) {
68 $class->_check_type_lib($_) for @libs;
69 @$store = @libs;
70
71 my %types = map {
72 my $lib = $_;
73 map +( $_ => $lib ), $lib->type_names
74 } @libs;
75
76 $class->_provided_types(%types);
77 }
ca9d7442 78
79 @$store;
80}
81
1808a2ae 82sub _check_type_lib {
83 my ($class, $lib) = @_;
84
85 Class::MOP::load_class($lib);
86
87 die "Cannot use $lib in a combined type library, it does not provide any types"
88 unless $lib->can('type_names');
89}
90
587387ef 91sub _provided_types {
92 my ($class, %types) = @_;
93
94 my $types =
95 do { no strict 'refs'; \%{ "${class}::__MOOSEX_TYPELIBRARY_TYPES" } };
96
97 %$types = %types
98 if keys %types;
99
100 %$types;
101}
102
ca9d7442 103=head1 SEE ALSO
104
105L<MooseX::Types>
106
107=head1 LICENSE
108
109This program is free software; you can redistribute it and/or modify
110it under the same terms as perl itself.
111
112=cut
113
1141;