From: Shawn M Moore Date: Thu, 11 Sep 2008 08:09:35 +0000 (+0000) Subject: Remove before/around triggers X-Git-Tag: 0.19~223 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a08e715f7c381c665e5974976e56ebae491f7eda;p=gitmo%2FMouse.git Remove before/around triggers --- diff --git a/lib/Mouse/Meta/Attribute.pm b/lib/Mouse/Meta/Attribute.pm index 230efb5..368e10d 100644 --- a/lib/Mouse/Meta/Attribute.pm +++ b/lib/Mouse/Meta/Attribute.pm @@ -59,11 +59,7 @@ sub generate_accessor { my $type = $attribute->type_constraint; my $constraint = $attribute->find_type_constraint; my $builder = $attribute->builder; - - my $trigger = $attribute->trigger; - my $before = $trigger->{before}; - my $after = $trigger->{after}; - my $around = $trigger->{around}; + my $trigger = $attribute->trigger; my $accessor = 'sub { my $self = shift;'; @@ -72,36 +68,21 @@ sub generate_accessor { $accessor .= 'if (@_) { local $_ = $_[0];'; - if ($before) { - $accessor .= '$before->($self, $_, $attribute);'; - } - - if ($around) { - $accessor .= '$around->(sub { - my $self = shift; - $_ = $_[0]; - '; + if ($constraint) { + $accessor .= 'unless ($constraint->()) { + my $display = defined($_) ? overload::StrVal($_) : "undef"; + Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display"); + }' } - if ($constraint) { - $accessor .= 'unless ($constraint->()) { - my $display = defined($_) ? overload::StrVal($_) : "undef"; - Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display"); - }' - } + $accessor .= '$self->{$key} = $_;'; - $accessor .= '$self->{$key} = $_;'; - - if ($attribute->is_weak_ref) { - $accessor .= 'Scalar::Util::weaken($self->{$key}) if ref($self->{$key});'; - } - - if ($around) { - $accessor .= '}, $self, $_, $attribute);'; + if ($attribute->is_weak_ref) { + $accessor .= 'Scalar::Util::weaken($self->{$key}) if ref($self->{$key});'; } - if ($after) { - $accessor .= '$after->($self, $_, $attribute);'; + if ($trigger) { + $accessor .= '$trigger->($self, $_, $attribute);'; } $accessor .= '}'; @@ -272,17 +253,12 @@ sub validate_args { && $args->{isa} ne 'HashRef'; if ($args->{trigger}) { - if (ref($args->{trigger}) eq 'CODE') { - $args->{trigger} = { - after => $args->{trigger}, - }; - } - elsif (ref($args->{trigger}) eq 'HASH') { - Carp::carp "HASH-based form of trigger is deprecated. Please switch back to using the coderef form of trigger."; + if (ref($args->{trigger}) eq 'HASH') { + Carp::carp "HASH-based form of trigger has been removed. Only the coderef form of triggers are now supported."; } confess "Trigger must be a CODE ref on attribute ($name)" - if ref($args->{trigger}) ne 'HASH'; + if ref($args->{trigger}) ne 'CODE'; } return 1; diff --git a/lib/Mouse/Object.pm b/lib/Mouse/Object.pm index 1511d28..115a534 100644 --- a/lib/Mouse/Object.pm +++ b/lib/Mouse/Object.pm @@ -20,35 +20,16 @@ sub new { my $default; if (defined($from) && exists($args->{$from})) { - if ($attribute->has_trigger && $attribute->trigger->{before}) { - $attribute->trigger->{before}->($instance, $args->{$from}, $attribute); - } - - if ($attribute->has_trigger && $attribute->trigger->{around}) { - $attribute->trigger->{around}->(sub { - $args->{$from} = $_[1]; - - $attribute->verify_type_constraint($args->{$from}) - if $attribute->has_type_constraint; + $attribute->verify_type_constraint($args->{$from}) + if $attribute->has_type_constraint; - $instance->{$key} = $args->{$from}; + $instance->{$key} = $args->{$from}; - weaken($instance->{$key}) - if ref($instance->{$key}) && $attribute->is_weak_ref; - }, $instance, $args->{$from}, $attribute); - } - else { - $attribute->verify_type_constraint($args->{$from}) - if $attribute->has_type_constraint; - - $instance->{$key} = $args->{$from}; - - weaken($instance->{$key}) - if ref($instance->{$key}) && $attribute->is_weak_ref; - } + weaken($instance->{$key}) + if ref($instance->{$key}) && $attribute->is_weak_ref; - if ($attribute->has_trigger && $attribute->trigger->{after}) { - $attribute->trigger->{after}->($instance, $args->{$from}, $attribute); + if ($attribute->has_trigger) { + $attribute->trigger->($instance, $args->{$from}, $attribute); } } else { diff --git a/t/016-trigger.t b/t/016-trigger.t index 312e9f2..223e387 100644 --- a/t/016-trigger.t +++ b/t/016-trigger.t @@ -1,7 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 21; +use Test::More tests => 11; use Test::Exception; my @trigger; @@ -50,69 +50,3 @@ my $object2 = Class->new(attr => 100); is(@trigger, 1, "trigger was called on new with the attribute specified"); is_deeply([splice @trigger], [[$object2, 100, $object2->meta->get_attribute('attr')]], "correct arguments to trigger in the constructor"); -do { - package Parent; - use Mouse; - - has attr => ( - is => 'rw', - trigger => { - before => sub { - push @trigger, ['before', @_]; - }, - after => sub { - push @trigger, ['after', @_]; - }, - around => sub { - my $code = shift; - my ($self, $value, $attr) = @_; - - push @trigger, ['around-before', $self, $value, $attr]; - $code->($self, 4 * $value, $attr); - push @trigger, ['around-after', $self, $value, $attr]; - }, - }, - ); - - package Child; - use Mouse; - extends 'Parent'; - - has '+attr' => ( - default => 10, - ); -}; - -my $child = Child->new; -is(@trigger, 0, "trigger not called on constructor with default"); - -is($child->attr, 10, "reader"); -is(@trigger, 0, "trigger not called on reader"); - -is($child->attr(5), 20, "writer"); -is_deeply([splice @trigger], [ - ['before', $child, 5, Child->meta->get_attribute('attr')], - ['around-before', $child, 5, Child->meta->get_attribute('attr')], - ['around-after', $child, 5, Child->meta->get_attribute('attr')], - ['after', $child, 20, Child->meta->get_attribute('attr')], -]); - -my $parent = Parent->new(attr => 2); -is_deeply([splice @trigger], [ - ['before', $parent, 2, Parent->meta->get_attribute('attr')], - ['around-before', $parent, 2, Parent->meta->get_attribute('attr')], - ['around-after', $parent, 2, Parent->meta->get_attribute('attr')], - ['after', $parent, 8, Parent->meta->get_attribute('attr')], -]); - -is($parent->attr, 8, "reader"); -is(@trigger, 0, "trigger not called on reader"); - -is($parent->attr(10), 40, "writer"); -is_deeply([splice @trigger], [ - ['before', $parent, 10, Parent->meta->get_attribute('attr')], - ['around-before', $parent, 10, Parent->meta->get_attribute('attr')], - ['around-after', $parent, 10, Parent->meta->get_attribute('attr')], - ['after', $parent, 40, Parent->meta->get_attribute('attr')], -]); -