From: Dylan William Hardison Date: Wed, 27 Jan 2010 23:31:54 +0000 (-0500) Subject: Behavior for method modifiers with non-regexp refs X-Git-Tag: 0.95~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=775666aaf8c87fc200eac76abe6967bd943751c4;p=gitmo%2FMoose.git Behavior for method modifiers with non-regexp refs The code: before ['foo', 'bar'] => sub { } Was previously silently ignored. Now it means the same as: before ('foo', 'bar') => sub { } This means that method modifiers can now be: * a list * a regexp ref. * an arrayref Anything else results in an error message. --- diff --git a/Changes b/Changes index 3f363b4..3a12a7c 100644 --- a/Changes +++ b/Changes @@ -7,6 +7,9 @@ for, noteworthy changes. Moose::Meta::Class::does_role already does this. (doy) * Moose::Object now has stubs for BUILD and DEMOLISH, so they can be safely wrapped in roles without needing to provide your own stubs. (doy) + * Moose::Util::add_method_modifier (and subsequently the sugar functions Moose::before, + Moose::after, and Moose::around) can now accept arrayrefs, with the same + behavior as lists. Types other than arrayref and regexp result in an error. 0.94 Mon, Jan 18, 2010 diff --git a/lib/Moose/Manual/Delta.pod b/lib/Moose/Manual/Delta.pod index 6b64457..94f3e49 100644 --- a/lib/Moose/Manual/Delta.pod +++ b/lib/Moose/Manual/Delta.pod @@ -16,6 +16,18 @@ feature. If you encounter a problem and have a solution but don't see it documented here, or think we missed an important feature, please send us a patch. +=head1 0.95 + +=over 4 + +=item Moose::Util add_method_modifier behavior + +add_method_modifier (and subsequently the sugar functions Moose::before, +Moose::after, and Moose::around) can now accept arrayrefs, with the same +behavior as lists. Types other than arrayref and regexp result in an error. + +=back + =head1 0.93_01 and 0.94 =over 4 diff --git a/lib/Moose/Util.pm b/lib/Moose/Util.pm index 225d41a..e5e0058 100644 --- a/lib/Moose/Util.pm +++ b/lib/Moose/Util.pm @@ -212,6 +212,18 @@ sub add_method_modifier { $meta->$add_modifier_method( $_->name, $code ) for @matched_methods; } + elsif ($method_modifier_type eq 'ARRAY') { + $meta->$add_modifier_method( $_, $code ) for @{$args->[0]}; + } + else { + $meta->throw_error( + sprintf( + "Methods passed to %s must be provided as a list, arrayref or regex, not %s", + $modifier_name, + $method_modifier_type, + ) + ); + } } else { $meta->$add_modifier_method( $_, $code ) for @{$args}; diff --git a/t/400_moose_util/008_method_mod_args.t b/t/400_moose_util/008_method_mod_args.t new file mode 100644 index 0000000..5919287 --- /dev/null +++ b/t/400_moose_util/008_method_mod_args.t @@ -0,0 +1,31 @@ +use strict; +use warnings; + +use Test::More; +use Test::Exception; +use Moose::Util qw( add_method_modifier ); + +my $COUNT = 0; +{ + package Foo; + use Moose; + + sub foo { } + sub bar { } +} + +lives_ok { + add_method_modifier('Foo', 'before', [ ['foo', 'bar'], sub { $COUNT++ } ]); +} 'method modifier with an arrayref'; + +dies_ok { + add_method_modifier('Foo', 'before', [ {'foo' => 'bar'}, sub { $COUNT++ } ]); +} 'method modifier with a hashref'; + +my $foo = Foo->new; +$foo->foo; +$foo->bar; +is($COUNT, 2, "checking that the modifiers were installed."); + + +done_testing;