no strict 'refs';
\@{ $args{name} . '::ISA' };
};
+ $args{roles} ||= [];
bless \%args, $class;
}
);
}
+sub roles { $_[0]->{roles} }
+
+sub does_role {
+ my ($self, $role_name) = @_;
+ (defined $role_name)
+ || confess "You must supply a role name to look for";
+ for my $role (@{ $self->{roles} }) {
+ return 1 if $role->name eq $role_name;
+ }
+ return 0;
+}
+
1;
__END__
$args{attributes} ||= {};
$args{required_methods} ||= [];
+ $args{roles} ||= [];
bless \%args, $class;
}
}
}
}
+
+ # append roles
+ push @{ $class->roles }, $self, @{ $self->roles };
}
for my $modifier_type (qw/before after around/) {
};
}
+sub roles { $_[0]->{roles} }
+
1;
*{"$caller\::subtype"} = \&_subtype;
*{"$caller\::coerce"} = \&_coerce;
*{"$caller\::class_type"} = \&_class_type;
-# *{"$caller\::role_type"} = \&_role_type;
+ *{"$caller\::role_type"} = \&_role_type;
}
sub _import {
};
}
+sub _role_type {
+ my $pkg = caller(0);
+ $SUBTYPE->{$pkg} ||= +{};
+ my($name, $conf) = @_;
+ my $role = $conf->{role};
+ $SUBTYPE->{$pkg}->{$name} = sub {
+ return unless defined $_ && ref($_) && $_->isa('Mouse::Object');
+ $_->meta->does_role($role);
+ };
+}
+
sub typecast_constraints {
my($class, $pkg, $type, $value) = @_;
return $value unless defined $COERCE->{$pkg} && defined $COERCE->{$pkg}->{$type};
use strict;
use warnings;
-use Test::More tests => 4;
+use Test::More tests => 5;
{
- package Response::Headers;
+ package Request::Headers::Role;
+ use Mouse::Role;
+ has 'foo' => ( is => 'rw' );
+}
+
+{
+ package Request::Headers;
use Mouse;
+ with 'Request::Headers::Role';
+}
+
+{
+ package Response::Headers::Role;
+ use Mouse::Role;
has 'foo' => ( is => 'rw' );
}
{
+ package Response::Headers;
+ use Mouse;
+ with 'Response::Headers::Role';
+}
+
+{
package Response;
use Mouse;
use Mouse::TypeRegistry;
- class_type Headers => { class => 'Response::Headers' };
+ role_type Headers => { role => 'Response::Headers::Role' };
coerce 'Headers' => +{
HashRef => sub {
Response::Headers->new(%{ $_ });
$res->headers({foo => 'yay'});
isa_ok($res->headers, 'Response::Headers');
is($res->headers->foo, 'yay');
+
+eval {
+ $res->headers( Request::Headers->new( foo => 'baz' ) );
+};
+ok $@;