From: Shawn M Moore Date: Fri, 23 May 2008 00:05:43 +0000 (+0000) Subject: Allow trigger on ro (or no-accessor) attributes X-Git-Tag: 0_55~153 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0b7df53c97a0e2bc4f329d276d0490e376698223;p=gitmo%2FMoose.git Allow trigger on ro (or no-accessor) attributes --- diff --git a/Changes b/Changes index 0247d44..5889fad 100644 --- a/Changes +++ b/Changes @@ -20,6 +20,11 @@ Revision history for Perl extension Moose is currently not supported in roles, but, ... patches welcome + * Moose::Meta::Attribute + - trigger on a ro-attribute is no longer an + error, as it's useful to trigger off of the + constructor + * Moose::Meta::Class - added same 'add_package_symbol' fix as in Class::MOP 0.56 diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index 1707392..a2bbfdb 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -209,14 +209,9 @@ sub _process_options { if (exists $options->{is}) { if ($options->{is} eq 'ro') { $options->{reader} ||= $name; - (!exists $options->{trigger}) - || confess "Cannot have a trigger on a read-only attribute $name"; } elsif ($options->{is} eq 'rw') { $options->{accessor} = $name; - ((reftype($options->{trigger}) || '') eq 'CODE') - || confess "Trigger must be a CODE ref" - if exists $options->{trigger}; } else { confess "I do not understand this option (is => " . $options->{is} . ") on attribute $name" @@ -259,6 +254,11 @@ sub _process_options { if $options->{weak_ref}; } + if (exists $options->{trigger}) { + (reftype($options->{trigger}) || '') eq 'CODE' + || confess "Trigger must be a CODE ref"; + } + if (exists $options->{auto_deref} && $options->{auto_deref}) { (exists $options->{type_constraint}) || confess "You cannot auto-dereference without specifying a type constraint on attribute $name"; diff --git a/t/020_attributes/004_attribute_triggers.t b/t/020_attributes/004_attribute_triggers.t index 0a744a5..b5cf34e 100644 --- a/t/020_attributes/004_attribute_triggers.t +++ b/t/020_attributes/004_attribute_triggers.t @@ -5,7 +5,7 @@ use warnings; use Scalar::Util 'isweak'; -use Test::More tests => 27; +use Test::More tests => 26; use Test::Exception; BEGIN { @@ -109,15 +109,6 @@ BEGIN { use Moose; ::dies_ok { - has('bling' => (is => 'ro', trigger => sub { 0 })); - } '... cannot create trigger on a read-only attr'; -} - -{ - package Bling::Bling; - use Moose; - - ::dies_ok { has('bling' => (is => 'rw', trigger => 'Fail')); } '... a trigger must be a CODE ref';