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.015-8-g23151a4
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.016';
6              
7 6     6   1231166 use strict;
  6         11  
  6         202  
8 6     6   23 use warnings;
  6         8  
  6         312  
9 6     6   3227 use MooseX::Types -declare => [qw/ LoadableClass LoadableRole /];
  6         1724896  
  6         41  
10 6     6   36092 use MooseX::Types::Moose qw(Str RoleName), ClassName => { -as => 'MooseClassName' };
  6         33497  
  6         56  
11 6     6   36605 use Module::Runtime qw(is_module_name use_package_optimistically);
  6         13  
  6         52  
12 6     6   508 use if MooseX::Types->VERSION >= 0.42, 'namespace::autoclean';
  6         15  
  6         345  
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             1;
40              
41             __END__
42              
43             =pod
44              
45             =encoding UTF-8
46              
47             =head1 NAME
48              
49             MooseX::Types::LoadableClass - ClassName type constraint with coercion to load the class.
50              
51             =head1 VERSION
52              
53             version 0.016
54              
55             =head1 SYNOPSIS
56              
57             package MyClass;
58             use Moose;
59             use MooseX::Types::LoadableClass qw/ LoadableClass /;
60              
61             has foobar_class => (
62             is => 'ro',
63             required => 1,
64             isa => LoadableClass,
65             );
66              
67             MyClass->new(foobar_class => 'FooBar'); # FooBar.pm is loaded or an
68             # exception is thrown.
69              
70             =head1 DESCRIPTION
71              
72             use Moose::Util::TypeConstraints;
73              
74             my $tc = subtype as ClassName;
75             coerce $tc, from Str, via { Class::Load::load_class($_); $_ };
76              
77             I've written those three lines of code quite a lot of times, in quite
78             a lot of places.
79              
80             Now I don't have to.
81              
82             =for stopwords ClassName
83              
84             =head1 TYPES EXPORTED
85              
86             =head2 C<LoadableClass>
87              
88             A normal class / package.
89              
90             =head2 C<LoadableRole>
91              
92             Like C<LoadableClass>, except the loaded package must be a L<Moose::Role>.
93              
94             =head1 SUPPORT
95              
96             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Types-LoadableClass>
97             (or L<bug-MooseX-Types-LoadableClass@rt.cpan.org|mailto:bug-MooseX-Types-LoadableClass@rt.cpan.org>).
98              
99             There is also a mailing list available for users of this distribution, at
100             L<http://lists.perl.org/list/moose.html>.
101              
102             There is also an irc channel available for users of this distribution, at
103             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
104              
105             =head1 AUTHOR
106              
107             Tomas Doran <bobtfish@bobtfish.net>
108              
109             =head1 CONTRIBUTORS
110              
111             =for stopwords Karen Etheridge Dagfinn Ilmari Mannsåker Florian Ragwitz Graham Knop Gregory Oschwald Сергей Романов
112              
113             =over 4
114              
115             =item *
116              
117             Karen Etheridge <ether@cpan.org>
118              
119             =item *
120              
121             Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
122              
123             =item *
124              
125             Florian Ragwitz <rafl@debian.org>
126              
127             =item *
128              
129             Graham Knop <haarg@haarg.org>
130              
131             =item *
132              
133             Gregory Oschwald <goschwald@maxmind.com>
134              
135             =item *
136              
137             Сергей Романов <sromanov@cpan.org>
138              
139             =back
140              
141             =head1 COPYRIGHT AND LICENCE
142              
143             This software is copyright (c) 2010 by Infinity Interactive, Inc.
144              
145             This is free software; you can redistribute it and/or modify it under
146             the same terms as the Perl 5 programming language system itself.
147              
148             =cut