Optimize the get branch for hash accessors a little bit
[gitmo/Moose.git] / lib / Moose / Meta / Method / Accessor / Native / Hash / accessor.pm
1 package Moose::Meta::Method::Accessor::Native::Hash::accessor;
2
3 use strict;
4 use warnings;
5
6 use Moose::Role;
7
8 with 'Moose::Meta::Method::Accessor::Native::Hash::set' => {
9     -excludes => [
10         qw(
11             _generate_method
12             _minimum_arguments
13             _maximum_arguments
14             )
15     ]
16     },
17     'Moose::Meta::Method::Accessor::Native::Hash::get' => {
18     -excludes => [
19         qw(
20             _generate_method
21             _minimum_arguments
22             _maximum_arguments
23             _inline_check_argument_count
24             _inline_check_arguments
25             _inline_process_arguments
26             _return_value
27             )
28     ]
29     };
30
31 sub _generate_method {
32     my $self = shift;
33
34     my $inv         = '$self';
35     my $slot_access = $self->_get_value($inv);
36
37     return (
38         'sub {',
39             'my ' . $inv . ' = shift;',
40             $self->_inline_curried_arguments,
41             $self->_inline_check_lazy($inv, '$type_constraint', '$type_coercion', '$type_message'),
42             # get
43             'if (@_ == 1) {',
44                 # XXX: ugh, this is a hack - we need _return_value from
45                 # both ::set and ::get, but we can only have one, so we pick
46                 # the one from ::set and munge it to work for the ::get case
47                 # this should be fixed in a better way
48                 # -doy
49                 'my @keys_idx = 0;',
50                 $self->_inline_check_var_is_valid_key('$_[0]'),
51                 $slot_access . '->{$_[0]}',
52             '}',
53             # set
54             'else {',
55                 $self->_inline_writer_core($inv, $slot_access),
56             '}',
57         '}',
58     );
59 }
60
61 sub _minimum_arguments { 1 }
62 sub _maximum_arguments { 2 }
63
64 no Moose::Role;
65
66 1;