Beginning of dzilization
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / Array / splice.pm
1 package Moose::Meta::Method::Accessor::Native::Array::splice;
2
3 use strict;
4 use warnings;
5
6 our $AUTHORITY = 'cpan:STEVAN';
7
8 use Moose::Role;
9
10 with 'Moose::Meta::Method::Accessor::Native::Array::Writer' => {
11     -excludes => [
12         qw(
13             _minimum_arguments
14             _inline_process_arguments
15             _inline_check_arguments
16             _inline_optimized_set_new_value
17             _return_value
18             )
19     ]
20 };
21
22 sub _minimum_arguments { 1 }
23
24 sub _adds_members { 1 }
25
26 sub _inline_process_arguments {
27     return (
28         'my $idx = shift;',
29         'my $len = @_ ? shift : undef;',
30     );
31 }
32
33 sub _inline_check_arguments {
34     my $self = shift;
35
36     return (
37         $self->_inline_check_var_is_valid_index('$idx'),
38         'if (defined($len) && $len !~ /^-?\d+$/) {',
39             $self->_inline_throw_error(
40                 '"The length argument passed to splice must be an integer"',
41             ) . ';',
42         '}',
43     );
44 }
45
46 sub _potential_value {
47     my $self = shift;
48     my ($slot_access) = @_;
49
50     return '(do { '
51              . 'my @potential = @{ (' . $slot_access . ') }; '
52              . '@return = defined $len '
53                  . '? (splice @potential, $idx, $len, @_) '
54                  . ': (splice @potential, $idx); '
55                  . '\@potential;'
56          . '})';
57 }
58
59 sub _inline_optimized_set_new_value {
60     my $self = shift;
61     my ($inv, $new, $slot_access) = @_;
62
63     return (
64         '@return = defined $len',
65             '? (splice @{ (' . $slot_access . ') }, $idx, $len, @_)',
66             ': (splice @{ (' . $slot_access . ') }, $idx);',
67     );
68 }
69
70 sub _return_value {
71     my $self = shift;
72     my ($slot_access) = @_;
73
74     return 'wantarray ? @return : $return[-1]';
75 }
76
77 no Moose::Role;
78
79 1;