Factor out canonicalize_handles into a separate method
[gitmo/Mouse.git] / lib / Mouse / Attribute.pm
index 08eab31..2076297 100644 (file)
@@ -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__