File Coverage

blib/lib/MooseX/Types/Combine.pm
Criterion Covered Total %
statement 55 55 100.0
branch 10 10 100.0
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 76 76 100.0


line stmt bran cond sub pod time code
1 1     1   26297 use strict;
  1         2  
  1         24  
2 1     1   5 use warnings;
  1         1  
  1         51  
3             package MooseX::Types::Combine;
4             # ABSTRACT: Combine type libraries for exporting
5              
6             our $VERSION = '0.46';
7              
8 1     1   681 use Module::Runtime 'use_module';
  1         1589  
  1         6  
9 1     1   650 use namespace::autoclean;
  1         14138  
  1         4  
10              
11             #pod =head1 SYNOPSIS
12             #pod
13             #pod package CombinedTypeLib;
14             #pod
15             #pod use base 'MooseX::Types::Combine';
16             #pod
17             #pod __PACKAGE__->provide_types_from(qw/TypeLib1 TypeLib2/);
18             #pod
19             #pod package UserClass;
20             #pod
21             #pod use CombinedTypeLib qw/Type1 Type2 ... /;
22             #pod
23             #pod =head1 DESCRIPTION
24             #pod
25             #pod Allows you to export types from multiple type libraries.
26             #pod
27             #pod Libraries on the right end of the list passed to L</provide_types_from>
28             #pod take precedence over those on the left in case of conflicts.
29             #pod
30             #pod =cut
31              
32             sub import {
33 3     3   1764 my ($class, @types) = @_;
34 3         7 my $caller = caller;
35              
36 3         10 my %types = $class->_provided_types;
37              
38 3 100       9 if ( grep { $_ eq ':all' } @types ) {
  5         15  
39             $_->import( { -into => $caller }, q{:all} )
40 1         4 for $class->provide_types_from;
41 1         401 return;
42             }
43              
44 2         4 my %from;
45 2         3 for my $type (@types) {
46 4 100       13 unless ($types{$type}) {
47 1         6 my @type_libs = $class->provide_types_from;
48              
49 1         16 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             }
53              
54 3         7 push @{ $from{ $types{$type} } }, $type;
  3         9  
55             }
56              
57 2         202 $_->import({ -into => $caller }, @{ $from{ $_ } })
58 1         7 for keys %from;
59             }
60              
61             #pod =head1 CLASS METHODS
62             #pod
63             #pod =head2 provide_types_from
64             #pod
65             #pod Sets or returns a list of type libraries to re-export from.
66             #pod
67             #pod =cut
68              
69             sub provide_types_from {
70 5     5 1 1147 my ($class, @libs) = @_;
71              
72             my $store =
73 1     1   298 do { no strict 'refs'; \@{ "${class}::__MOOSEX_TYPELIBRARY_LIBRARIES" } };
  1         2  
  1         197  
  5         8  
  5         8  
  5         23  
74              
75 5 100       14 if (@libs) {
76 3         16 $class->_check_type_lib($_) for @libs;
77 1         4 @$store = @libs;
78              
79             my %types = map {
80 1         3 my $lib = $_;
  2         2  
81 2         8 map +( $_ => $lib ), $lib->type_names
82             } @libs;
83              
84 1         12 $class->_provided_types(%types);
85             }
86              
87 3         25 @$store;
88             }
89              
90             sub _check_type_lib {
91 4     4   8 my ($class, $lib) = @_;
92              
93 4         25 use_module($lib);
94              
95 3 100       2138 die "Cannot use $lib in a combined type library, it does not provide any types"
96             unless $lib->can('type_names');
97             }
98              
99             sub _provided_types {
100 4     4   9 my ($class, %types) = @_;
101              
102             my $types =
103 1     1   5 do { no strict 'refs'; \%{ "${class}::__MOOSEX_TYPELIBRARY_TYPES" } };
  1         2  
  1         66  
  4         5  
  4         5  
  4         16  
104              
105 4 100       40 %$types = %types
106             if keys %types;
107              
108 4         23 %$types;
109             }
110              
111             #pod =head1 SEE ALSO
112             #pod
113             #pod L<MooseX::Types>
114             #pod
115             #pod =cut
116              
117             1;
118              
119             __END__
120              
121             =pod
122              
123             =encoding UTF-8
124              
125             =head1 NAME
126              
127             MooseX::Types::Combine - Combine type libraries for exporting
128              
129             =head1 VERSION
130              
131             version 0.46
132              
133             =head1 SYNOPSIS
134              
135             package CombinedTypeLib;
136              
137             use base 'MooseX::Types::Combine';
138              
139             __PACKAGE__->provide_types_from(qw/TypeLib1 TypeLib2/);
140              
141             package UserClass;
142              
143             use CombinedTypeLib qw/Type1 Type2 ... /;
144              
145             =head1 DESCRIPTION
146              
147             Allows you to export types from multiple type libraries.
148              
149             Libraries on the right end of the list passed to L</provide_types_from>
150             take precedence over those on the left in case of conflicts.
151              
152             =head1 CLASS METHODS
153              
154             =head2 provide_types_from
155              
156             Sets or returns a list of type libraries to re-export from.
157              
158             =head1 SEE ALSO
159              
160             L<MooseX::Types>
161              
162             =head1 AUTHOR
163              
164             Robert "phaylon" Sedlacek <rs@474.at>
165              
166             =head1 COPYRIGHT AND LICENSE
167              
168             This software is copyright (c) 2007 by Robert "phaylon" Sedlacek.
169              
170             This is free software; you can redistribute it and/or modify it under
171             the same terms as the Perl 5 programming language system itself.
172              
173             =cut