| 1 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | package MooseX::Types::Combine; | ||||||||||
| 4 | # ABSTRACT: Combine type libraries for exporting | ||||||||||
| 5 | |||||||||||
| 6 | 1 | our $VERSION = '0.51'; | |||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use Module::Runtime 'use_module'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 create a single class that will allow you to export types from | ||||||||||
| 26 | #pod multiple type libraries: | ||||||||||
| 27 | #pod | ||||||||||
| 28 | #pod package TransportTypes; | ||||||||||
| 29 | #pod | ||||||||||
| 30 | #pod use base 'MooseX::Types::Combine'; | ||||||||||
| 31 | #pod | ||||||||||
| 32 | #pod __PACKAGE__->provide_types_from(qw/ MotorizedTypes UnmotorizedTypes /); | ||||||||||
| 33 | #pod | ||||||||||
| 34 | #pod 1; | ||||||||||
| 35 | #pod | ||||||||||
| 36 | #pod In this example all types defined in C<MotorizedTypes> and C<UnmotorizedTypes> | ||||||||||
| 37 | #pod are available through the C<TransportTypes> combined type library. | ||||||||||
| 38 | #pod | ||||||||||
| 39 | #pod package SkiingTrip; | ||||||||||
| 40 | #pod | ||||||||||
| 41 | #pod use Moose; | ||||||||||
| 42 | #pod | ||||||||||
| 43 | #pod use TransportTypes qw( CarType SkisType ); | ||||||||||
| 44 | #pod | ||||||||||
| 45 | #pod has car => ( is => 'ro', isa => CarType, required => 1 ); | ||||||||||
| 46 | #pod has ski_rack => ( is => 'ro', isa => ArrayRef[SkisType], required => 1 ); | ||||||||||
| 47 | #pod ... | ||||||||||
| 48 | #pod | ||||||||||
| 49 | #pod Libraries on the right end of the list passed to L</provide_types_from> take | ||||||||||
| 50 | #pod precedence over those on the left in case of conflicts. So, in the above | ||||||||||
| 51 | #pod example if both the C<MotorizedTypes> and C<UnmotorizedTypes> libraries provided | ||||||||||
| 52 | #pod a C<Bike> type, you'd get the bicycle from C<UnmotorizedTypes> not the | ||||||||||
| 53 | #pod motorbike from C<MorotizedTypes>. | ||||||||||
| 54 | #pod | ||||||||||
| 55 | #pod You can also further combine combined type libraries with additional type | ||||||||||
| 56 | #pod libraries or other combined type libraries in the same way to provide even | ||||||||||
| 57 | #pod larger type libraries: | ||||||||||
| 58 | #pod | ||||||||||
| 59 | #pod package MeetingTransportTypes; | ||||||||||
| 60 | #pod | ||||||||||
| 61 | #pod use base 'MooseX::Types::Combine'; | ||||||||||
| 62 | #pod | ||||||||||
| 63 | #pod __PACKAGE__->provide_types_from(qw/ TransportTypes TelepresenceTypes /); | ||||||||||
| 64 | #pod | ||||||||||
| 65 | #pod 1; | ||||||||||
| 66 | #pod | ||||||||||
| 67 | #pod =cut | ||||||||||
| 68 | |||||||||||
| 69 | 2 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=8 SCAR=22.0
| |||||||||||
| 70 | 2 | my ($class, @types) = @_; | |||||||||
| 71 | 2 | my $caller = caller; | |||||||||
| 72 | |||||||||||
| 73 | 2 | my $where_to_import_to = $caller; | |||||||||
| 74 | 2 | ▶ | if (ref $types[0] eq 'HASH') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 1 | my $extra = shift @types; | |||||||||
| 76 | 1 | ▶ | $where_to_import_to = $extra->{-into} if exists $extra->{-into}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 2 | my %types = $class->_provided_types; | |||||||||
| 80 | |||||||||||
| 81 | 2 | ▶ | if ( grep { $_ eq ':all' } @types ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | $_->import( { -into => $where_to_import_to }, q{:all} ) | ||||||||||
| 83 | 0 | for $class->provide_types_from; | |||||||||
| 84 | 0 | return; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 2 | my %from; | |||||||||
| 88 | 2 | for my $type (@types) { | |||||||||
| 89 | 4 | ▶ | unless ($types{$type}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 0 | my @type_libs = $class->provide_types_from; | |||||||||
| 91 | |||||||||||
| 92 | 0 | die | |||||||||
| 93 | "$caller asked for a type ($type) which is not found in any of the" | ||||||||||
| 94 | . " type libraries (@type_libs) combined by $class\n"; | ||||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 4 | push @{ $from{ $types{$type} } }, $type; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 3 | $_->import({ -into => $where_to_import_to }, @{ $from{ $_ } }) | |||||||||
| 101 | 2 | for keys %from; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | #pod =head1 CLASS METHODS | ||||||||||
| 105 | #pod | ||||||||||
| 106 | #pod =head2 provide_types_from | ||||||||||
| 107 | #pod | ||||||||||
| 108 | #pod Sets or returns a list of type libraries (or combined type libraries) to | ||||||||||
| 109 | #pod re-export from. | ||||||||||
| 110 | #pod | ||||||||||
| 111 | #pod =cut | ||||||||||
| 112 | |||||||||||
| 113 | 2 | ▶ | sub provide_types_from { | ||||||||
|
Subroutine
provide_types_from: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 114 | 2 | my ($class, @libs) = @_; | |||||||||
| 115 | |||||||||||
| 116 | my $store = | ||||||||||
| 117 | 1 | ▶ | do { no strict 'refs'; \@{ "${class}::__MOOSEX_TYPELIBRARY_LIBRARIES" } }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 118 | |||||||||||
| 119 | 2 | ▶ | if (@libs) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 2 | $class->_check_type_lib($_) for @libs; | |||||||||
| 121 | 2 | @$store = @libs; | |||||||||
| 122 | |||||||||||
| 123 | my %types = map { | ||||||||||
| 124 | 2 | my $lib = $_; | |||||||||
| 125 | 3 | map +( $_ => $lib ), $lib->type_names | |||||||||
| 126 | } @libs; | ||||||||||
| 127 | |||||||||||
| 128 | 2 | $class->_provided_types(%types); | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | 2 | @$store; | |||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | 3 | ▶ | sub _check_type_lib { | ||||||||
|
Subroutine
_check_type_lib: called CC=2 SCAR=7.1
| |||||||||||
| 135 | 3 | my ($class, $lib) = @_; | |||||||||
| 136 | |||||||||||
| 137 | 3 | use_module($lib); | |||||||||
| 138 | |||||||||||
| 139 | 3 | ▶ | die "Cannot use $lib in a combined type library, it does not provide any types" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | unless $lib->can('type_names'); | ||||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 5 | ▶ | sub _provided_types { | ||||||||
|
Subroutine
_provided_types: called CC=2 SCAR=6.9
| |||||||||||
| 144 | 5 | my ($class, %types) = @_; | |||||||||
| 145 | |||||||||||
| 146 | my $types = | ||||||||||
| 147 | 1 | ▶ | do { no strict 'refs'; \%{ "${class}::__MOOSEX_TYPELIBRARY_TYPES" } }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 148 | |||||||||||
| 149 | 5 | ▶ | %$types = %types | ||||||||
|
Branch: 1 branch
| |||||||||||
| 150 | if keys %types; | ||||||||||
| 151 | |||||||||||
| 152 | 5 | %$types; | |||||||||
| 153 | } | ||||||||||
| 154 | |||||||||||
| 155 | #pod =head2 type_names | ||||||||||
| 156 | #pod | ||||||||||
| 157 | #pod Returns a list of all known types by their name. | ||||||||||
| 158 | #pod | ||||||||||
| 159 | #pod =cut | ||||||||||
| 160 | |||||||||||
| 161 | 1 | ▶ | sub type_names { | ||||||||
|
Subroutine
type_names: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 162 | 1 | my ($class) = @_; | |||||||||
| 163 | |||||||||||
| 164 | 1 | my %types = $class->_provided_types(); | |||||||||
| 165 | 1 | return keys %types; | |||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | #pod =head1 SEE ALSO | ||||||||||
| 169 | #pod | ||||||||||
| 170 | #pod L<MooseX::Types> | ||||||||||
| 171 | #pod | ||||||||||
| 172 | #pod =cut | ||||||||||
| 173 | |||||||||||
| 174 | 1 | 1; | |||||||||
| 175 | |||||||||||
| 176 | __END__ | ||||||||||