1 package Moose::Meta::Method::Accessor::Native;
6 use Carp qw( confess );
7 use Scalar::Util qw( blessed weaken );
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Moose::Meta::Method::Accessor', 'Moose::Meta::Method::Delegation';
19 die "Cannot instantiate a $class object directly"
20 if $class eq __PACKAGE__;
22 ( exists $options{attribute} )
23 || confess "You must supply an attribute to construct with";
25 ( blessed( $options{attribute} )
26 && $options{attribute}->isa('Class::MOP::Attribute') )
28 "You must supply an attribute which is a 'Class::MOP::Attribute' instance";
30 ( $options{package_name} && $options{name} )
31 || confess "You must supply the package_name and name parameters";
33 exists $options{curried_arguments}
34 || ( $options{curried_arguments} = [] );
36 ( $options{curried_arguments}
37 && ( 'ARRAY' eq ref $options{curried_arguments} ) )
39 'You must supply a curried_arguments which is an ARRAY reference';
41 $options{delegate_to_method} = lc( ( split /::/, $class)[-1] );
43 $options{definition_context} = $options{attribute}->definition_context;
45 my $self = $class->_new( \%options );
47 weaken( $self->{'attribute'} );
49 $self->_initialize_body;
56 my $options = @_ == 1 ? $_[0] : {@_};
58 return bless $options, $class;
61 sub _initialize_body {
64 $self->{'body'} = $self->_eval_code( $self->_generate_method );
69 sub _eval_environment {
72 my $env = $self->SUPER::_eval_environment;
74 $env->{'@curried'} = $self->curried_arguments;
79 sub _inline_curried_arguments {
82 return q{} unless @{ $self->curried_arguments };
84 return 'unshift @_, @curried;'
87 sub _inline_check_argument_count {
92 if ( my $min = $self->_minimum_arguments ) {
93 my $err_msg = sprintf(
94 q{"Cannot call %s without at least %s argument%s"},
95 $self->delegate_to_method,
97 ( $min == 1 ? q{} : 's' )
102 . $self->_inline_throw_error($err_msg)
103 . " unless \@_ >= $min;";
106 if ( defined( my $max = $self->_maximum_arguments ) ) {
107 my $err_msg = sprintf(
108 q{"Cannot call %s with %s argument%s"},
109 $self->delegate_to_method,
110 ( $max ? "more than $max" : 'any' ),
111 ( $max == 1 ? q{} : 's' )
116 . $self->_inline_throw_error($err_msg)
123 sub _minimum_arguments { 0 }
124 sub _maximum_arguments { undef }
126 sub _inline_check_arguments { q{} }