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