From: Florian Ragwitz Date: Sun, 31 Jan 2010 23:05:16 +0000 (+0100) Subject: Add execute_method to Native::Trait::Code. X-Git-Tag: 0.95~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d9299060d978a65ce60e7a4b4bbb92a73ff58208;p=gitmo%2FMoose.git Add execute_method to Native::Trait::Code. --- diff --git a/Changes b/Changes index d48e593..db666d9 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,12 @@ Also see Moose::Manual::Delta for more details of, and workarounds for, noteworthy changes. + [NEW FEATURES] + + * Moose::Meta::Attribute::Native::Trait::Code now provides execute_method, in + addition to execute, to be able to call a code attribute as a method. + (Florian Ragwitz) + [ENHANCEMENTS] * Moose::Object::does no longer checks the entire inheritance tree, since diff --git a/lib/Moose/Meta/Attribute/Native/MethodProvider/Code.pm b/lib/Moose/Meta/Attribute/Native/MethodProvider/Code.pm index e90eb01..5332f96 100644 --- a/lib/Moose/Meta/Attribute/Native/MethodProvider/Code.pm +++ b/lib/Moose/Meta/Attribute/Native/MethodProvider/Code.pm @@ -6,8 +6,19 @@ $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; sub execute : method { - my ( $attr, $reader, $writer ) = @_; - return sub { my ($self, @args) = @_; $reader->($self)->(@args) }; + my ($attr, $reader, $writer) = @_; + return sub { + my ($self, @args) = @_; + $reader->($self)->(@args); + }; +} + +sub execute_method : method { + my ($attr, $reader, $writer) = @_; + return sub { + my ($self, @args) = @_; + $reader->($self)->($self, @args); + }; } no Moose::Role; diff --git a/lib/Moose/Meta/Attribute/Native/Trait/Code.pm b/lib/Moose/Meta/Attribute/Native/Trait/Code.pm index aa0a599..7d62ce3 100644 --- a/lib/Moose/Meta/Attribute/Native/Trait/Code.pm +++ b/lib/Moose/Meta/Attribute/Native/Trait/Code.pm @@ -59,6 +59,10 @@ This provides operations on coderef attributes. Calls the coderef with the given args. +=item B + +Calls the coderef with the the instance as invocant and given args. + =back =head1 METHODS diff --git a/t/070_native_traits/209_trait_code.t b/t/070_native_traits/209_trait_code.t index 842b323..5569902 100644 --- a/t/070_native_traits/209_trait_code.t +++ b/t/070_native_traits/209_trait_code.t @@ -8,11 +8,19 @@ use Test::More; use Moose; has callback => ( - traits => ['Code'], - is => 'ro', - isa => 'CodeRef', + traits => ['Code'], + is => 'ro', + isa => 'CodeRef', + required => 1, + handles => { 'invoke_callback' => 'execute' }, + ); + + has callback_method => ( + traits => ['Code'], + is => 'ro', + isa => 'CodeRef', required => 1, - handles => { 'invoke_callback' => 'execute' }, + handles => { 'invoke_method_callback' => 'execute_method' }, ); has multiplier => ( @@ -26,13 +34,15 @@ use Test::More; my $i = 0; my $thingy = Thingy->new( - callback => sub { ++$i }, - multiplier => sub { $_[0] * 2 } + callback => sub { ++$i }, + multiplier => sub { $_[0] * 2 }, + callback_method => sub { shift->multiply(@_) }, ); is($i, 0); $thingy->invoke_callback; is($i, 1); is($thingy->multiply(3), 6); +is($thingy->invoke_method_callback(3), 6); done_testing;