From: gfx Date: Tue, 15 Sep 2009 05:55:02 +0000 (+0900) Subject: Improve type constraint stuff X-Git-Tag: 0.32~57 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9c85e9dc1fea8af52dde98b81ad345eb44e79242;p=gitmo%2FMouse.git Improve type constraint stuff --- diff --git a/Changes b/Changes index 0ae2f73..7f5a7cb 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,7 @@ Revision history for Mouse 0.29 * Support is => 'bare', and you must pass the 'is' option (gfx) + * Make generator methods private (gfx) 0.28 Wed Sep 8 20:00:06 2009 diff --git a/lib/Mouse/Meta/Attribute.pm b/lib/Mouse/Meta/Attribute.pm index 65d6daf..4c220b1 100644 --- a/lib/Mouse/Meta/Attribute.pm +++ b/lib/Mouse/Meta/Attribute.pm @@ -221,13 +221,6 @@ sub create { if exists $args{coerce}; if (exists $args{isa}) { - confess "Got isa => $args{isa}, but Mouse does not yet support parameterized types for containers other than ArrayRef and HashRef (rt.cpan.org #39795)" - if $args{isa} =~ /^([^\[]+)\[.+\]$/ && - $1 ne 'ArrayRef' && - $1 ne 'HashRef' && - $1 ne 'Maybe' - ; - my $type_constraint = delete $args{isa}; $args{type_constraint}= Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($type_constraint); } diff --git a/lib/Mouse/Meta/Role.pm b/lib/Mouse/Meta/Role.pm index 73199c4..4910f72 100644 --- a/lib/Mouse/Meta/Role.pm +++ b/lib/Mouse/Meta/Role.pm @@ -3,7 +3,6 @@ use strict; use warnings; use Carp 'confess'; -use Mouse::Meta::Attribute; use Mouse::Util qw(version authority identifier); do { @@ -51,7 +50,7 @@ sub add_attribute { my $self = shift; my $name = shift; my $spec = shift; - $self->{attributes}->{$name} = Mouse::Meta::Attribute->new($name, %$spec); + $self->{attributes}->{$name} = $spec; } sub has_attribute { exists $_[0]->{attributes}->{$_[1]} } diff --git a/lib/Mouse/Meta/TypeConstraint.pm b/lib/Mouse/Meta/TypeConstraint.pm index 538e3b2..51b0867 100644 --- a/lib/Mouse/Meta/TypeConstraint.pm +++ b/lib/Mouse/Meta/TypeConstraint.pm @@ -1,6 +1,8 @@ package Mouse::Meta::TypeConstraint; use strict; use warnings; +use Carp (); + use overload '""' => sub { shift->{name} }, # stringify to tc name fallback => 1; @@ -28,6 +30,26 @@ sub check { $self->{_compiled_type_constraint}->(@_); } +sub validate { + my ($self, $value) = @_; + if ($self->{_compiled_type_constraint}->($value)) { + return undef; + } + else { + $self->get_message($value); + } +} + +sub assert_valid { + my ($self, $value) = @_; + + my $error = $self->validate($value); + return 1 if ! defined $error; + + Carp::confess($error); +} + + sub message { return $_[0]->{message}; } diff --git a/lib/Mouse/Util/TypeConstraints.pm b/lib/Mouse/Util/TypeConstraints.pm index 4f77130..993e8e0 100644 --- a/lib/Mouse/Util/TypeConstraints.pm +++ b/lib/Mouse/Util/TypeConstraints.pm @@ -308,6 +308,13 @@ sub find_type_constraint { sub find_or_create_isa_type_constraint { my $type_constraint = shift; + Carp::confess("Got isa => type_constraints, but Mouse does not yet support parameterized types for containers other than ArrayRef and HashRef and Maybe (rt.cpan.org #39795)") + if $type_constraint =~ /\A ( [^\[]+ ) \[\.+\] \z/xms && + $1 ne 'ArrayRef' && + $1 ne 'HashRef' && + $1 ne 'Maybe' + ; + my $code; $type_constraint =~ s/\s+//g; diff --git a/t/030_roles/002_role.t b/t/030_roles/002_role.t index 4deb2ec..96ead79 100755 --- a/t/030_roles/002_role.t +++ b/t/030_roles/002_role.t @@ -85,13 +85,13 @@ is_deeply( ok($foo_role->has_attribute('bar'), '... FooRole does have the bar attribute'); -is $foo_role->get_attribute('bar')->name, 'bar', '... got the correct description of the bar attribute'; +is $foo_role->get_attribute('bar')->{is}, 'rw', '... got the correct description of the bar attribute'; ok($foo_role->has_attribute('baz'), '... FooRole does have the baz attribute'); is( - $foo_role->get_attribute('baz')->name, - 'baz', + $foo_role->get_attribute('baz')->{is}, + 'ro', '... got the correct description of the baz attribute'); # method modifiers diff --git a/t/402-attribute-application.t b/t/402-attribute-application.t index cfd0256..e4745c5 100644 --- a/t/402-attribute-application.t +++ b/t/402-attribute-application.t @@ -16,7 +16,7 @@ do { no Mouse::Role; }; -is(Role->meta->get_attribute('attr')->default, 'Role'); +is(Role->meta->get_attribute('attr')->{default}, 'Role'); do { package Class;