Commit | Line | Data |
6f68f022 |
1 | package Method::Generate::Constructor; |
2 | |
3 | use strictures 1; |
4 | use Sub::Quote; |
b1eebd55 |
5 | use base qw(Moo::Object); |
14f32032 |
6 | use Sub::Defer; |
a16d301e |
7 | use B 'perlstring'; |
6f68f022 |
8 | |
96d3f07a |
9 | sub register_attribute_specs { |
10 | my ($self, %spec) = @_; |
11 | @{$self->{attribute_specs}||={}}{keys %spec} = values %spec; |
12 | $self; |
13 | } |
14 | |
15 | sub all_attribute_specs { |
16 | $_[0]->{attribute_specs} |
14f32032 |
17 | } |
18 | |
a16d301e |
19 | sub accessor_generator { |
20 | $_[0]->{accessor_generator} |
21 | } |
22 | |
de5c0e53 |
23 | sub construction_string { |
24 | my ($self) = @_; |
25 | $self->{construction_string} or 'bless({}, $class);' |
26 | } |
27 | |
14f32032 |
28 | sub 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 | |
39 | sub 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"; |
46 | $body .= $self->_generate_args; |
47 | $body .= $self->_check_required($spec); |
de5c0e53 |
48 | $body .= ' my $new = '.$self->construction_string.";\n"; |
6f68f022 |
49 | $body .= $self->_assign_new($spec); |
077bd026 |
50 | if ($into->can('BUILD')) { |
51 | require Method::Generate::BuildAll; |
52 | $body .= Method::Generate::BuildAll->new->buildall_body_for( |
53 | $into, '$new', '$args' |
54 | ); |
55 | } |
4053679e |
56 | $body .= ' return $new;'."\n"; |
6f68f022 |
57 | quote_sub |
4053679e |
58 | "${into}::${name}" => $body, |
a16d301e |
59 | $self->{captures}, $quote_opts||{} |
6f68f022 |
60 | ; |
61 | } |
62 | |
3a9a65a4 |
63 | sub _cap_call { |
64 | my ($self, $code, $captures) = @_; |
65 | @{$self->{captures}}{keys %$captures} = values %$captures if $captures; |
66 | $code; |
67 | } |
68 | |
6f68f022 |
69 | sub _generate_args { |
70 | my ($self) = @_; |
71 | q{ my $args = ref($_[0]) eq 'HASH' ? $_[0] : { @_ };}."\n"; |
72 | } |
73 | |
74 | sub _assign_new { |
75 | my ($self, $spec) = @_; |
46389f86 |
76 | my (@init, @slots, %test); |
3a9a65a4 |
77 | my $ag = $self->accessor_generator; |
46389f86 |
78 | NAME: foreach my $name (sort keys %$spec) { |
6f68f022 |
79 | my $attr_spec = $spec->{$name}; |
46389f86 |
80 | next NAME unless defined(my $i = $attr_spec->{init_arg}); |
3a9a65a4 |
81 | unless ($ag->is_simple_attribute($name, $attr_spec)) { |
46389f86 |
82 | $test{$name} = $i; |
83 | next NAME; |
84 | } |
85 | push @init, $i; |
6f68f022 |
86 | push @slots, $name; |
87 | } |
649ac264 |
88 | return '' unless @init or %test; |
46389f86 |
89 | join '', ( |
90 | @init |
3a9a65a4 |
91 | ? ' '.$self->_cap_call($ag->generate_multi_set( |
5d349892 |
92 | '$new', [ @slots ], '@{$args}{qw('.join(' ',@init).')}' |
93 | )).";\n" |
46389f86 |
94 | : '' |
95 | ), map { |
96 | my $arg_key = perlstring($test{$_}); |
649ac264 |
97 | my $test = "exists \$args->{$arg_key}"; |
98 | my $source = "\$args->{$arg_key}"; |
99 | my $attr_spec = $spec->{$_}; |
3a9a65a4 |
100 | $self->_cap_call($ag->generate_populate_set( |
101 | '$new', $_, $attr_spec, $source, $test |
102 | )); |
46389f86 |
103 | } sort keys %test; |
6f68f022 |
104 | } |
105 | |
106 | sub _check_required { |
107 | my ($self, $spec) = @_; |
108 | my @required_init = |
109 | map $spec->{$_}{init_arg}, |
110 | grep $spec->{$_}{required}, |
6d377074 |
111 | sort keys %$spec; |
6f68f022 |
112 | return '' unless @required_init; |
113 | ' if (my @missing = grep !exists $args->{$_}, qw(' |
114 | .join(' ',@required_init).')) {'."\n" |
115 | .q{ die "Missing required arguments: ".join(', ', sort @missing);}."\n" |
116 | ." }\n"; |
117 | } |
118 | |
6d377074 |
119 | sub _check_isa { |
120 | my ($self, $spec) = @_; |
121 | my $acc = $self->accessor_generator; |
122 | my $captures = $self->{captures}; |
123 | my $check = ''; |
124 | foreach my $name (sort keys %$spec) { |
125 | my ($init, $isa) = @{$spec->{$name}}{qw(init_arg isa)}; |
126 | next unless $init and $isa; |
127 | my $init_str = perlstring($init); |
128 | my ($code, $add_captures) = $acc->generate_isa_check( |
129 | $name, "\$args->{${init_str}}", $isa |
130 | ); |
131 | @{$captures}{keys %$add_captures} = values %$add_captures; |
649ac264 |
132 | $check .= " ${code}".( |
133 | (not($spec->{lazy}) and ($spec->{default} or $spec->{builder}) |
134 | ? ";\n" |
135 | : "if exists \$args->{${init_str}};\n" |
136 | ) |
137 | ); |
6d377074 |
138 | } |
139 | return $check; |
140 | } |
141 | |
a16d301e |
142 | sub _fire_triggers { |
143 | my ($self, $spec) = @_; |
a16d301e |
144 | my $acc = $self->accessor_generator; |
145 | my $captures = $self->{captures}; |
146 | my $fire = ''; |
6d377074 |
147 | foreach my $name (sort keys %$spec) { |
a16d301e |
148 | my ($init, $trigger) = @{$spec->{$name}}{qw(init_arg trigger)}; |
149 | next unless $init && $trigger; |
150 | my ($code, $add_captures) = $acc->generate_trigger( |
151 | $name, '$new', $acc->generate_simple_get('$new', $name), $trigger |
152 | ); |
153 | @{$captures}{keys %$add_captures} = values %$add_captures; |
154 | $fire .= " ${code} if exists \$args->{${\perlstring $init}};\n"; |
155 | } |
156 | return $fire; |
157 | } |
158 | |
6f68f022 |
159 | 1; |