From: Yuval Kogman Date: Sun, 16 Sep 2007 22:54:09 +0000 (+0000) Subject: add clear X-Git-Tag: 0.18_01~62 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8cf40f808c137fab5ab7438ade7308fe97d09884;p=gitmo%2FMooseX-AttributeHelpers.git add clear --- diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/Array.pm b/lib/MooseX/AttributeHelpers/MethodProvider/Array.pm index 2dd8f1d..54f0557 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/Array.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/Array.pm @@ -60,6 +60,13 @@ sub shift : method { CORE::shift @{$reader->($_[0])} }; } + +sub clear : method { + my ($attr, $reader, $writer) = @_; + return sub { + @{$reader->($_[0])} = () + }; +} sub get : method { my ($attr, $reader, $writer) = @_; @@ -127,6 +134,8 @@ see those provied methods, refer to that documentation. =item B +=item B + =back =head1 BUGS diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm b/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm index cff3237..5f2cde3 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm @@ -49,6 +49,11 @@ sub empty : method { return sub { scalar keys %{$reader->($_[0])} ? 1 : 0 }; } +sub clear : method { + my ($attr, $reader, $writer) = @_; + return sub { %{$reader->($_[0])} = () }; +} + sub delete : method { my ($attr, $reader, $writer) = @_; return sub { delete $reader->($_[0])->{$_[1]} }; @@ -87,6 +92,8 @@ L. =item B +=item B + =item B =item B diff --git a/t/002_basic_array.t b/t/002_basic_array.t index fbd7176..4cc3404 100644 --- a/t/002_basic_array.t +++ b/t/002_basic_array.t @@ -28,6 +28,7 @@ BEGIN { 'set' => 'set_option_at', 'count' => 'num_options', 'empty' => 'has_options', + 'clear' => 'clear_options', } ); } @@ -43,6 +44,7 @@ can_ok($stuff, $_) for qw[ get_option_at set_option_at num_options + clear_options has_options ]; @@ -107,6 +109,9 @@ is($stuff->remove_first_option, 10, '... getting the first option'); is($stuff->num_options, 5, '... got 5 options'); is($stuff->get_option_at(0), 20, '... get option at index 0'); +$stuff->clear_options; +is_deeply( $stuff->options, [], "... clear options" ); + ## check some errors dies_ok { @@ -139,6 +144,7 @@ is_deeply($options->provides, { 'set' => 'set_option_at', 'count' => 'num_options', 'empty' => 'has_options', + 'clear' => 'clear_options', }, '... got the right provies mapping'); is($options->container_type, 'Int', '... got the right container type'); diff --git a/t/003_basic_hash.t b/t/003_basic_hash.t index f0baeba..8888f4b 100644 --- a/t/003_basic_hash.t +++ b/t/003_basic_hash.t @@ -25,6 +25,7 @@ BEGIN { 'get' => 'get_option', 'empty' => 'has_options', 'count' => 'num_options', + 'clear' => 'clear_options', 'delete' => 'delete_option', } ); @@ -39,6 +40,7 @@ can_ok($stuff, $_) for qw[ has_options num_options delete_option + clear_options ]; ok(!$stuff->has_options, '... we have no options'); @@ -70,6 +72,10 @@ lives_ok { is($stuff->num_options, 1, '... we have 1 option(s)'); is_deeply($stuff->options, { foo => 'bar' }, '... got more options now'); +$stuff->clear_options; + +is_deeply($stuff->options, { }, "... cleared options" ); + lives_ok { Stuff->new(options => { foo => 'BAR' }); } '... good constructor params';