Commit | Line | Data |
6f68f022 |
1 | package Method::Generate::Constructor; |
2 | |
3 | use strictures 1; |
4 | use Sub::Quote; |
5 | use base qw(Class::Tiny::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 | |
14f32032 |
23 | sub install_delayed { |
24 | my ($self) = @_; |
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 } |
29 | ) |
30 | }; |
31 | $self; |
32 | } |
6f68f022 |
33 | |
34 | sub generate_method { |
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; |
38 | } |
a16d301e |
39 | local $self->{captures} = {}; |
6f68f022 |
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); |
a16d301e |
45 | $body .= $self->_fire_triggers($spec); |
4053679e |
46 | $body .= ' return $new;'."\n"; |
6f68f022 |
47 | quote_sub |
4053679e |
48 | "${into}::${name}" => $body, |
a16d301e |
49 | $self->{captures}, $quote_opts||{} |
6f68f022 |
50 | ; |
51 | } |
52 | |
53 | sub _generate_args { |
54 | my ($self) = @_; |
55 | q{ my $args = ref($_[0]) eq 'HASH' ? $_[0] : { @_ };}."\n"; |
56 | } |
57 | |
58 | sub _assign_new { |
59 | my ($self, $spec) = @_; |
60 | my (@init, @slots); |
61 | NAME: foreach my $name (keys %$spec) { |
62 | my $attr_spec = $spec->{$name}; |
63 | push @init, do { |
64 | next NAME unless defined(my $i = $attr_spec->{init_arg}); |
65 | $i; |
66 | }; |
67 | push @slots, $name; |
68 | } |
14f32032 |
69 | return '' unless @init; |
6f68f022 |
70 | ' @{$new}{qw('.join(' ',@slots).')} = @{$args}{qw('.join(' ',@init).')};' |
71 | ."\n"; |
72 | } |
73 | |
74 | sub _check_required { |
75 | my ($self, $spec) = @_; |
76 | my @required_init = |
77 | map $spec->{$_}{init_arg}, |
78 | grep $spec->{$_}{required}, |
f0d79ee0 |
79 | keys %$spec; |
6f68f022 |
80 | return '' unless @required_init; |
81 | ' if (my @missing = grep !exists $args->{$_}, qw(' |
82 | .join(' ',@required_init).')) {'."\n" |
83 | .q{ die "Missing required arguments: ".join(', ', sort @missing);}."\n" |
84 | ." }\n"; |
85 | } |
86 | |
a16d301e |
87 | sub _fire_triggers { |
88 | my ($self, $spec) = @_; |
89 | my @fire = map { |
90 | [ $_, $spec->{$_}{init_arg}, $spec->{$_}{trigger} ] |
91 | } grep { $spec->{$_}{init_arg} && $spec->{$_}{trigger} } keys %$spec; |
92 | my $acc = $self->accessor_generator; |
93 | my $captures = $self->{captures}; |
94 | my $fire = ''; |
95 | foreach my $name (keys %$spec) { |
96 | my ($init, $trigger) = @{$spec->{$name}}{qw(init_arg trigger)}; |
97 | next unless $init && $trigger; |
98 | my ($code, $add_captures) = $acc->generate_trigger( |
99 | $name, '$new', $acc->generate_simple_get('$new', $name), $trigger |
100 | ); |
101 | @{$captures}{keys %$add_captures} = values %$add_captures; |
102 | $fire .= " ${code} if exists \$args->{${\perlstring $init}};\n"; |
103 | } |
104 | return $fire; |
105 | } |
106 | |
6f68f022 |
107 | 1; |