BUILDARGS exception
[gitmo/Moo.git] / lib / Method / Generate / Constructor.pm
CommitLineData
6f68f022 1package Method::Generate::Constructor;
2
3use strictures 1;
4use Sub::Quote;
b1eebd55 5use base qw(Moo::Object);
14f32032 6use Sub::Defer;
a16d301e 7use B 'perlstring';
6f68f022 8
96d3f07a 9sub register_attribute_specs {
10 my ($self, %spec) = @_;
11 @{$self->{attribute_specs}||={}}{keys %spec} = values %spec;
12 $self;
13}
14
15sub all_attribute_specs {
16 $_[0]->{attribute_specs}
14f32032 17}
18
a16d301e 19sub accessor_generator {
20 $_[0]->{accessor_generator}
21}
22
de5c0e53 23sub construction_string {
24 my ($self) = @_;
25 $self->{construction_string} or 'bless({}, $class);'
26}
27
14f32032 28sub install_delayed {
29 my ($self) = @_;
30 my $package = $self->{package};
31 defer_sub "${package}::new" => sub {
32 unquote_sub $self->generate_method(
33 $package, 'new', $self->{attribute_specs}, { no_install => 1 }
34 )
35 };
36 $self;
37}
6f68f022 38
39sub generate_method {
40 my ($self, $into, $name, $spec, $quote_opts) = @_;
41 foreach my $no_init (grep !exists($spec->{$_}{init_arg}), keys %$spec) {
42 $spec->{$no_init}{init_arg} = $no_init;
43 }
a16d301e 44 local $self->{captures} = {};
301616bb 45 my $body = ' my $class = shift;'."\n"
46 .' $class = ref($class) if ref($class);'."\n";
e0e12d16 47 $body .= $self->_handle_subconstructor($into, $name);
0123201b 48 my $into_buildargs = $into->can('BUILDARGS');
49 if ( $into_buildargs && $into_buildargs != \&Moo::Object::BUILDARGS ) {
eae70e33 50 $body .= $self->_generate_args_via_buildargs;
51 } else {
52 $body .= $self->_generate_args;
53 }
6f68f022 54 $body .= $self->_check_required($spec);
de5c0e53 55 $body .= ' my $new = '.$self->construction_string.";\n";
6f68f022 56 $body .= $self->_assign_new($spec);
077bd026 57 if ($into->can('BUILD')) {
faa9ce11 58 require Method::Generate::BuildAll;
077bd026 59 $body .= Method::Generate::BuildAll->new->buildall_body_for(
60 $into, '$new', '$args'
61 );
62 }
4053679e 63 $body .= ' return $new;'."\n";
31eec6e8 64 if ($into->can('DEMOLISH')) {
7568ba55 65 require Method::Generate::DemolishAll;
31eec6e8 66 Method::Generate::DemolishAll->new->generate_method($into);
67 }
6f68f022 68 quote_sub
4053679e 69 "${into}::${name}" => $body,
a16d301e 70 $self->{captures}, $quote_opts||{}
6f68f022 71 ;
72}
73
e0e12d16 74sub _handle_subconstructor {
75 my ($self, $into, $name) = @_;
76ab3977 76 if (my $gen = $self->{subconstructor_handler}) {
e0e12d16 77 ' if ($class ne '.perlstring($into).') {'."\n".
76ab3977 78 $gen.
e0e12d16 79 ' }'."\n";
80 } else {
81 ''
82 }
83}
84
3a9a65a4 85sub _cap_call {
86 my ($self, $code, $captures) = @_;
87 @{$self->{captures}}{keys %$captures} = values %$captures if $captures;
88 $code;
89}
90
eae70e33 91sub _generate_args_via_buildargs {
6f68f022 92 my ($self) = @_;
09233107 93 q{ my $args = $class->BUILDARGS(@_);}."\n"
94 .q{ die "BUILDARGS did not return a hashref" unless ref($args) eq 'HASH';}
95 ."\n";
6f68f022 96}
97
0123201b 98# inlined from Moo::Object - update that first.
eae70e33 99sub _generate_args {
100 my ($self) = @_;
0123201b 101 return <<'_EOA';
102 my $args;
103 if ( scalar @_ == 1 ) {
104 unless ( defined $_[0] && ref $_[0] eq 'HASH' ) {
105 die "Single parameters to new() must be a HASH ref"
106 ." data => ". $_[0] ."\n";
107 }
108 $args = { %{ $_[0] } };
109 }
110 elsif ( @_ % 2 ) {
111 die "The new() method for $class expects a hash reference or a key/value list."
112 . " You passed an odd number of arguments\n";
113 }
114 else {
115 $args = {@_};
116 }
117_EOA
118
eae70e33 119}
120
6f68f022 121sub _assign_new {
122 my ($self, $spec) = @_;
46389f86 123 my (@init, @slots, %test);
3a9a65a4 124 my $ag = $self->accessor_generator;
46389f86 125 NAME: foreach my $name (sort keys %$spec) {
6f68f022 126 my $attr_spec = $spec->{$name};
3a9a65a4 127 unless ($ag->is_simple_attribute($name, $attr_spec)) {
d02da2bc 128 next NAME unless defined($attr_spec->{init_arg})
4ced3a94 129 or $ag->has_eager_default($name, $attr_spec);
d02da2bc 130 $test{$name} = $attr_spec->{init_arg};
46389f86 131 next NAME;
132 }
d02da2bc 133 next NAME unless defined(my $i = $attr_spec->{init_arg});
46389f86 134 push @init, $i;
6f68f022 135 push @slots, $name;
136 }
649ac264 137 return '' unless @init or %test;
46389f86 138 join '', (
139 @init
3a9a65a4 140 ? ' '.$self->_cap_call($ag->generate_multi_set(
5d349892 141 '$new', [ @slots ], '@{$args}{qw('.join(' ',@init).')}'
142 )).";\n"
46389f86 143 : ''
144 ), map {
145 my $arg_key = perlstring($test{$_});
649ac264 146 my $test = "exists \$args->{$arg_key}";
147 my $source = "\$args->{$arg_key}";
148 my $attr_spec = $spec->{$_};
3a9a65a4 149 $self->_cap_call($ag->generate_populate_set(
150 '$new', $_, $attr_spec, $source, $test
151 ));
46389f86 152 } sort keys %test;
6f68f022 153}
154
155sub _check_required {
156 my ($self, $spec) = @_;
157 my @required_init =
158 map $spec->{$_}{init_arg},
159 grep $spec->{$_}{required},
6d377074 160 sort keys %$spec;
6f68f022 161 return '' unless @required_init;
162 ' if (my @missing = grep !exists $args->{$_}, qw('
163 .join(' ',@required_init).')) {'."\n"
164 .q{ die "Missing required arguments: ".join(', ', sort @missing);}."\n"
165 ." }\n";
166}
167
6d377074 168sub _check_isa {
169 my ($self, $spec) = @_;
170 my $acc = $self->accessor_generator;
171 my $captures = $self->{captures};
172 my $check = '';
173 foreach my $name (sort keys %$spec) {
174 my ($init, $isa) = @{$spec->{$name}}{qw(init_arg isa)};
175 next unless $init and $isa;
176 my $init_str = perlstring($init);
177 my ($code, $add_captures) = $acc->generate_isa_check(
178 $name, "\$args->{${init_str}}", $isa
179 );
180 @{$captures}{keys %$add_captures} = values %$add_captures;
649ac264 181 $check .= " ${code}".(
182 (not($spec->{lazy}) and ($spec->{default} or $spec->{builder})
183 ? ";\n"
184 : "if exists \$args->{${init_str}};\n"
185 )
186 );
6d377074 187 }
188 return $check;
189}
190
a16d301e 191sub _fire_triggers {
192 my ($self, $spec) = @_;
a16d301e 193 my $acc = $self->accessor_generator;
194 my $captures = $self->{captures};
195 my $fire = '';
6d377074 196 foreach my $name (sort keys %$spec) {
a16d301e 197 my ($init, $trigger) = @{$spec->{$name}}{qw(init_arg trigger)};
198 next unless $init && $trigger;
199 my ($code, $add_captures) = $acc->generate_trigger(
200 $name, '$new', $acc->generate_simple_get('$new', $name), $trigger
201 );
202 @{$captures}{keys %$add_captures} = values %$add_captures;
203 $fire .= " ${code} if exists \$args->{${\perlstring $init}};\n";
204 }
205 return $fire;
206}
207
6f68f022 2081;