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