1 package Method::Generate::Constructor;
5 use base qw(Class::Tiny::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}
25 my $package = $self->{package};
26 defer_sub "${package}::new" => sub {
27 unquote_sub $self->generate_method(
28 $package, 'new', $self->{attribute_specs}, { no_install => 1 }
35 my ($self, $into, $name, $spec, $quote_opts) = @_;
36 foreach my $no_init (grep !exists($spec->{$_}{init_arg}), keys %$spec) {
37 $spec->{$no_init}{init_arg} = $no_init;
39 local $self->{captures} = {};
40 my $body = ' my $class = shift;'."\n";
41 $body .= $self->_generate_args;
42 $body .= $self->_check_required($spec);
43 $body .= $self->_check_isa($spec);
44 $body .= ' my $new = bless({}, $class);'."\n";
45 $body .= $self->_assign_new($spec);
46 $body .= $self->_fire_triggers($spec);
47 if ($into->can('BUILD')) {
48 require Method::Generate::BuildAll;
49 $body .= Method::Generate::BuildAll->new->buildall_body_for(
50 $into, '$new', '$args'
53 $body .= ' return $new;'."\n";
55 "${into}::${name}" => $body,
56 $self->{captures}, $quote_opts||{}
62 q{ my $args = ref($_[0]) eq 'HASH' ? $_[0] : { @_ };}."\n";
66 my ($self, $spec) = @_;
67 my (@init, @slots, %test);
68 NAME: foreach my $name (sort keys %$spec) {
69 my $attr_spec = $spec->{$name};
70 next NAME unless defined(my $i = $attr_spec->{init_arg});
71 if ($attr_spec->{lazy} or $attr_spec->{default} or $attr_spec->{builder}) {
78 return '' unless @init or %test;
81 ? ' @{$new}{qw('.join(' ',@slots).')}'
82 .' = @{$args}{qw('.join(' ',@init).')};'."\n"
85 my $arg_key = perlstring($test{$_});
86 my $ag = $self->accessor_generator;
87 my $test = "exists \$args->{$arg_key}";
88 my $source = "\$args->{$arg_key}";
89 my $attr_spec = $spec->{$_};
90 my ($code, $add_captures);
91 if (!$attr_spec->{lazy} and
92 ($attr_spec->{default} or $attr_spec->{builder})) {
94 ($code, $add_captures) = $ag->generate_simple_set(
96 "(\n ${test}\n ? ${source}\n : "
98 (my $get, $get_captures) = $ag->generate_get_default(
99 '$new', $_, $attr_spec
105 @{$add_captures}{keys %$get_captures} = values %$get_captures;
108 ($code, $add_captures) = $ag->generate_simple_set(
109 '$new', $_, "\$args->{$arg_key}"
111 $code .= " if ${test};\n";
113 @{$self->{captures}}{keys %$add_captures} = values %$add_captures;
118 sub _check_required {
119 my ($self, $spec) = @_;
121 map $spec->{$_}{init_arg},
122 grep $spec->{$_}{required},
124 return '' unless @required_init;
125 ' if (my @missing = grep !exists $args->{$_}, qw('
126 .join(' ',@required_init).')) {'."\n"
127 .q{ die "Missing required arguments: ".join(', ', sort @missing);}."\n"
132 my ($self, $spec) = @_;
133 my $acc = $self->accessor_generator;
134 my $captures = $self->{captures};
136 foreach my $name (sort keys %$spec) {
137 my ($init, $isa) = @{$spec->{$name}}{qw(init_arg isa)};
138 next unless $init and $isa;
139 my $init_str = perlstring($init);
140 my ($code, $add_captures) = $acc->generate_isa_check(
141 $name, "\$args->{${init_str}}", $isa
143 @{$captures}{keys %$add_captures} = values %$add_captures;
144 $check .= " ${code}".(
145 (not($spec->{lazy}) and ($spec->{default} or $spec->{builder})
147 : "if exists \$args->{${init_str}};\n"
155 my ($self, $spec) = @_;
156 my $acc = $self->accessor_generator;
157 my $captures = $self->{captures};
159 foreach my $name (sort keys %$spec) {
160 my ($init, $trigger) = @{$spec->{$name}}{qw(init_arg trigger)};
161 next unless $init && $trigger;
162 my ($code, $add_captures) = $acc->generate_trigger(
163 $name, '$new', $acc->generate_simple_get('$new', $name), $trigger
165 @{$captures}{keys %$add_captures} = values %$add_captures;
166 $fire .= " ${code} if exists \$args->{${\perlstring $init}};\n";