1 package Moose::Meta::Method::Accessor::Native;
6 use Carp qw( confess );
7 use Scalar::Util qw( blessed weaken );
9 our $AUTHORITY = 'cpan:STEVAN';
18 $options{curried_arguments} = []
19 unless exists $options{curried_arguments};
21 confess 'You must supply a curried_arguments which is an ARRAY reference'
22 unless $options{curried_arguments}
23 && ref($options{curried_arguments}) eq 'ARRAY';
25 $options{definition_context} = $options{attribute}->definition_context;
27 $options{accessor_type} = 'native';
29 return $class->$orig(%options);
34 my $options = @_ == 1 ? $_[0] : {@_};
36 return bless $options, $class;
39 sub root_types { (shift)->{'root_types'} }
41 sub _initialize_body {
44 $self->{'body'} = $self->_compile_code( [$self->_generate_method] );
49 sub _inline_curried_arguments {
52 return unless @{ $self->curried_arguments };
54 return 'unshift @_, @curried;';
57 sub _inline_check_argument_count {
62 if (my $min = $self->_minimum_arguments) {
64 'if (@_ < ' . $min . ') {',
65 $self->_inline_throw_error(
67 '"Cannot call %s without at least %s argument%s"',
68 $self->delegate_to_method,
70 ($min == 1 ? '' : 's'),
77 if (defined(my $max = $self->_maximum_arguments)) {
79 'if (@_ > ' . $max . ') {',
80 $self->_inline_throw_error(
82 '"Cannot call %s with %s argument%s"',
83 $self->delegate_to_method,
84 $max ? "more than $max" : 'any',
85 ($max == 1 ? '' : 's'),
95 sub _inline_return_value {
97 my ($slot_access, $for_writer) = @_;
99 return 'return ' . $self->_return_value($slot_access, $for_writer) . ';';
102 sub _minimum_arguments { 0 }
103 sub _maximum_arguments { undef }
105 override _get_value => sub {
109 return $self->_slot_access_can_be_inlined
111 : $instance . '->$reader';
114 override _inline_store_value => sub {
116 my ($instance, $value) = @_;
118 return $self->_slot_access_can_be_inlined
120 : $instance . '->$writer(' . $value . ');';
123 override _eval_environment => sub {
128 $env->{'@curried'} = $self->curried_arguments;
130 return $env if $self->_slot_access_can_be_inlined;
132 my $reader = $self->associated_attribute->get_read_method_ref;
133 $reader = $reader->body if blessed $reader;
135 $env->{'$reader'} = \$reader;
137 my $writer = $self->associated_attribute->get_write_method_ref;
138 $writer = $writer->body if blessed $writer;
140 $env->{'$writer'} = \$writer;
145 sub _slot_access_can_be_inlined {
148 return $self->is_inline && $self->_instance_is_inlinable;