X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FAttributeHelpers%2FMethodProvider%2FHash.pm;h=2ab57a4ad8bf964c84815e931b4830c1cb3450d0;hb=720fa35be1e2cb679dc0d146f2cef1d459cb4b5e;hp=f70c5ef4f0060626b109e058ba3c2471253eef76;hpb=9a9764976656e1a089735d0cb1f1affd06f4d7e4;p=gitmo%2FMooseX-AttributeHelpers.git diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm b/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm index f70c5ef..2ab57a4 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm @@ -1,24 +1,30 @@ package MooseX::AttributeHelpers::MethodProvider::Hash; use Moose::Role; +use MooseX::AttributeHelpers::Collection::TypeCheck; -our $VERSION = '0.03'; +our $VERSION = '0.04'; our $AUTHORITY = 'cpan:STEVAN'; 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 { - ($container_type_constraint->check($_[2])) - || confess "Value " . ($_[2]||'undef') . " did not pass container type constraint"; - $reader->($_[0])->{$_[1]} = $_[2] - }; - } - else { - return sub { $reader->($_[0])->{$_[1]} = $_[2] }; - } + 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; + } + }, + ); } sub clear : method { @@ -28,7 +34,10 @@ sub clear : method { sub delete : method { my ($attr, $reader, $writer) = @_; - return sub { CORE::delete $reader->($_[0])->{$_[1]} }; + return sub { + my $hashref = $reader->(shift); + CORE::delete @{$hashref}{@_}; + }; } 1; @@ -44,18 +53,9 @@ MooseX::AttributeHelpers::MethodProvider::Hash =head1 DESCRIPTION This is a role which provides the method generators for -L. - -This role is composed from the -L role. - -=head1 METHODS - -=over 4 - -=item B - -=back +L. It consumes +L, and thus +provides all its methods as wel. =head1 PROVIDED METHODS @@ -63,23 +63,20 @@ L role. =item B -=item B - -=item B +Returns the number of items in the hash. -=item B +=item B -=item B +Deletes the specified keys from the hash. -=item B +=item B -=item B +Deletes all keys from the hash. =item B -=item B - -=item B +Sets the specified keys to the specified values. You can specify several of +these at once, in key => value order. =back @@ -95,7 +92,7 @@ Stevan Little Estevan@iinteractive.comE =head1 COPYRIGHT AND LICENSE -Copyright 2007 by Infinity Interactive, Inc. +Copyright 2007-2008 by Infinity Interactive, Inc. L