Implement passing the old value to a trigger when appropriate
[gitmo/Moose.git] / lib / Moose / Meta / Attribute.pm
index b734854..33ec298 100644 (file)
@@ -7,7 +7,7 @@ use warnings;
 use Scalar::Util 'blessed', 'weaken';
 use overload     ();
 
-our $VERSION   = '0.84';
+our $VERSION   = '0.88';
 our $AUTHORITY = 'cpan:STEVAN';
 
 use Moose::Meta::Method::Accessor;
@@ -163,7 +163,7 @@ my @legal_options_for_inheritance = qw(
     documentation lazy handles
     builder type_constraint
     definition_context
-    lazy_build
+    lazy_build weak_ref
 );
 
 sub legal_options_for_inheritance { @legal_options_for_inheritance }
@@ -495,6 +495,11 @@ sub set_value {
     my $meta_instance = Class::MOP::Class->initialize(blessed($instance))
                                          ->get_meta_instance;
 
+    my @old;
+    if ( $self->has_trigger && $self->has_value($instance) ) {
+        @old = $self->get_value($instance, 'for trigger');
+    }
+
     $meta_instance->set_slot_value($instance, $attr_name, $value);
 
     if (ref $value && $self->is_weak_ref) {
@@ -502,12 +507,12 @@ sub set_value {
     }
 
     if ($self->has_trigger) {
-        $self->trigger->($instance, $value);
+        $self->trigger->($instance, $value, @old);
     }
 }
 
 sub get_value {
-    my ($self, $instance) = @_;
+    my ($self, $instance, $for_trigger) = @_;
 
     if ($self->is_lazy) {
         unless ($self->has_value($instance)) {
@@ -524,7 +529,7 @@ sub get_value {
         }
     }
 
-    if ($self->should_auto_deref) {
+    if ( $self->should_auto_deref && ! $for_trigger ) {
 
         my $type_constraint = $self->type_constraint;
 
@@ -560,7 +565,7 @@ sub install_accessors {
     return;
 }
 
-sub check_associated_methods {
+sub _check_associated_methods {
     my $self = shift;
     unless (
         @{ $self->associated_methods }
@@ -585,7 +590,7 @@ sub _process_accessors {
      && (!$self->definition_context
       || $method->package_name eq $self->definition_context->{package})) {
         Carp::cluck(
-            "You cannot overwrite a locally defined method ($accessor) with "
+            "You are overwriting a locally defined method ($accessor) with "
           . "an accessor"
         );
     }
@@ -831,7 +836,7 @@ name as the attribute, and a C<writer> with the name you provided.
 Use 'bare' when you are deliberately not installing any methods
 (accessor, reader, etc.) associated with this attribute; otherwise,
 Moose will issue a deprecation warning when this attribute is added to a
-metaclass.  See L</check_associated_methods>.
+metaclass.
 
 =item * isa => $type
 
@@ -1063,13 +1068,6 @@ Given a value, this method returns true if the value is valid for the
 attribute's type constraint. If the value is not valid, it throws an
 error.
 
-=item B<< $attr->check_associated_methods >>
-
-This method makes sure that either an explicit C<< is => 'bare' >> was passed
-to the attribute's constructor or that the attribute has at least one
-associated method (reader, writer, delegation, etc.).  Otherwise, it issues a
-warning.
-
 =item B<< $attr->handles >>
 
 This returns the value of the C<handles> option passed to the