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