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->_generate_args;
47 $body .= $self->_check_required($spec);
48 $body .= ' my $new = '.$self->construction_string.";\n";
49 $body .= $self->_assign_new($spec);
50 if ($into->can('BUILD')) {
51 require Method::Generate::BuildAll;
52 $body .= Method::Generate::BuildAll->new->buildall_body_for(
53 $into, '$new', '$args'
56 $body .= ' return $new;'."\n";
58 "${into}::${name}" => $body,
59 $self->{captures}, $quote_opts||{}
64 my ($self, $code, $captures) = @_;
65 @{$self->{captures}}{keys %$captures} = values %$captures if $captures;
71 q{ my $args = ref($_[0]) eq 'HASH' ? $_[0] : { @_ };}."\n";
75 my ($self, $spec) = @_;
76 my (@init, @slots, %test);
77 my $ag = $self->accessor_generator;
78 NAME: foreach my $name (sort keys %$spec) {
79 my $attr_spec = $spec->{$name};
80 unless ($ag->is_simple_attribute($name, $attr_spec)) {
81 next NAME unless defined($attr_spec->{init_arg})
82 or (($attr_spec->{default} or $attr_spec->{builder})
83 and not $attr_spec->{lazy});
84 $test{$name} = $attr_spec->{init_arg};
87 next NAME unless defined(my $i = $attr_spec->{init_arg});
91 return '' unless @init or %test;
94 ? ' '.$self->_cap_call($ag->generate_multi_set(
95 '$new', [ @slots ], '@{$args}{qw('.join(' ',@init).')}'
99 my $arg_key = perlstring($test{$_});
100 my $test = "exists \$args->{$arg_key}";
101 my $source = "\$args->{$arg_key}";
102 my $attr_spec = $spec->{$_};
103 $self->_cap_call($ag->generate_populate_set(
104 '$new', $_, $attr_spec, $source, $test
109 sub _check_required {
110 my ($self, $spec) = @_;
112 map $spec->{$_}{init_arg},
113 grep $spec->{$_}{required},
115 return '' unless @required_init;
116 ' if (my @missing = grep !exists $args->{$_}, qw('
117 .join(' ',@required_init).')) {'."\n"
118 .q{ die "Missing required arguments: ".join(', ', sort @missing);}."\n"
123 my ($self, $spec) = @_;
124 my $acc = $self->accessor_generator;
125 my $captures = $self->{captures};
127 foreach my $name (sort keys %$spec) {
128 my ($init, $isa) = @{$spec->{$name}}{qw(init_arg isa)};
129 next unless $init and $isa;
130 my $init_str = perlstring($init);
131 my ($code, $add_captures) = $acc->generate_isa_check(
132 $name, "\$args->{${init_str}}", $isa
134 @{$captures}{keys %$add_captures} = values %$add_captures;
135 $check .= " ${code}".(
136 (not($spec->{lazy}) and ($spec->{default} or $spec->{builder})
138 : "if exists \$args->{${init_str}};\n"
146 my ($self, $spec) = @_;
147 my $acc = $self->accessor_generator;
148 my $captures = $self->{captures};
150 foreach my $name (sort keys %$spec) {
151 my ($init, $trigger) = @{$spec->{$name}}{qw(init_arg trigger)};
152 next unless $init && $trigger;
153 my ($code, $add_captures) = $acc->generate_trigger(
154 $name, '$new', $acc->generate_simple_get('$new', $name), $trigger
156 @{$captures}{keys %$add_captures} = values %$add_captures;
157 $fire .= " ${code} if exists \$args->{${\perlstring $init}};\n";