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