Commit | Line | Data |
a7821be5 |
1 | package Moose::Meta::Method::Accessor::Native::Array::insert; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
245478d5 |
6 | our $VERSION = '1.19'; |
a7821be5 |
7 | $VERSION = eval $VERSION; |
8 | our $AUTHORITY = 'cpan:STEVAN'; |
9 | |
8b9641b8 |
10 | use Moose::Role; |
11 | |
12 | with '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 | |
25 | sub _minimum_arguments { 2 } |
26 | |
27 | sub _maximum_arguments { 2 } |
28 | |
29 | sub _adds_members { 1 } |
30 | |
31 | sub _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 |
44 | sub _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 |
54 | sub _new_members { '$_[1]' } |
a7821be5 |
55 | |
53a4677c |
56 | sub _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 |
63 | sub _return_value { |
53a4677c |
64 | my $self = shift; |
65 | my ($slot_access) = @_; |
7f5ec80d |
66 | |
53a4677c |
67 | return $slot_access . '->[ $_[0] ]'; |
7f5ec80d |
68 | } |
69 | |
8b9641b8 |
70 | no Moose::Role; |
71 | |
a7821be5 |
72 | 1; |