All unit tests passing with refactored stuff, documentation updated significantly.
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / Hash.pm
index 6e08454..2ab57a4 100644 (file)
@@ -1,5 +1,6 @@
 package MooseX::AttributeHelpers::MethodProvider::Hash;
 use Moose::Role;
+use MooseX::AttributeHelpers::Collection::TypeCheck;
 
 our $VERSION   = '0.04';
 our $AUTHORITY = 'cpan:STEVAN';
@@ -8,45 +9,22 @@ with 'MooseX::AttributeHelpers::MethodProvider::ImmutableHash';
 
 sub set : method {
     my ($attr, $reader, $writer) = @_;
-    if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) {
-        my $container_type_constraint = $attr->type_constraint->type_parameter;
-        return sub { 
-            my ( $self, @kvp ) = @_;
-           
-            my ( @keys, @values );
-
-            while ( @kvp ) {
-                my ( $key, $value ) = ( shift(@kvp), shift(@kvp) );
-                ($container_type_constraint->check($value)) 
-                    || confess "Value " . ($value||'undef') . " did not pass container type constraint";
-                push @keys, $key;
-                push @values, $value;
+    type_check(
+        $attr, 
+        sub {
+            my ($self, %pairs) = @_;
+            return (values %pairs);
+        },
+        sub {
+            my ($self, @pairs) = @_;
+            my $hash = $reader->($self);
+            while (@pairs) {
+                my $key = shift(@pairs);
+                my $value = shift(@pairs);
+                $hash->{$key} = $value;
             }
-
-            if ( @values > 1 ) {
-                @{ $reader->($self) }{@keys} = @values;
-            } else {
-                $reader->($self)->{$keys[0]} = $values[0];
-            }
-        };
-    }
-    else {
-        return sub {
-            if ( @_ == 3 ) {
-                $reader->($_[0])->{$_[1]} = $_[2]
-            } else {
-                my ( $self, @kvp ) = @_;
-                my ( @keys, @values );
-
-                while ( @kvp ) {
-                    push @keys, shift @kvp;
-                    push @values, shift @kvp;
-                }
-
-                @{ $reader->($_[0]) }{@keys} = @values;
-            }
-        };
-    }
+        },
+    );
 }
 
 sub clear : method {
@@ -75,18 +53,9 @@ MooseX::AttributeHelpers::MethodProvider::Hash
 =head1 DESCRIPTION
 
 This is a role which provides the method generators for 
-L<MooseX::AttributeHelpers::Collection::Hash>.
-
-This role is composed from the 
-L<MooseX::AttributeHelpers::Collection::ImmutableHash> role.
-
-=head1 METHODS
-
-=over 4
-
-=item B<meta>
-
-=back
+L<MooseX::AttributeHelpers::Collection::Hash>.  It consumes 
+L<MooseX::AttributeHelpers::MethodProvider::ImmutableHash>, and thus 
+provides all its methods as wel.
 
 =head1 PROVIDED METHODS
 
@@ -94,23 +63,20 @@ L<MooseX::AttributeHelpers::Collection::ImmutableHash> role.
 
 =item B<count>
 
-=item B<delete>
+Returns the number of items in the hash.
 
-=item B<empty>
+=item B<delete(@keys)>
 
-=item B<clear>
+Deletes the specified keys from the hash.
 
-=item B<exists>
-
-=item B<get>
+=item B<clear>
 
-=item B<keys>
+Deletes all keys from the hash.
 
 =item B<set>
 
-=item B<values>
-
-=item B<kv>
+Sets the specified keys to the specified values.  You can specify several of
+these at once, in key => value order.
 
 =back