X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FAttribute.pm;fp=lib%2FMouse%2FAttribute.pm;h=2076297b39606e487838d7a5bf2f2aeec5ebddb7;hb=af745d5a30ceb45518de556943e95dfdc03f451a;hp=08eab3101e1d5fcb3caa8eba0eb9ebb8a8f43509;hpb=ab27a55e79fe77296eeebfbba1adbd3155f1a746;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Attribute.pm b/lib/Mouse/Attribute.pm index 08eab31..2076297 100644 --- a/lib/Mouse/Attribute.pm +++ b/lib/Mouse/Attribute.pm @@ -144,13 +144,8 @@ sub create { if ref($args{default}) && ref($args{default}) ne 'CODE'; - $args{handles} = { map { $_ => $_ } @{ $args{handles} } } - if $args{handles} - && ref($args{handles}) eq 'ARRAY'; - - confess "You must pass a HASH or ARRAY to handles" - if exists($args{handles}) - && ref($args{handles}) ne 'HASH'; + $args{handles} = { $self->_canonicalize_handles($args{handles}) } + if $args{handles}; $args{type_constraint} = delete $args{isa}; @@ -205,8 +200,7 @@ sub verify_type_constraint { my $type = $self->type_constraint or return 1; - my $constraint = $self->find_type_constraint - or return 1; + my $constraint = $self->find_type_constraint; return 1 if $constraint->($_); @@ -215,6 +209,21 @@ sub verify_type_constraint { Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_"); } +sub _canonicalize_handles { + my $self = shift; + my $handles = shift; + + if (ref($handles) eq 'HASH') { + return %$handles; + } + elsif (ref($handles) eq 'ARRAY') { + return map { $_ => $_ } @$handles; + } + else { + confess "Unable to canonicalize the 'handles' option with $handles"; + } +} + 1; __END__