From: Dave Rolsky Date: Wed, 26 Aug 2009 21:48:52 +0000 (-0500) Subject: Fix triggers to pass old value along with new. X-Git-Tag: 0.10^0 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8207dfe76f6ddd0f0a074746c873ac6b05aed834;p=gitmo%2FMooseX-ClassAttribute.git Fix triggers to pass old value along with new. Require latest Moose as well. --- diff --git a/Build.PL b/Build.PL index ccf034a..9240f9e 100644 --- a/Build.PL +++ b/Build.PL @@ -6,7 +6,7 @@ use Module::Build; my $builder = Module::Build->new ( module_name => 'MooseX::ClassAttribute', license => 'perl', - requires => { 'Moose' => '0.74', + requires => { 'Moose' => '0.89', 'MooseX::AttributeHelpers' => '0.13', }, build_requires => { 'Scalar::Util' => '0', diff --git a/Changes b/Changes index 0964d0c..9fdc675 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,9 @@ +0.10 2009-08-26 + +- Fixed to make triggers work with Moose 0.89+, and made triggers pass the old + attribute value when appropriate just like non-class attributes. + + 0.09 2009-07-09 - An attribute with a builder that wasn't also lazy caused an diff --git a/lib/MooseX/ClassAttribute.pm b/lib/MooseX/ClassAttribute.pm index 772ca85..52eea7f 100644 --- a/lib/MooseX/ClassAttribute.pm +++ b/lib/MooseX/ClassAttribute.pm @@ -3,10 +3,10 @@ package MooseX::ClassAttribute; use strict; use warnings; -our $VERSION = '0.09'; +our $VERSION = '0.10'; our $AUTHORITY = 'cpan:DROLSKY'; -use Moose 0.74 (); +use Moose 0.89 (); use Moose::Exporter; use MooseX::ClassAttribute::Role::Meta::Class; diff --git a/lib/MooseX/ClassAttribute/Meta/Method/Accessor.pm b/lib/MooseX/ClassAttribute/Meta/Method/Accessor.pm index e446ee6..f028970 100644 --- a/lib/MooseX/ClassAttribute/Meta/Method/Accessor.pm +++ b/lib/MooseX/ClassAttribute/Meta/Method/Accessor.pm @@ -100,6 +100,23 @@ sub _inline_check_lazy ( q{'} . $self->associated_attribute()->associated_class()->name() . q{'} ); } +sub _inline_get_old_value_for_trigger +{ + my $self = shift; + + my $attr = $self->associated_attribute(); + return '' unless $attr->has_trigger(); + + my $pred = + $attr->associated_class()->inline_is_class_slot_initialized( $attr->name() ); + + return + 'my @old = ' + . $pred . q{ ? } + . $self->_inline_get() . q{ : ()} . ";\n"; + +} + no Moose; 1; diff --git a/t/03-introspection.t b/t/03-introspection.t index 6818ab0..fa87acf 100644 --- a/t/03-introspection.t +++ b/t/03-introspection.t @@ -16,7 +16,17 @@ ok( HasClassAttribute->meta()->get_class_attribute('ObjectCount') ->meta()->does_role('MooseX::ClassAttribute::Role::Meta::Attribute'), 'get_class_attribute_list returns an object which does the MooseX::ClassAttribute::Role::Meta::Attribute role' ); -my @ca = qw( Delegatee LazyAttribute ManyNames Mapping ObjectCount ReadOnlyAttribute WeakAttribute Built LazyBuilt ); +my @ca = qw( Delegatee + LazyAttribute + ManyNames + Mapping + ObjectCount + ReadOnlyAttribute + WeakAttribute + Built + LazyBuilt + Triggerish + ); is_deeply( [ sort HasClassAttribute->meta()->get_class_attribute_list() ], [ sort @ca ], diff --git a/t/lib/SharedTests.pm b/t/lib/SharedTests.pm index 1299263..b2f542b 100644 --- a/t/lib/SharedTests.pm +++ b/t/lib/SharedTests.pm @@ -87,6 +87,11 @@ use Test::More; builder => '_BuildIt', ); + class_has 'Triggerish' => + ( is => 'rw', + trigger => sub { shift->_CallTrigger(@_) }, + ); + has 'size' => ( is => 'rw', isa => 'Int', @@ -104,6 +109,12 @@ use Test::More; sub _BuildIt { 42 } + our @Triggered; + sub _CallTrigger + { + push @Triggered, [@_]; + } + sub make_immutable { my $class = shift; @@ -152,7 +163,7 @@ use Test::More; sub run_tests { - plan tests => 26; + plan tests => 30; local $Test::Builder::Level = $Test::Builder::Level + 1; @@ -264,6 +275,21 @@ sub run_tests is( HasClassAttribute->LazyBuilt(), 42, 'attribute with lazy builder works' ); } + + { + HasClassAttribute->Triggerish(42); + is( scalar @HasClassAttribute::Triggered, 1, 'trigger was called' ); + is( HasClassAttribute->Triggerish(), 42, 'Triggerish is now 42' ); + + HasClassAttribute->Triggerish(84); + is( HasClassAttribute->Triggerish(), 84, 'Triggerish is now 84' ); + + is_deeply( \@HasClassAttribute::Triggered, + [ [ qw( HasClassAttribute 42 ) ], + [ qw( HasClassAttribute 84 42 ) ], + ], + 'trigger passes old value correctly' ); + } }