1 package Method::Generate::Constructor;
5 use base qw(Moo::Object);
9 sub register_attribute_specs {
10 my ($self, %spec) = @_;
11 @{$self->{attribute_specs}||={}}{keys %spec} = values %spec;
15 sub all_attribute_specs {
16 $_[0]->{attribute_specs}
19 sub accessor_generator {
20 $_[0]->{accessor_generator}
23 sub construction_string {
25 $self->{construction_string} or 'bless({}, $class);'
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 }
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;
44 local $self->{captures} = {};
45 my $body = ' my $class = shift;'."\n"
46 .' $class = ref($class) if ref($class);'."\n";
47 $body .= $self->_handle_subconstructor($into, $name);
48 my $into_buildargs = $into->can('BUILDARGS');
49 if ( $into_buildargs && $into_buildargs != \&Moo::Object::BUILDARGS ) {
50 $body .= $self->_generate_args_via_buildargs;
52 $body .= $self->_generate_args;
54 $body .= $self->_check_required($spec);
55 $body .= ' my $new = '.$self->construction_string.";\n";
56 $body .= $self->_assign_new($spec);
57 if ($into->can('BUILD')) {
58 require Method::Generate::BuildAll;
59 $body .= Method::Generate::BuildAll->new->buildall_body_for(
60 $into, '$new', '$args'
63 $body .= ' return $new;'."\n";
64 if ($into->can('DEMOLISH')) {
65 require Method::Generate::DemolishAll;
66 Method::Generate::DemolishAll->new->generate_method($into);
69 "${into}::${name}" => $body,
70 $self->{captures}, $quote_opts||{}
74 sub _handle_subconstructor {
75 my ($self, $into, $name) = @_;
76 if (my $gen = $self->{subconstructor_handler}) {
77 ' if ($class ne '.perlstring($into).') {'."\n".
86 my ($self, $code, $captures) = @_;
87 @{$self->{captures}}{keys %$captures} = values %$captures if $captures;
91 sub _generate_args_via_buildargs {
93 q{ my $args = $class->BUILDARGS(@_);}."\n"
94 .q{ die "BUILDARGS did not return a hashref" unless ref($args) eq 'HASH';}
98 # inlined from Moo::Object - update that first.
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";
108 $args = { %{ $_[0] } };
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";
122 my ($self, $spec) = @_;
123 my (@init, @slots, %test);
124 my $ag = $self->accessor_generator;
125 NAME: foreach my $name (sort keys %$spec) {
126 my $attr_spec = $spec->{$name};
127 unless ($ag->is_simple_attribute($name, $attr_spec)) {
128 next NAME unless defined($attr_spec->{init_arg})
129 or $ag->has_eager_default($name, $attr_spec);
130 $test{$name} = $attr_spec->{init_arg};
133 next NAME unless defined(my $i = $attr_spec->{init_arg});
137 return '' unless @init or %test;
140 ? ' '.$self->_cap_call($ag->generate_multi_set(
141 '$new', [ @slots ], '@{$args}{qw('.join(' ',@init).')}'
145 my $arg_key = perlstring($test{$_});
146 my $test = "exists \$args->{$arg_key}";
147 my $source = "\$args->{$arg_key}";
148 my $attr_spec = $spec->{$_};
149 $self->_cap_call($ag->generate_populate_set(
150 '$new', $_, $attr_spec, $source, $test
155 sub _check_required {
156 my ($self, $spec) = @_;
158 map $spec->{$_}{init_arg},
159 grep $spec->{$_}{required},
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"
169 my ($self, $spec) = @_;
170 my $acc = $self->accessor_generator;
171 my $captures = $self->{captures};
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
180 @{$captures}{keys %$add_captures} = values %$add_captures;
181 $check .= " ${code}".(
182 (not($spec->{lazy}) and ($spec->{default} or $spec->{builder})
184 : "if exists \$args->{${init_str}};\n"
192 my ($self, $spec) = @_;
193 my $acc = $self->accessor_generator;
194 my $captures = $self->{captures};
196 foreach my $name (sort keys %$spec) {
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
202 @{$captures}{keys %$add_captures} = values %$add_captures;
203 $fire .= " ${code} if exists \$args->{${\perlstring $init}};\n";