line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooseX::Types::LoadableClass; |
2
|
|
|
|
|
|
|
# git description: v0.012-13-gcdaf68a |
3
|
|
|
|
|
|
|
$MooseX::Types::LoadableClass::VERSION = '0.013'; |
4
|
|
|
|
|
|
|
# ABSTRACT: ClassName type constraint with coercion to load the class. |
5
|
|
|
|
|
|
|
# KEYWORDS: moose types constraints class classes role roles module modules |
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
74579
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
99
|
|
8
|
3
|
|
|
3
|
|
12
|
use warnings; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
85
|
|
9
|
3
|
|
|
3
|
|
2727
|
use MooseX::Types -declare => [qw/ ClassName LoadableClass LoadableRole /]; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use MooseX::Types::Moose qw(Str RoleName), ClassName => { -as => 'MooseClassName' }; |
11
|
|
|
|
|
|
|
use Moose::Util::TypeConstraints; |
12
|
|
|
|
|
|
|
use Module::Runtime qw(is_module_name use_package_optimistically); |
13
|
|
|
|
|
|
|
use if MooseX::Types->VERSION >= 0.42, 'namespace::autoclean'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
subtype LoadableClass, |
16
|
|
|
|
|
|
|
as Str, |
17
|
|
|
|
|
|
|
where { |
18
|
|
|
|
|
|
|
is_module_name($_) |
19
|
|
|
|
|
|
|
and use_package_optimistically($_) |
20
|
|
|
|
|
|
|
and MooseClassName->check($_) |
21
|
|
|
|
|
|
|
}; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
subtype LoadableRole, |
24
|
|
|
|
|
|
|
as Str, |
25
|
|
|
|
|
|
|
where { |
26
|
|
|
|
|
|
|
is_module_name($_) |
27
|
|
|
|
|
|
|
and use_package_optimistically($_) |
28
|
|
|
|
|
|
|
and RoleName->check($_) |
29
|
|
|
|
|
|
|
}; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# back compat |
33
|
|
|
|
|
|
|
coerce LoadableClass, from Str, via { $_ }; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
coerce LoadableRole, from Str, via { $_ }; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
__PACKAGE__->type_storage->{ClassName} |
38
|
|
|
|
|
|
|
= __PACKAGE__->type_storage->{LoadableClass}; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
41
|
|
|
|
|
|
|
1; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
__END__ |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=pod |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=encoding UTF-8 |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head1 NAME |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
MooseX::Types::LoadableClass - ClassName type constraint with coercion to load the class. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 VERSION |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
version 0.013 |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 SYNOPSIS |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
package MyClass; |
60
|
|
|
|
|
|
|
use Moose; |
61
|
|
|
|
|
|
|
use MooseX::Types::LoadableClass qw/ LoadableClass /; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
has foobar_class => ( |
64
|
|
|
|
|
|
|
is => 'ro', |
65
|
|
|
|
|
|
|
required => 1, |
66
|
|
|
|
|
|
|
isa => LoadableClass, |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
MyClass->new(foobar_class => 'FooBar'); # FooBar.pm is loaded or an |
70
|
|
|
|
|
|
|
# exception is thrown. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 DESCRIPTION |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
use Moose::Util::TypeConstraints; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $tc = subtype as ClassName; |
77
|
|
|
|
|
|
|
coerce $tc, from Str, via { Class::Load::load_class($_); $_ }; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
I've written those three lines of code quite a lot of times, in quite |
80
|
|
|
|
|
|
|
a lot of places. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Now I don't have to. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=for stopwords ClassName |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 TYPES EXPORTED |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head2 C<LoadableClass> |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
A normal class / package. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head2 C<LoadableRole> |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Like C<LoadableClass>, except the loaded package must be a L<Moose::Role>. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 AUTHOR |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Tomas Doran <bobtfish@bobtfish.net> |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
This software is copyright (c) 2010 by Infinity Interactive, Inc. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
105
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 CONTRIBUTORS |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=for stopwords Karen Etheridge Dagfinn Ilmari MannsÃ¥ker Florian Ragwitz СеÑгей Романов |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=over 4 |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=item * |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Karen Etheridge <ether@cpan.org> |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=item * |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Dagfinn Ilmari Mannsåker <ilmari@ilmari.org> |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=item * |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Florian Ragwitz <rafl@debian.org> |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=item * |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
СеÑгей Романов <sromanov@cpan.org> |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=back |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=cut |