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 .= ' my $new = bless({}, $class);'."\n";
44 $body .= $self->_assign_new($spec);
45 if ($into->can('BUILD')) {
46 require Method::Generate::BuildAll;
47 $body .= Method::Generate::BuildAll->new->buildall_body_for(
48 $into, '$new', '$args'
51 $body .= ' return $new;'."\n";
53 "${into}::${name}" => $body,
54 $self->{captures}, $quote_opts||{}
59 my ($self, $code, $captures) = @_;
60 @{$self->{captures}}{keys %$captures} = values %$captures if $captures;
66 q{ my $args = ref($_[0]) eq 'HASH' ? $_[0] : { @_ };}."\n";
70 my ($self, $spec) = @_;
71 my (@init, @slots, %test);
72 my $ag = $self->accessor_generator;
73 NAME: foreach my $name (sort keys %$spec) {
74 my $attr_spec = $spec->{$name};
75 next NAME unless defined(my $i = $attr_spec->{init_arg});
76 unless ($ag->is_simple_attribute($name, $attr_spec)) {
83 return '' unless @init or %test;
86 ? ' '.$self->_cap_call($ag->generate_multi_set(
87 '$new', [ @slots ], '@{$args}{qw('.join(' ',@init).')}'
91 my $arg_key = perlstring($test{$_});
92 my $test = "exists \$args->{$arg_key}";
93 my $source = "\$args->{$arg_key}";
94 my $attr_spec = $spec->{$_};
95 $self->_cap_call($ag->generate_populate_set(
96 '$new', $_, $attr_spec, $source, $test
101 sub _check_required {
102 my ($self, $spec) = @_;
104 map $spec->{$_}{init_arg},
105 grep $spec->{$_}{required},
107 return '' unless @required_init;
108 ' if (my @missing = grep !exists $args->{$_}, qw('
109 .join(' ',@required_init).')) {'."\n"
110 .q{ die "Missing required arguments: ".join(', ', sort @missing);}."\n"
115 my ($self, $spec) = @_;
116 my $acc = $self->accessor_generator;
117 my $captures = $self->{captures};
119 foreach my $name (sort keys %$spec) {
120 my ($init, $isa) = @{$spec->{$name}}{qw(init_arg isa)};
121 next unless $init and $isa;
122 my $init_str = perlstring($init);
123 my ($code, $add_captures) = $acc->generate_isa_check(
124 $name, "\$args->{${init_str}}", $isa
126 @{$captures}{keys %$add_captures} = values %$add_captures;
127 $check .= " ${code}".(
128 (not($spec->{lazy}) and ($spec->{default} or $spec->{builder})
130 : "if exists \$args->{${init_str}};\n"
138 my ($self, $spec) = @_;
139 my $acc = $self->accessor_generator;
140 my $captures = $self->{captures};
142 foreach my $name (sort keys %$spec) {
143 my ($init, $trigger) = @{$spec->{$name}}{qw(init_arg trigger)};
144 next unless $init && $trigger;
145 my ($code, $add_captures) = $acc->generate_trigger(
146 $name, '$new', $acc->generate_simple_get('$new', $name), $trigger
148 @{$captures}{keys %$add_captures} = values %$add_captures;
149 $fire .= " ${code} if exists \$args->{${\perlstring $init}};\n";