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