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 $body .= $self->_handle_subconstructor($into, $name);
47 my $into_buildargs = $into->can('BUILDARGS');
48 if ( $into_buildargs && $into_buildargs != \&Moo::Object::BUILDARGS ) {
49 $body .= $self->_generate_args_via_buildargs;
51 $body .= $self->_generate_args;
53 $body .= $self->_check_required($spec);
54 $body .= ' my $new = '.$self->construction_string.";\n";
55 $body .= $self->_assign_new($spec);
56 if ($into->can('BUILD')) {
57 require Method::Generate::BuildAll;
58 $body .= Method::Generate::BuildAll->new->buildall_body_for(
59 $into, '$new', '$args'
62 $body .= ' return $new;'."\n";
64 "${into}::${name}" => $body,
65 $self->{captures}, $quote_opts||{}
69 sub _handle_subconstructor {
70 my ($self, $into, $name) = @_;
71 if (my $gen = $self->{subconstructor_generator}) {
72 ' if ($class ne '.perlstring($into).') {'."\n".
74 ' return $class->'.$name.'(@_)'.";\n".
82 my ($self, $code, $captures) = @_;
83 @{$self->{captures}}{keys %$captures} = values %$captures if $captures;
87 sub _generate_args_via_buildargs {
89 q{ my $args = $class->BUILDARGS(@_);}."\n";
92 # inlined from Moo::Object - update that first.
97 if ( scalar @_ == 1 ) {
98 unless ( defined $_[0] && ref $_[0] eq 'HASH' ) {
99 die "Single parameters to new() must be a HASH ref"
100 ." data => ". $_[0] ."\n";
102 $args = { %{ $_[0] } };
105 die "The new() method for $class expects a hash reference or a key/value list."
106 . " You passed an odd number of arguments\n";
116 my ($self, $spec) = @_;
117 my (@init, @slots, %test);
118 my $ag = $self->accessor_generator;
119 NAME: foreach my $name (sort keys %$spec) {
120 my $attr_spec = $spec->{$name};
121 unless ($ag->is_simple_attribute($name, $attr_spec)) {
122 next NAME unless defined($attr_spec->{init_arg})
123 or $ag->has_eager_default($name, $attr_spec);
124 $test{$name} = $attr_spec->{init_arg};
127 next NAME unless defined(my $i = $attr_spec->{init_arg});
131 return '' unless @init or %test;
134 ? ' '.$self->_cap_call($ag->generate_multi_set(
135 '$new', [ @slots ], '@{$args}{qw('.join(' ',@init).')}'
139 my $arg_key = perlstring($test{$_});
140 my $test = "exists \$args->{$arg_key}";
141 my $source = "\$args->{$arg_key}";
142 my $attr_spec = $spec->{$_};
143 $self->_cap_call($ag->generate_populate_set(
144 '$new', $_, $attr_spec, $source, $test
149 sub _check_required {
150 my ($self, $spec) = @_;
152 map $spec->{$_}{init_arg},
153 grep $spec->{$_}{required},
155 return '' unless @required_init;
156 ' if (my @missing = grep !exists $args->{$_}, qw('
157 .join(' ',@required_init).')) {'."\n"
158 .q{ die "Missing required arguments: ".join(', ', sort @missing);}."\n"
163 my ($self, $spec) = @_;
164 my $acc = $self->accessor_generator;
165 my $captures = $self->{captures};
167 foreach my $name (sort keys %$spec) {
168 my ($init, $isa) = @{$spec->{$name}}{qw(init_arg isa)};
169 next unless $init and $isa;
170 my $init_str = perlstring($init);
171 my ($code, $add_captures) = $acc->generate_isa_check(
172 $name, "\$args->{${init_str}}", $isa
174 @{$captures}{keys %$add_captures} = values %$add_captures;
175 $check .= " ${code}".(
176 (not($spec->{lazy}) and ($spec->{default} or $spec->{builder})
178 : "if exists \$args->{${init_str}};\n"
186 my ($self, $spec) = @_;
187 my $acc = $self->accessor_generator;
188 my $captures = $self->{captures};
190 foreach my $name (sort keys %$spec) {
191 my ($init, $trigger) = @{$spec->{$name}}{qw(init_arg trigger)};
192 next unless $init && $trigger;
193 my ($code, $add_captures) = $acc->generate_trigger(
194 $name, '$new', $acc->generate_simple_get('$new', $name), $trigger
196 @{$captures}{keys %$add_captures} = values %$add_captures;
197 $fire .= " ${code} if exists \$args->{${\perlstring $init}};\n";