Types/Base.pm

filter: Statementstmt 100% 98 / 98 Branchbran 75.0% 18 / 24 Conditioncond 92.3% 12 / 13 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 100% 22 / 22 Podpod 100% 11 / 11 totaltotal 95.4% 167 / 175 CC 20 SCAR 20.5
CC
20
Cov
95%
import
17.0
get_type
2.0
register_class_type
2.0
CRAP 7.8
↑ Summary Types.pm »
1 package MooseX::Types::Base;
2 # ABSTRACT: Type library base class
3  
4 17 our $VERSION = '0.51';
5  
6 17 ▶use Carp::Clan qw( ^MooseX::Types );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Sub::Exporter qw( build_exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use Moose::Util::TypeConstraints qw( find_type_constraint );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 17 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 #pod =head1 DESCRIPTION
13 #pod
14 #pod You normally won't need to interact with this class by yourself. It is
15 #pod merely a collection of functionality that type libraries need to
16 #pod interact with moose and the rest of the L<MooseX::Types> module.
17 #pod
18 #pod =cut
19  
20 17 my $UndefMsg = q{Unable to find type '%s' in library '%s'};
21  
22 #pod =head1 METHODS
23 #pod
24 #pod =cut
25  
26 #pod =head2 import
27 #pod
28 #pod Provides the import mechanism for your library. See
29 #pod L<MooseX::Types/"LIBRARY USAGE"> for syntax details on this.
30 #pod
31 #pod =cut
32  
33 54 ▶sub import {
Subroutine import: called CC=17 SCAR=28.4
34 54 my ($class, @args) = @_;
35  
36 # filter or create options hash for S:E
37 54 ▶ my $options = (@args and (ref($args[0]) eq 'HASH')) ? $args[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@args && ref $args[0] eq 'HASH' ? : 23 31 54
Condition66%2/3
@args and ref $args[0] eq "HASH"
!ll&&!rl&&r
03123
MC/DC50%1/2
@args and ref $args[0] eq "HASH"
@args ref $args[0] eq "HASH"
Truth table66%2/3
A and B
A= @argsB= ref $args[0] eq "HASH"
ABresult
0X0
100
111
38  
39 # preserve additional options, to ensure types are installed into the type library's namespace
40 54 ▶ my %ex_spec = %{ $options || {} };
Condition100%2/2
$options || {}
l!l
2331
MC/DC100%1/1
$options || {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
41 54 delete @ex_spec{ qw(-wrapper -into -full) };
42  
43 54 ▶ unless ($options) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options) 31 23 54
44 31 $options = {};
45 31 unshift @args, $options;
46 }
47  
48 # all types known to us
49 54 my @types = $class->type_names;
50  
51 # determine the wrapper, -into is supported for compatibility reasons
52 54 ▶ my $wrapper = $options->{ -wrapper } || 'MooseX::Types';
Condition100%2/2
$options->{'-wrapper'} || 'MooseX::Types'
l!l
252
MC/DC100%1/1
$options->{'-wrapper'} || 'MooseX::Types'
$options->{'-wrapper'}
Truth table100%2/2
A
A= $options->{'-wrapper'}
Aresult
11
00
53  
54 $args[0]->{into} = $options->{ -into }
55 54 ▶ if exists $options->{ -into };
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"-into"} 23 31 54
56  
57 54 my %ex_util;
58  
59 TYPE:
60 54 for my $type_short (@types) {
61  
62 # find type name and object, create undefined message
63 607 ▶ my $type_full = $class->get_type($type_short)
Branch: 1 branch
BranchTrueFalseTotal
my $type_full = $class->get_type($type_short) or croak("No fully qualified type name stored for '${type_short}'") 607 0 607
64 or croak "No fully qualified type name stored for '$type_short'";
65 607 my $type_cons = find_type_constraint($type_full);
66 607 my $undef_msg = sprintf($UndefMsg, $type_short, $class);
67  
68 # the type itself
69 607 push @{ $ex_spec{exports} },
70 $type_short,
71 149 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 149 bless $wrapper->type_export_generator($type_short, $type_full),
73 'MooseX::Types::EXPORTED_TYPE_CONSTRAINT';
74 607 };
75  
76 # the check helper
77 607 my $check_name = "is_${type_short}";
78 607 push @{ $ex_spec{exports} },
79 $check_name,
80 607 ▶ sub { $wrapper->check_export_generator($type_short, $type_full, $undef_msg) };
Subroutine __ANON__: called CC=1 SCAR=0.0
81  
82 # only export coercion helper if full (for libraries) or coercion is defined
83 next TYPE
84 unless $options->{ -full }
85 607 ▶ or ($type_cons and $type_cons->has_coercion);
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"-full"} or $type_cons and $type_cons->has_coercion 535 72 607
Condition100%6/6
$options->{"-full"} or $type_cons and $type_cons->has_coercion
l!l&&r!l&&!r
5616535
$type_cons and $type_cons->has_coercion
!ll&&!rl&&r
253316
MC/DC100%3/3
$options->{"-full"} or $type_cons and $type_cons->has_coercion
$options->{"-full"} $type_cons $type_cons->has_coercion
Truth table100%4/4
A or B and C
A= $options->{"-full"}B= $type_consC= $type_cons->has_coercion
ABCresult
1XX1
0111
00X0
0100
86  
87 72 my $coercion_name = "to_${type_short}";
88 72 push @{ $ex_spec{exports} },
89 $coercion_name,
90 72 ▶ sub { $wrapper->coercion_export_generator($type_short, $type_full, $undef_msg) };
Subroutine __ANON__: called CC=1 SCAR=0.0
91 72 $ex_util{ $type_short }{to}++; # shortcut to remember this exists
92 }
93  
94 # create S:E exporter and increase export level unless specified explicitly
95 54 my $exporter = build_exporter \%ex_spec;
96 $options->{into_level}++
97 54 ▶ unless $options->{into};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"into"} 31 23 54
98  
99 # remember requested symbols to determine what helpers to auto-export
100 my %was_requested =
101 129 map { ($_ => 1) }
102 54 grep { not ref }
103 @args;
104  
105 # determine which additional symbols (helpers) to export along
106 54 my %add;
107 EXPORT:
108 54 for my $type (grep { exists $was_requested{ $_ } } @types) {
109 $add{ "is_$type" }++
110 127 ▶ unless $was_requested{ "is_$type" };
Branch: 1 branch
BranchTrueFalseTotal
unless $was_requested{"is_$type"} 127 0 127
111 next EXPORT
112 127 ▶ unless exists $ex_util{ $type }{to};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $ex_util{$type}{"to"} 60 67 127
113 $add{ "to_$type" }++
114 67 ▶ unless $was_requested{ "to_$type" };
Branch: 1 branch
BranchTrueFalseTotal
unless $was_requested{"to_$type"} 67 0 67
115 }
116  
117 # and on to the real exporter
118 54 my @new_args = (@args, keys %add);
119 54 return $class->$exporter(@new_args);
120 }
121  
122 #pod =head2 get_type
123 #pod
124 #pod This returns a type from the library's store by its name.
125 #pod
126 #pod =cut
127  
128 607 ▶sub get_type {
Subroutine get_type: called CC=2 SCAR=7.1
Pod documented
129 607 my ($class, $type) = @_;
130  
131 # useful message if the type couldn't be found
132 607 ▶ croak "Unknown type '$type' in library '$class'"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->has_type($type) 0 607 607
133 unless $class->has_type($type);
134  
135 # return real name of the type
136 607 return $class->type_storage->{ $type };
137 }
138  
139 #pod =head2 type_names
140 #pod
141 #pod Returns a list of all known types by their name.
142 #pod
143 #pod =cut
144  
145 57 ▶sub type_names {
Subroutine type_names: called CC=1 SCAR=0.0
Pod documented
146 57 my ($class) = @_;
147  
148 # return short names of all stored types
149 57 return keys %{ $class->type_storage };
150 }
151  
152 #pod =head2 add_type
153 #pod
154 #pod Adds a new type to the library.
155 #pod
156 #pod =cut
157  
158 56 ▶sub add_type {
Subroutine add_type: called CC=1 SCAR=0.0
Pod documented
159 56 my ($class, $type) = @_;
160  
161 # store type with library prefix as real name
162 56 $class->type_storage->{ $type } = "${class}::${type}";
163 }
164  
165 #pod =head2 has_type
166 #pod
167 #pod Returns true or false depending on if this library knows a type by that
168 #pod name.
169 #pod
170 #pod =cut
171  
172 607 ▶sub has_type {
Subroutine has_type: called CC=1 SCAR=0.0
Pod documented
173 607 my ($class, $type) = @_;
174  
175 # check if we stored a type under that name
176 607 return ! ! $class->type_storage->{ $type };
177 }
178  
179 #pod =head2 type_storage
180 #pod
181 #pod Returns the library's type storage hash reference. You shouldn't use this
182 #pod method directly unless you know what you are doing. It is not an internal
183 #pod method because overriding it makes virtual libraries very easy.
184 #pod
185 #pod =cut
186  
187 294 ▶sub type_storage {
Subroutine type_storage: called CC=1 SCAR=0.0
Pod documented
188 294 my ($class) = @_;
189  
190 # return a reference to the storage in ourself
191 17 ▶ { no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
192 294 return \%{ $class . '::__MOOSEX_TYPELIBRARY_STORAGE' };
193 }
194 }
195  
196 #pod =head2 registered_class_types
197 #pod
198 #pod Returns the class types registered within this library. Don't use directly.
199 #pod
200 #pod =cut
201  
202 6 ▶sub registered_class_types {
Subroutine registered_class_types: called CC=1 SCAR=0.0
Pod documented
203 6 my ($class) = @_;
204  
205 {
206 17 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
207 6 return \%{ $class . '::__MOOSEX_TYPELIBRARY_CLASS_TYPES' };
208 }
209 }
210  
211 #pod =head2 register_class_type
212 #pod
213 #pod Register a C<class_type> for use in this library by class name.
214 #pod
215 #pod =cut
216  
217 2 ▶sub register_class_type {
Subroutine register_class_type: called CC=2 SCAR=7.1
Pod documented
218 2 my ($class, $type) = @_;
219  
220 2 ▶ croak "Not a class_type"
Branch: 1 branch
BranchTrueFalseTotal
unless $type->isa("Moose::Meta::TypeConstraint::Class") 0 2 2
221 unless $type->isa('Moose::Meta::TypeConstraint::Class');
222  
223 2 $class->registered_class_types->{$type->class} = $type;
224 }
225  
226 #pod =head2 get_registered_class_type
227 #pod
228 #pod Get a C<class_type> registered in this library by name.
229 #pod
230 #pod =cut
231  
232 4 ▶sub get_registered_class_type {
Subroutine get_registered_class_type: called CC=1 SCAR=0.0
Pod documented
233 4 my ($class, $name) = @_;
234  
235 4 $class->registered_class_types->{$name};
236 }
237  
238 #pod =head2 registered_role_types
239 #pod
240 #pod Returns the role types registered within this library. Don't use directly.
241 #pod
242 #pod =cut
243  
244 3 ▶sub registered_role_types {
Subroutine registered_role_types: called CC=1 SCAR=0.0
Pod documented
245 3 my ($class) = @_;
246  
247 {
248 17 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
249 3 return \%{ $class . '::__MOOSEX_TYPELIBRARY_ROLE_TYPES' };
250 }
251 }
252  
253 #pod =head2 register_role_type
254 #pod
255 #pod Register a C<role_type> for use in this library by role name.
256 #pod
257 #pod =cut
258  
259 1 ▶sub register_role_type {
Subroutine register_role_type: called CC=2 SCAR=7.1
Pod documented
260 1 my ($class, $type) = @_;
261  
262 1 ▶ croak "Not a role_type"
Branch: 1 branch
BranchTrueFalseTotal
unless $type->isa("Moose::Meta::TypeConstraint::Role") 0 1 1
263 unless $type->isa('Moose::Meta::TypeConstraint::Role');
264  
265 1 $class->registered_role_types->{$type->role} = $type;
266 }
267  
268 #pod =head2 get_registered_role_type
269 #pod
270 #pod Get a C<role_type> registered in this library by role name.
271 #pod
272 #pod =cut
273  
274 2 ▶sub get_registered_role_type {
Subroutine get_registered_role_type: called CC=1 SCAR=0.0
Pod documented
275 2 my ($class, $name) = @_;
276  
277 2 $class->registered_role_types->{$name};
278 }
279  
280 #pod =head1 SEE ALSO
281 #pod
282 #pod L<MooseX::Types::Moose>
283 #pod
284 #pod =cut
285  
286 17 1;
287  
288 __END__
↑ Summary Types.pm »