Include both attribute name and init_arg in constructor errors (RT#79596)
[gitmo/Moo.git] / lib / Method / Generate / Constructor.pm
1 package Method::Generate::Constructor;
2
3 use strictures 1;
4 use Sub::Quote;
5 use base qw(Moo::Object);
6 use Sub::Defer;
7 use B 'perlstring';
8
9 sub register_attribute_specs {
10   my ($self, @new_specs) = @_;
11   my $specs = $self->{attribute_specs}||={};
12   while (my ($name, $new_spec) = splice @new_specs, 0, 2) {
13     if ($name =~ s/^\+//) {
14       die "has '+${name}' given but no ${name} attribute already exists"
15         unless my $old_spec = $specs->{$name};
16       foreach my $key (keys %$old_spec) {
17         $new_spec->{$key} = $old_spec->{$key}
18           unless exists $new_spec->{$key};
19       }
20     }
21     $new_spec->{index} = scalar keys %$specs
22       unless defined $new_spec->{index};
23     $specs->{$name} = $new_spec;
24   }
25   $self;
26 }
27
28 sub all_attribute_specs {
29   $_[0]->{attribute_specs}
30 }
31
32 sub accessor_generator {
33   $_[0]->{accessor_generator}
34 }
35
36 sub construction_string {
37   my ($self) = @_;
38   $self->{construction_string}
39     or 'bless('
40        .$self->accessor_generator->default_construction_string
41        .', $class);'
42 }
43
44 sub install_delayed {
45   my ($self) = @_;
46   my $package = $self->{package};
47   defer_sub "${package}::new" => sub {
48     unquote_sub $self->generate_method(
49       $package, 'new', $self->{attribute_specs}, { no_install => 1 }
50     )
51   };
52   $self;
53 }
54
55 sub generate_method {
56   my ($self, $into, $name, $spec, $quote_opts) = @_;
57   foreach my $no_init (grep !exists($spec->{$_}{init_arg}), keys %$spec) {
58     $spec->{$no_init}{init_arg} = $no_init;
59   }
60   local $self->{captures} = {};
61   my $body = '    my $class = shift;'."\n"
62             .'    $class = ref($class) if ref($class);'."\n";
63   $body .= $self->_handle_subconstructor($into, $name);
64   my $into_buildargs = $into->can('BUILDARGS');
65   if ( $into_buildargs && $into_buildargs != \&Moo::Object::BUILDARGS ) {
66       $body .= $self->_generate_args_via_buildargs;
67   } else {
68       $body .= $self->_generate_args;
69   }
70   $body .= $self->_check_required($spec);
71   $body .= '    my $new = '.$self->construction_string.";\n";
72   $body .= $self->_assign_new($spec);
73   if ($into->can('BUILD')) {
74     require Method::Generate::BuildAll;
75     $body .= Method::Generate::BuildAll->new->buildall_body_for(
76       $into, '$new', '$args'
77     );
78   }
79   $body .= '    return $new;'."\n";
80   if ($into->can('DEMOLISH')) {
81     require Method::Generate::DemolishAll;
82     Method::Generate::DemolishAll->new->generate_method($into);
83   }
84   quote_sub
85     "${into}::${name}" => $body,
86     $self->{captures}, $quote_opts||{}
87   ;
88 }
89
90 sub _handle_subconstructor {
91   my ($self, $into, $name) = @_;
92   if (my $gen = $self->{subconstructor_handler}) {
93     '    if ($class ne '.perlstring($into).') {'."\n".
94     $gen.
95     '    }'."\n";
96   } else {
97     ''
98   }
99 }
100
101 sub _cap_call {
102   my ($self, $code, $captures) = @_;
103   @{$self->{captures}}{keys %$captures} = values %$captures if $captures;
104   $code;
105 }
106
107 sub _generate_args_via_buildargs {
108   my ($self) = @_;
109   q{    my $args = $class->BUILDARGS(@_);}."\n"
110   .q{    die "BUILDARGS did not return a hashref" unless ref($args) eq 'HASH';}
111   ."\n";
112 }
113
114 # inlined from Moo::Object - update that first.
115 sub _generate_args {
116   my ($self) = @_;
117   return <<'_EOA';
118     my $args;
119     if ( scalar @_ == 1 ) {
120         unless ( defined $_[0] && ref $_[0] eq 'HASH' ) {
121             die "Single parameters to new() must be a HASH ref"
122                 ." data => ". $_[0] ."\n";
123         }
124         $args = { %{ $_[0] } };
125     }
126     elsif ( @_ % 2 ) {
127         die "The new() method for $class expects a hash reference or a key/value list."
128                 . " You passed an odd number of arguments\n";
129     }
130     else {
131         $args = {@_};
132     }
133 _EOA
134
135 }
136
137 sub _assign_new {
138   my ($self, $spec) = @_;
139   my $ag = $self->accessor_generator;
140   my %test;
141   NAME: foreach my $name (sort keys %$spec) {
142     my $attr_spec = $spec->{$name};
143     next NAME unless defined($attr_spec->{init_arg})
144                        or $ag->has_eager_default($name, $attr_spec);
145     $test{$name} = $attr_spec->{init_arg};
146   }
147   join '', map {
148     my $arg_key = perlstring($test{$_});
149     my $test = "exists \$args->{$arg_key}";
150     my $source = "\$args->{$arg_key}";
151     my $attr_spec = $spec->{$_};
152     $self->_cap_call($ag->generate_populate_set(
153       '$new', $_, $attr_spec, $source, $test, $test{$_},
154     ));
155   } sort keys %test;
156 }
157
158 sub _check_required {
159   my ($self, $spec) = @_;
160   my @required_init =
161     map $spec->{$_}{init_arg},
162       grep {
163         my %s = %{$spec->{$_}}; # ignore required if default or builder set
164         $s{required} and not($s{builder} or $s{default})
165       } sort keys %$spec;
166   return '' unless @required_init;
167   '    if (my @missing = grep !exists $args->{$_}, qw('
168     .join(' ',@required_init).')) {'."\n"
169     .q{      die "Missing required arguments: ".join(', ', sort @missing);}."\n"
170     ."    }\n";
171 }
172
173 1;