cleanup require usage so we don't trample on $@ and tweak the DEMOLISH code slightly
[gitmo/Role-Tiny.git] / lib / Method / Generate / Constructor.pm
CommitLineData
6f68f022 1package Method::Generate::Constructor;
2
3use strictures 1;
4use Sub::Quote;
b1eebd55 5use base qw(Moo::Object);
14f32032 6use Sub::Defer;
a16d301e 7use B 'perlstring';
6f68f022 8
96d3f07a 9sub register_attribute_specs {
10 my ($self, %spec) = @_;
11 @{$self->{attribute_specs}||={}}{keys %spec} = values %spec;
12 $self;
13}
14
15sub all_attribute_specs {
16 $_[0]->{attribute_specs}
14f32032 17}
18
a16d301e 19sub accessor_generator {
20 $_[0]->{accessor_generator}
21}
22
de5c0e53 23sub construction_string {
24 my ($self) = @_;
25 $self->{construction_string} or 'bless({}, $class);'
26}
27
14f32032 28sub install_delayed {
29 my ($self) = @_;
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 }
34 )
35 };
36 $self;
37}
6f68f022 38
39sub generate_method {
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;
43 }
a16d301e 44 local $self->{captures} = {};
6f68f022 45 my $body = ' my $class = shift;'."\n";
e0e12d16 46 $body .= $self->_handle_subconstructor($into, $name);
0123201b 47 my $into_buildargs = $into->can('BUILDARGS');
48 if ( $into_buildargs && $into_buildargs != \&Moo::Object::BUILDARGS ) {
eae70e33 49 $body .= $self->_generate_args_via_buildargs;
50 } else {
51 $body .= $self->_generate_args;
52 }
6f68f022 53 $body .= $self->_check_required($spec);
de5c0e53 54 $body .= ' my $new = '.$self->construction_string.";\n";
6f68f022 55 $body .= $self->_assign_new($spec);
077bd026 56 if ($into->can('BUILD')) {
59812c87 57 { local $@; require Method::Generate::BuildAll; }
077bd026 58 $body .= Method::Generate::BuildAll->new->buildall_body_for(
59 $into, '$new', '$args'
60 );
61 }
4053679e 62 $body .= ' return $new;'."\n";
6f68f022 63 quote_sub
4053679e 64 "${into}::${name}" => $body,
a16d301e 65 $self->{captures}, $quote_opts||{}
6f68f022 66 ;
67}
68
e0e12d16 69sub _handle_subconstructor {
70 my ($self, $into, $name) = @_;
71 if (my $gen = $self->{subconstructor_generator}) {
72 ' if ($class ne '.perlstring($into).') {'."\n".
73 ' '.$gen.";\n".
74 ' return $class->'.$name.'(@_)'.";\n".
75 ' }'."\n";
76 } else {
77 ''
78 }
79}
80
3a9a65a4 81sub _cap_call {
82 my ($self, $code, $captures) = @_;
83 @{$self->{captures}}{keys %$captures} = values %$captures if $captures;
84 $code;
85}
86
eae70e33 87sub _generate_args_via_buildargs {
6f68f022 88 my ($self) = @_;
a17be455 89 q{ my $args = $class->BUILDARGS(@_);}."\n";
6f68f022 90}
91
0123201b 92# inlined from Moo::Object - update that first.
eae70e33 93sub _generate_args {
94 my ($self) = @_;
0123201b 95 return <<'_EOA';
96 my $args;
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";
101 }
102 $args = { %{ $_[0] } };
103 }
104 elsif ( @_ % 2 ) {
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";
107 }
108 else {
109 $args = {@_};
110 }
111_EOA
112
eae70e33 113}
114
6f68f022 115sub _assign_new {
116 my ($self, $spec) = @_;
46389f86 117 my (@init, @slots, %test);
3a9a65a4 118 my $ag = $self->accessor_generator;
46389f86 119 NAME: foreach my $name (sort keys %$spec) {
6f68f022 120 my $attr_spec = $spec->{$name};
3a9a65a4 121 unless ($ag->is_simple_attribute($name, $attr_spec)) {
d02da2bc 122 next NAME unless defined($attr_spec->{init_arg})
4ced3a94 123 or $ag->has_eager_default($name, $attr_spec);
d02da2bc 124 $test{$name} = $attr_spec->{init_arg};
46389f86 125 next NAME;
126 }
d02da2bc 127 next NAME unless defined(my $i = $attr_spec->{init_arg});
46389f86 128 push @init, $i;
6f68f022 129 push @slots, $name;
130 }
649ac264 131 return '' unless @init or %test;
46389f86 132 join '', (
133 @init
3a9a65a4 134 ? ' '.$self->_cap_call($ag->generate_multi_set(
5d349892 135 '$new', [ @slots ], '@{$args}{qw('.join(' ',@init).')}'
136 )).";\n"
46389f86 137 : ''
138 ), map {
139 my $arg_key = perlstring($test{$_});
649ac264 140 my $test = "exists \$args->{$arg_key}";
141 my $source = "\$args->{$arg_key}";
142 my $attr_spec = $spec->{$_};
3a9a65a4 143 $self->_cap_call($ag->generate_populate_set(
144 '$new', $_, $attr_spec, $source, $test
145 ));
46389f86 146 } sort keys %test;
6f68f022 147}
148
149sub _check_required {
150 my ($self, $spec) = @_;
151 my @required_init =
152 map $spec->{$_}{init_arg},
153 grep $spec->{$_}{required},
6d377074 154 sort keys %$spec;
6f68f022 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"
159 ." }\n";
160}
161
6d377074 162sub _check_isa {
163 my ($self, $spec) = @_;
164 my $acc = $self->accessor_generator;
165 my $captures = $self->{captures};
166 my $check = '';
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
173 );
174 @{$captures}{keys %$add_captures} = values %$add_captures;
649ac264 175 $check .= " ${code}".(
176 (not($spec->{lazy}) and ($spec->{default} or $spec->{builder})
177 ? ";\n"
178 : "if exists \$args->{${init_str}};\n"
179 )
180 );
6d377074 181 }
182 return $check;
183}
184
a16d301e 185sub _fire_triggers {
186 my ($self, $spec) = @_;
a16d301e 187 my $acc = $self->accessor_generator;
188 my $captures = $self->{captures};
189 my $fire = '';
6d377074 190 foreach my $name (sort keys %$spec) {
a16d301e 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
195 );
196 @{$captures}{keys %$add_captures} = values %$add_captures;
197 $fire .= " ${code} if exists \$args->{${\perlstring $init}};\n";
198 }
199 return $fire;
200}
201
6f68f022 2021;