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
|
package MooseX::Types::LoadableClass;
BEGIN {
$MooseX::Types::LoadableClass::AUTHORITY = 'cpan:BOBTFISH';
}
{
$MooseX::Types::LoadableClass::VERSION = '0.012';
}
# git description: v0.011-2-gb666a58
# ABSTRACT: ClassName type constraint with coercion to load the class.
use strict;
use warnings;
use MooseX::Types -declare => [qw/ ClassName LoadableClass LoadableRole /];
use MooseX::Types::Moose qw(Str RoleName), ClassName => { -as => 'MooseClassName' };
use Moose::Util::TypeConstraints;
use Class::Load qw(is_class_loaded load_optional_class);
use Module::Runtime qw(is_module_name);
use namespace::autoclean;
subtype LoadableClass,
as Str,
where {
is_module_name($_)
and is_class_loaded($_) || load_optional_class($_)
and MooseClassName->check($_)
};
subtype LoadableRole,
as Str,
where {
is_module_name($_)
and is_class_loaded($_) || load_optional_class($_)
and RoleName->check($_)
};
# back compat
coerce LoadableClass, from Str, via { $_ };
coerce LoadableRole, from Str, via { $_ };
__PACKAGE__->type_storage->{ClassName}
= __PACKAGE__->type_storage->{LoadableClass};
__PACKAGE__->meta->make_immutable;
1;
__END__
=pod
=encoding UTF-8
=for :stopwords Tomas Doran Infinity Interactive, Inc Dagfinn Ilmari Mannsåker Florian
Ragwitz Karen Etheridge ClassName
=head1 NAME
MooseX::Types::LoadableClass - ClassName type constraint with coercion to load the class.
=head1 VERSION
version 0.012
=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.
=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 AUTHOR
Tomas Doran <bobtfish@bobtfish.net>
=head1 COPYRIGHT AND LICENSE
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.
=head1 CONTRIBUTORS
=over 4
=item *
Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
=item *
Florian Ragwitz <rafl@debian.org>
=item *
Karen Etheridge <ether@cpan.org>
=back
=cut
|