error if we have a lazy attr with no default or builder
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / Array / insert.pm
CommitLineData
a7821be5 1package Moose::Meta::Method::Accessor::Native::Array::insert;
2
3use strict;
4use warnings;
5
245478d5 6our $VERSION = '1.19';
a7821be5 7$VERSION = eval $VERSION;
8our $AUTHORITY = 'cpan:STEVAN';
9
8b9641b8 10use Moose::Role;
11
12with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => {
13 -excludes => [
14 qw(
15 _minimum_arguments
16 _maximum_arguments
53a4677c 17 _inline_coerce_new_values
8b9641b8 18 _new_members
53a4677c 19 _optimized_set_new_value
7f5ec80d 20 _return_value
8b9641b8 21 )
22 ]
23};
a7821be5 24
25sub _minimum_arguments { 2 }
26
27sub _maximum_arguments { 2 }
28
29sub _adds_members { 1 }
30
31sub _potential_value {
53a4677c 32 my $self = shift;
33 my ($slot_access) = @_;
a7821be5 34
53a4677c 35 return '(do { '
36 . 'my @potential = @{ (' . $slot_access . ') }; '
37 . 'splice @potential, $_[0], 0, $_[1]; '
38 . '\@potential; '
39 . '})';
a7821be5 40}
41
7bf5e58d 42# We need to override this because while @_ can be written to, we cannot write
43# directly to $_[1].
53a4677c 44sub _inline_coerce_new_values {
7bf5e58d 45 my $self = shift;
46
53a4677c 47 return unless $self->associated_attribute->should_coerce;
7bf5e58d 48
53a4677c 49 return unless $self->_tc_member_type_can_coerce;
7bf5e58d 50
53a4677c 51 return '@_ = ($_[0], $member_tc_obj->coerce($_[1]));';
7bf5e58d 52};
53
44babf1f 54sub _new_members { '$_[1]' }
a7821be5 55
53a4677c 56sub _optimized_set_new_value {
57 my $self = shift;
58 my ($inv, $new, $slot_access) = @_;
e32b7489 59
53a4677c 60 return 'splice @{ (' . $slot_access . ') }, $_[0], 0, $_[1]';
e32b7489 61}
62
7f5ec80d 63sub _return_value {
53a4677c 64 my $self = shift;
65 my ($slot_access) = @_;
7f5ec80d 66
53a4677c 67 return $slot_access . '->[ $_[0] ]';
7f5ec80d 68}
69
8b9641b8 70no Moose::Role;
71
a7821be5 721;