More changes for NEXT
[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
587387ef 39 my %types = $class->_provided_types;
ca9d7442 40
41 my %from;
bd47cf64 42 for my $type (@types) {
587387ef 43 unless ($types{$type}) {
44 my @type_libs = $class->provide_types_from;
45
46 die
47 "$caller asked for a type ($type) which is not found in any of the"
48 . " type libraries (@type_libs) combined by $class\n";
49 }
bd47cf64 50
51 push @{ $from{ $types{$type} } }, $type;
52 }
ca9d7442 53
54 $_->import({ -into => $caller }, @{ $from{ $_ } })
55 for keys %from;
56}
57
58=head1 CLASS METHODS
59
60=head2 provide_types_from
61
62Sets or returns a list of type libraries to re-export from.
63
64=cut
65
66sub provide_types_from {
67 my ($class, @libs) = @_;
68
69 my $store =
70 do { no strict 'refs'; \@{ "${class}::__MOOSEX_TYPELIBRARY_LIBRARIES" } };
71
587387ef 72 if (@libs) {
73 $class->_check_type_lib($_) for @libs;
74 @$store = @libs;
75
76 my %types = map {
77 my $lib = $_;
78 map +( $_ => $lib ), $lib->type_names
79 } @libs;
80
81 $class->_provided_types(%types);
82 }
ca9d7442 83
84 @$store;
85}
86
587387ef 87sub _provided_types {
88 my ($class, %types) = @_;
89
90 my $types =
91 do { no strict 'refs'; \%{ "${class}::__MOOSEX_TYPELIBRARY_TYPES" } };
92
93 %$types = %types
94 if keys %types;
95
96 %$types;
97}
98
99sub _check_type_lib {
100 my ($class, $lib) = @_;
101
102 Class::MOP::load_class($lib);
103
104 die "Cannot use $lib in a combined type library, it does not provide any types"
105 unless $lib->can('type_names');
106}
107
ca9d7442 108=head1 SEE ALSO
109
110L<MooseX::Types>
111
b55332a8 112=head1 AUTHOR
113
114See L<MooseX::Types/AUTHOR>.
115
ca9d7442 116=head1 LICENSE
117
118This program is free software; you can redistribute it and/or modify
119it under the same terms as perl itself.
120
121=cut
122
1231;