From: Shawn M Moore Date: Tue, 10 Jun 2008 01:57:47 +0000 (+0000) Subject: Factor out canonicalize_handles into a separate method X-Git-Tag: 0.04~75 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=commitdiff_plain;h=af745d5a30ceb45518de556943e95dfdc03f451a;hp=ab27a55e79fe77296eeebfbba1adbd3155f1a746 Factor out canonicalize_handles into a separate method --- 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__ diff --git a/t/019-handles.t b/t/019-handles.t index 6d265be..79d29c2 100644 --- a/t/019-handles.t +++ b/t/019-handles.t @@ -42,25 +42,25 @@ do { has error => ( handles => "string", ); - } qr/You must pass a HASH or ARRAY to handles/; + } qr/Unable to canonicalize the 'handles' option with string/; ::throws_ok { has error2 => ( handles => \"ref_to_string", ); - } qr/You must pass a HASH or ARRAY to handles/; + } qr/Unable to canonicalize the 'handles' option with SCALAR\(\w+\)/; ::throws_ok { has error3 => ( handles => qr/regex/, ); - } qr/You must pass a HASH or ARRAY to handles/; + } qr/Unable to canonicalize the 'handles' option with \(\?-xism:regex\)/; ::throws_ok { has error4 => ( handles => sub { "code" }, ); - } qr/You must pass a HASH or ARRAY to handles/; + } qr/Unable to canonicalize the 'handles' option with CODE\(\w+\)/; }; can_ok(Class => qw(person has_person person_name person_age name age quid));