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