add $VERSION to all modules so that people can install MooseX::Types::Moose without...
[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 our $VERSION = "0.20";
9
10 use strict;
11 use warnings;
12 use Class::MOP ();
13
14 =head1 SYNOPSIS
15
16     package CombinedTypeLib;
17
18     use base 'MooseX::Types::Combine';
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
28 Allows you to export types from multiple type libraries. 
29
30 Libraries on the right side of the type libs passed to L</provide_types_from>
31 take precedence over those on the left in case of conflicts.
32
33 =cut
34
35 sub 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;
48     push @{ $from{ $types{ $_ } } }, $_ for @types;
49
50     $_->import({ -into => $caller }, @{ $from{ $_ } })
51         for keys %from;
52 }
53
54 =head1 CLASS METHODS
55
56 =head2 provide_types_from
57
58 Sets or returns a list of type libraries to re-export from.
59
60 =cut
61
62 sub provide_types_from {
63     my ($class, @libs) = @_;
64
65     my $store =
66      do { no strict 'refs'; \@{ "${class}::__MOOSEX_TYPELIBRARY_LIBRARIES" } };
67
68     @$store = @libs if @libs;
69
70     @$store;
71 }
72
73 =head1 SEE ALSO
74
75 L<MooseX::Types>
76
77 =head1 AUTHOR
78
79 See L<MooseX::Types/AUTHOR>.
80
81 =head1 LICENSE
82
83 This program is free software; you can redistribute it and/or modify
84 it under the same terms as perl itself.
85
86 =cut
87
88 1;