no strict 'refs';
*{"$caller\::subtype"} = \&_subtype;
*{"$caller\::coerce"} = \&_coerce;
-# *{"$caller\::class_type"} = \&_class_type;
+ *{"$caller\::class_type"} = \&_class_type;
# *{"$caller\::role_type"} = \&_role_type;
}
$COERCE->{$pkg}->{$name} = $conf;
}
+sub _class_type {
+ my $pkg = caller(0);
+ $SUBTYPE->{$pkg} ||= +{};
+ my($name, $conf) = @_;
+ my $class = $conf->{class};
+ $SUBTYPE->{$pkg}->{$name} = sub {
+ defined $_ && ref($_) eq $class;
+ };
+}
+
sub typecast_constraints {
my($class, $pkg, $type, $value) = @_;
return $value unless defined $COERCE->{$pkg} && defined $COERCE->{$pkg}->{$type};
--- /dev/null
+use strict;
+use warnings;
+use Test::More tests => 4;
+
+{
+ package Response::Headers;
+ use Mouse;
+ has 'foo' => ( is => 'rw' );
+}
+
+{
+ package Response;
+ use Mouse;
+ use Mouse::TypeRegistry;
+
+ class_type Headers => { class => 'Response::Headers' };
+ coerce 'Headers' => +{
+ HashRef => sub {
+ Response::Headers->new(%{ $_ });
+ },
+ };
+
+ has headers => (
+ is => 'rw',
+ isa => 'Headers',
+ coerce => 1,
+ );
+}
+
+my $res = Response->new(headers => { foo => 'bar' });
+isa_ok($res->headers, 'Response::Headers');
+is($res->headers->foo, 'bar');
+$res->headers({foo => 'yay'});
+isa_ok($res->headers, 'Response::Headers');
+is($res->headers->foo, 'yay');