<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package MooseX::StrictAttributes::Meta::Attribute::Trait::Isa;
use Moose::Role;

after 'attach_to_class' =&gt; sub {
    my ($attr, $class) = @_;
    return unless $attr-&gt;has_type_constraint;
    my $type_constraint = $attr-&gt;type_constraint;
    if ($type_constraint-&gt;isa('Moose::Meta::TypeConstraint::Class')) {
        $attr-&gt;__test_class_type_constraint($type_constraint); 
    }
    elsif ($type_constraint-&gt;isa('Moose::Meta::TypeConstraint::Parameterized') &amp;&amp;
        $type_constraint-&gt;has_type_parameter
    ) {
        $attr-&gt;__test_class_type_constraint($type_constraint-&gt;type_parameter);
    }
};

sub __test_class_type_constraint {
    my ($attr, $type_constraint) = @_;
    my $type_constraint_class = $type_constraint-&gt;class;
 
    confess("Can't create a class type constraint for isa on attribute " . $attr-&gt;name .
        " because '" . $type_constraint_class . "' is not a class name") 
        unless Class::MOP::is_class_loaded($type_constraint_class);
}

sub _is_valid_class_name {
    my $class = shift;

    return 0 if ref($class);
    return 0 unless defined($class);
    return 0 unless length($class);

    return 1 if $class =~ /^\w+(?:::\w+)*$/;

    return 0;
}

1;

__END__

=head1 NAME

MooseX::StrictAttributes::Meta::Attribute::Trait::Isa - An attribute metaclass trait which
enures that all class types attached to an attribute with isa refer to loaded classes.

=head1 SYNOPSIS

    package My::Class
    use Moose;
    
    # Throws exception as we haven't loaded Other::Class
    has foo ( isa =&gt; 'Other::Class', traits =&gt; [qw/ StrictIsa /] );

    package Other::Class;
    use Moose;
    
    # This is now fine, as we defined My::Class above
    has bar ( isa =&gt; 'My::Class', traits =&gt; [qw/ StrictIsa /] );

=head1 DESCRIPTION

Performs a check that the C&lt;isa&gt; type of any attributes with this type, if a class type,
refers to a valid and loaded class. This module will also try to introspect more complex
(e.g. parametrized and union) type constraints, extract any class types from them, and
test they also refer to valid classes.

=head1 BUGS AND SOURCE CODE

This software probably contains bugs somewhere, and the way in which some components
are implemented is not optimal.

Patches welcome. Please ask in #moose for commit bits.

The source code for this project is in the Moose repository at L&lt;http://code2.0beta.co.uk/moose/svn/&gt;

=head1 AUTHORS

Tomas Doran &lt;bobtfish@bobtfish.net&gt; (t0m on #moose)

=head1 COPYRIGHT AND LICENSE

Copyright 2008 Tomas Doran.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
</pre></body></html>