File Coverage

blib/lib/MooseX/Types/LoadableClass.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 24 24 100.0


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