Add support for Type in native traits
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / Writer.pm
index de127f9..2b55aae 100644 (file)
@@ -4,10 +4,7 @@ use strict;
 use warnings;
 
 use List::MoreUtils qw( any );
-
-our $VERSION = '1.19';
-$VERSION = eval $VERSION;
-our $AUTHORITY = 'cpan:STEVAN';
+use Moose::Util;
 
 use Moose::Role;
 
@@ -19,20 +16,18 @@ sub _generate_method {
     my $self = shift;
 
     my $inv         = '$self';
-    my $slot_access = $self->_inline_get($inv);
+    my $slot_access = $self->_get_value($inv);
 
     return (
         'sub {',
-            $self->_inline_pre_body(@_),
             'my ' . $inv . ' = shift;',
             $self->_inline_curried_arguments,
-            $self->_writer_core($inv, $slot_access),
-            $self->_inline_post_body(@_),
+            $self->_inline_writer_core($inv, $slot_access),
         '}',
     );
 }
 
-sub _writer_core {
+sub _inline_writer_core {
     my $self = shift;
     my ($inv, $slot_access) = @_;
 
@@ -44,7 +39,7 @@ sub _writer_core {
         $self->_inline_check_argument_count,
         $self->_inline_process_arguments($inv, $slot_access),
         $self->_inline_check_arguments('for writer'),
-        $self->_inline_check_lazy($inv),
+        $self->_inline_check_lazy($inv, '$type_constraint', '$type_coercion', '$type_message'),
     );
 
     if ($self->_return_value($slot_access)) {
@@ -56,7 +51,7 @@ sub _writer_core {
     push @code, (
         $self->_inline_coerce_new_values,
         $self->_inline_copy_native_value(\$potential),
-        $self->_inline_tc_code($potential),
+        $self->_inline_tc_code($potential, '$type_constraint', '$type_coercion', '$type_message'),
         $self->_inline_get_old_value_for_trigger($inv, $old),
         $self->_inline_capture_return_value($slot_access),
         $self->_inline_set_new_value($inv, $potential, $slot_access),
@@ -73,7 +68,7 @@ sub _inline_check_arguments { return }
 
 sub _inline_coerce_new_values { return }
 
-sub _value_needs_copy {
+sub _writer_value_needs_copy {
     my $self = shift;
 
     return $self->_constraint_must_be_checked;
@@ -91,52 +86,49 @@ sub _constraint_must_be_checked {
 
 sub _is_root_type {
     my $self = shift;
-    my ($type) = @_;
-
-    my $name = $type->name;
-
-    return any { $name eq $_ } @{ $self->root_types };
+    my $type = shift;
+
+    if (
+        Moose::Util::does_role( $type, 'Type::Constraint::Role::Interface' ) )
+    {
+        require Type::Library::Builtins;
+        return
+            any { $type->is_same_type_as( Type::Library::Builtins::t($_) ) }
+        @{ $self->root_types };
+    }
+    else {
+        my $name = $type->name;
+        return any { $name eq $_ } @{ $self->root_types };
+    }
 }
 
 sub _inline_copy_native_value {
     my $self = shift;
     my ($potential_ref) = @_;
 
-    return unless $self->_value_needs_copy;
+    return unless $self->_writer_value_needs_copy;
 
     my $code = 'my $potential = ' . ${$potential_ref} . ';';
 
     ${$potential_ref} = '$potential';
 
-    return ($code);
+    return $code;
 }
 
 around _inline_tc_code => sub {
     my $orig = shift;
     my $self = shift;
-    my ($value, $for_lazy) = @_;
+    my ($value, $tc, $coercion, $message, $for_lazy) = @_;
 
     return unless $for_lazy || $self->_constraint_must_be_checked;
 
     return $self->$orig(@_);
 };
 
-sub _inline_check_coercion {
-    my $self = shift;
-    my ($value) = @_;
-
-    my $attr = $self->associated_attribute;
-    return unless $attr->should_coerce && $attr->type_constraint->has_coercion;
-
-    # We want to break the aliasing in @_ in case the coercion tries to make a
-    # destructive change to an array member.
-    return ($value . ' = $type_constraint_obj->coerce(' . $value . ');');
-}
-
 around _inline_check_constraint => sub {
     my $orig = shift;
     my $self = shift;
-    my ($value, $for_lazy) = @_;
+    my ($value, $tc, $message, $for_lazy) = @_;
 
     return unless $for_lazy || $self->_constraint_must_be_checked;
 
@@ -145,32 +137,27 @@ around _inline_check_constraint => sub {
 
 sub _inline_capture_return_value { return }
 
-sub _set_new_value {
+sub _inline_set_new_value {
     my $self = shift;
 
-    return $self->_inline_store(@_)
-        if $self->_value_needs_copy
+    return $self->_inline_store_value(@_)
+        if $self->_writer_value_needs_copy
         || !$self->_slot_access_can_be_inlined
-        || !$self->_inline_get_is_lvalue;
+        || !$self->_get_is_lvalue;
 
-    return $self->_optimized_set_new_value(@_);
-}
-
-sub _inline_set_new_value {
-    my $self = shift;
-    return $self->_set_new_value(@_) . ';';
+    return $self->_inline_optimized_set_new_value(@_);
 }
 
-sub _inline_get_is_lvalue {
+sub _get_is_lvalue {
     my $self = shift;
 
     return $self->associated_attribute->associated_class->instance_metaclass->inline_get_is_lvalue;
 }
 
-sub _optimized_set_new_value {
+sub _inline_optimized_set_new_value {
     my $self = shift;
 
-    return $self->_inline_store(@_);
+    return $self->_inline_store_value(@_);
 }
 
 sub _return_value {