support has '+foo'
[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 {
02e9ef74 10 my ($self, @new_specs) = @_;
11 my $specs = $self->{attribute_specs}||={};
12 while (my ($name, $new_spec) = splice @new_specs, 0, 2) {
9f10cfe0 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 }
02e9ef74 21 $new_spec->{index} = scalar keys %$specs
57d402ef 22 unless defined $new_spec->{index};
02e9ef74 23 $specs->{$name} = $new_spec;
24 }
96d3f07a 25 $self;
26}
27
28sub all_attribute_specs {
29 $_[0]->{attribute_specs}
14f32032 30}
31
a16d301e 32sub accessor_generator {
33 $_[0]->{accessor_generator}
34}
35
de5c0e53 36sub construction_string {
37 my ($self) = @_;
02e9ef74 38 $self->{construction_string}
39 or 'bless('
40 .$self->accessor_generator->default_construction_string
41 .', $class);'
de5c0e53 42}
43
14f32032 44sub 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}
6f68f022 54
55sub 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 }
a16d301e 60 local $self->{captures} = {};
301616bb 61 my $body = ' my $class = shift;'."\n"
62 .' $class = ref($class) if ref($class);'."\n";
e0e12d16 63 $body .= $self->_handle_subconstructor($into, $name);
0123201b 64 my $into_buildargs = $into->can('BUILDARGS');
65 if ( $into_buildargs && $into_buildargs != \&Moo::Object::BUILDARGS ) {
eae70e33 66 $body .= $self->_generate_args_via_buildargs;
67 } else {
68 $body .= $self->_generate_args;
69 }
6f68f022 70 $body .= $self->_check_required($spec);
de5c0e53 71 $body .= ' my $new = '.$self->construction_string.";\n";
6f68f022 72 $body .= $self->_assign_new($spec);
077bd026 73 if ($into->can('BUILD')) {
faa9ce11 74 require Method::Generate::BuildAll;
077bd026 75 $body .= Method::Generate::BuildAll->new->buildall_body_for(
76 $into, '$new', '$args'
77 );
78 }
4053679e 79 $body .= ' return $new;'."\n";
31eec6e8 80 if ($into->can('DEMOLISH')) {
7568ba55 81 require Method::Generate::DemolishAll;
31eec6e8 82 Method::Generate::DemolishAll->new->generate_method($into);
83 }
6f68f022 84 quote_sub
4053679e 85 "${into}::${name}" => $body,
a16d301e 86 $self->{captures}, $quote_opts||{}
6f68f022 87 ;
88}
89
e0e12d16 90sub _handle_subconstructor {
91 my ($self, $into, $name) = @_;
76ab3977 92 if (my $gen = $self->{subconstructor_handler}) {
e0e12d16 93 ' if ($class ne '.perlstring($into).') {'."\n".
76ab3977 94 $gen.
e0e12d16 95 ' }'."\n";
96 } else {
97 ''
98 }
99}
100
3a9a65a4 101sub _cap_call {
102 my ($self, $code, $captures) = @_;
103 @{$self->{captures}}{keys %$captures} = values %$captures if $captures;
104 $code;
105}
106
eae70e33 107sub _generate_args_via_buildargs {
6f68f022 108 my ($self) = @_;
09233107 109 q{ my $args = $class->BUILDARGS(@_);}."\n"
110 .q{ die "BUILDARGS did not return a hashref" unless ref($args) eq 'HASH';}
111 ."\n";
6f68f022 112}
113
0123201b 114# inlined from Moo::Object - update that first.
eae70e33 115sub _generate_args {
116 my ($self) = @_;
0123201b 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
eae70e33 135}
136
6f68f022 137sub _assign_new {
138 my ($self, $spec) = @_;
46389f86 139 my (@init, @slots, %test);
3a9a65a4 140 my $ag = $self->accessor_generator;
46389f86 141 NAME: foreach my $name (sort keys %$spec) {
6f68f022 142 my $attr_spec = $spec->{$name};
3a9a65a4 143 unless ($ag->is_simple_attribute($name, $attr_spec)) {
d02da2bc 144 next NAME unless defined($attr_spec->{init_arg})
4ced3a94 145 or $ag->has_eager_default($name, $attr_spec);
d02da2bc 146 $test{$name} = $attr_spec->{init_arg};
46389f86 147 next NAME;
148 }
d02da2bc 149 next NAME unless defined(my $i = $attr_spec->{init_arg});
46389f86 150 push @init, $i;
6f68f022 151 push @slots, $name;
152 }
649ac264 153 return '' unless @init or %test;
46389f86 154 join '', (
155 @init
3a9a65a4 156 ? ' '.$self->_cap_call($ag->generate_multi_set(
ceea0e38 157 '$new', [ @slots ], '@{$args}{qw('.join(' ',@init).')}', $spec
5d349892 158 )).";\n"
46389f86 159 : ''
160 ), map {
161 my $arg_key = perlstring($test{$_});
649ac264 162 my $test = "exists \$args->{$arg_key}";
163 my $source = "\$args->{$arg_key}";
164 my $attr_spec = $spec->{$_};
3a9a65a4 165 $self->_cap_call($ag->generate_populate_set(
166 '$new', $_, $attr_spec, $source, $test
167 ));
46389f86 168 } sort keys %test;
6f68f022 169}
170
171sub _check_required {
172 my ($self, $spec) = @_;
173 my @required_init =
174 map $spec->{$_}{init_arg},
175 grep $spec->{$_}{required},
6d377074 176 sort keys %$spec;
6f68f022 177 return '' unless @required_init;
178 ' if (my @missing = grep !exists $args->{$_}, qw('
179 .join(' ',@required_init).')) {'."\n"
180 .q{ die "Missing required arguments: ".join(', ', sort @missing);}."\n"
181 ." }\n";
182}
183
6d377074 184sub _check_isa {
185 my ($self, $spec) = @_;
186 my $acc = $self->accessor_generator;
187 my $captures = $self->{captures};
188 my $check = '';
189 foreach my $name (sort keys %$spec) {
190 my ($init, $isa) = @{$spec->{$name}}{qw(init_arg isa)};
191 next unless $init and $isa;
192 my $init_str = perlstring($init);
193 my ($code, $add_captures) = $acc->generate_isa_check(
194 $name, "\$args->{${init_str}}", $isa
195 );
196 @{$captures}{keys %$add_captures} = values %$add_captures;
649ac264 197 $check .= " ${code}".(
198 (not($spec->{lazy}) and ($spec->{default} or $spec->{builder})
199 ? ";\n"
200 : "if exists \$args->{${init_str}};\n"
201 )
202 );
6d377074 203 }
204 return $check;
205}
206
a16d301e 207sub _fire_triggers {
208 my ($self, $spec) = @_;
a16d301e 209 my $acc = $self->accessor_generator;
210 my $captures = $self->{captures};
211 my $fire = '';
6d377074 212 foreach my $name (sort keys %$spec) {
a16d301e 213 my ($init, $trigger) = @{$spec->{$name}}{qw(init_arg trigger)};
214 next unless $init && $trigger;
215 my ($code, $add_captures) = $acc->generate_trigger(
ceea0e38 216 $name, '$new', $acc->generate_simple_get('$new', $name, $spec), $trigger
a16d301e 217 );
218 @{$captures}{keys %$add_captures} = values %$add_captures;
219 $fire .= " ${code} if exists \$args->{${\perlstring $init}};\n";
220 }
221 return $fire;
222}
223
6f68f022 2241;