Add explicit return values for (almost) all native delegation mutating methods
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / Hash / set.pm
CommitLineData
44babf1f 1package Moose::Meta::Method::Accessor::Native::Hash::set;
2
3use strict;
4use warnings;
5
6use Scalar::Util qw( looks_like_number );
7
efa728b4 8our $VERSION = '1.15';
44babf1f 9$VERSION = eval $VERSION;
10our $AUTHORITY = 'cpan:STEVAN';
11
8b9641b8 12use Moose::Role;
13
14with 'Moose::Meta::Method::Accessor::Native::Hash::Writer' => {
15 -excludes => [
16 qw(
17 _minimum_arguments
18 _maximum_arguments
19 _inline_process_arguments
20 _inline_check_arguments
21 _inline_optimized_set_new_value
7f5ec80d 22 _return_value
8b9641b8 23 )
24 ],
25};
44babf1f 26
27sub _minimum_arguments { 2 }
28
29sub _maximum_arguments { undef }
30
8b9641b8 31around _inline_check_argument_count => sub {
32 my $orig = shift;
44babf1f 33 my $self = shift;
34
35 return
8b9641b8 36 $self->$orig(@_) . "\n"
44babf1f 37 . $self->_inline_throw_error(
38 q{'You must pass an even number of arguments to set'})
39 . ' if @_ % 2;';
8b9641b8 40};
44babf1f 41
42sub _inline_process_arguments {
43 my $self = shift;
44
45 return 'my @keys_idx = grep { ! ($_ % 2) } 0..$#_;' . "\n"
46 . 'my @values_idx = grep { $_ % 2 } 0..$#_;';
47}
48
49sub _inline_check_arguments {
50 my $self = shift;
51
52 return
53 'for (@keys_idx) {' . "\n"
54 . $self->_inline_throw_error(
55 q{'Hash keys passed to set must be defined'})
56 . ' unless defined $_[$_];' . "\n" . '}';
57}
58
59sub _adds_members { 1 }
60
61sub _potential_value {
62 my ( $self, $slot_access ) = @_;
63
fe4e0a8c 64 return "{ %{ $slot_access }, \@_ }";
44babf1f 65}
66
67sub _new_members { '@_[ @values_idx ]' }
68
69sub _inline_optimized_set_new_value {
70 my ( $self, $inv, $new, $slot_access ) = @_;
71
584540d9 72 return "\@{ $slot_access }{ \@_[ \@keys_idx] } = \@_[ \@values_idx ]";
44babf1f 73}
74
7f5ec80d 75sub _return_value {
76 my ( $self, $slot_access ) = @_;
77
78 return "return wantarray ? \@{ $slot_access }{ \@_[ \@keys_idx ] } : ${slot_access}->{ \$_[ \$keys_idx[0] ] };";
79}
80
8b9641b8 81no Moose::Role;
82
44babf1f 831;