add predicate and clearer
[gitmo/Moo.git] / lib / Method / Generate / Constructor.pm
CommitLineData
6f68f022 1package Method::Generate::Constructor;
2
3use strictures 1;
4use Sub::Quote;
5use base qw(Class::Tiny::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
14f32032 23sub 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
34sub 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);
077bd026 45 if ($into->can('BUILD')) {
46 require Method::Generate::BuildAll;
47 $body .= Method::Generate::BuildAll->new->buildall_body_for(
48 $into, '$new', '$args'
49 );
50 }
4053679e 51 $body .= ' return $new;'."\n";
6f68f022 52 quote_sub
4053679e 53 "${into}::${name}" => $body,
a16d301e 54 $self->{captures}, $quote_opts||{}
6f68f022 55 ;
56}
57
3a9a65a4 58sub _cap_call {
59 my ($self, $code, $captures) = @_;
60 @{$self->{captures}}{keys %$captures} = values %$captures if $captures;
61 $code;
62}
63
6f68f022 64sub _generate_args {
65 my ($self) = @_;
66 q{ my $args = ref($_[0]) eq 'HASH' ? $_[0] : { @_ };}."\n";
67}
68
69sub _assign_new {
70 my ($self, $spec) = @_;
46389f86 71 my (@init, @slots, %test);
3a9a65a4 72 my $ag = $self->accessor_generator;
46389f86 73 NAME: foreach my $name (sort keys %$spec) {
6f68f022 74 my $attr_spec = $spec->{$name};
46389f86 75 next NAME unless defined(my $i = $attr_spec->{init_arg});
3a9a65a4 76 unless ($ag->is_simple_attribute($name, $attr_spec)) {
46389f86 77 $test{$name} = $i;
78 next NAME;
79 }
80 push @init, $i;
6f68f022 81 push @slots, $name;
82 }
649ac264 83 return '' unless @init or %test;
46389f86 84 join '', (
85 @init
3a9a65a4 86 ? ' '.$self->_cap_call($ag->generate_multi_set(
87 '$new', [ @slots ], '@{$args}{qw('.join(' ',@init).')}'
88 )).";\n"
46389f86 89 : ''
90 ), map {
91 my $arg_key = perlstring($test{$_});
649ac264 92 my $test = "exists \$args->{$arg_key}";
93 my $source = "\$args->{$arg_key}";
94 my $attr_spec = $spec->{$_};
3a9a65a4 95 $self->_cap_call($ag->generate_populate_set(
96 '$new', $_, $attr_spec, $source, $test
97 ));
46389f86 98 } sort keys %test;
6f68f022 99}
100
101sub _check_required {
102 my ($self, $spec) = @_;
103 my @required_init =
104 map $spec->{$_}{init_arg},
105 grep $spec->{$_}{required},
6d377074 106 sort keys %$spec;
6f68f022 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"
111 ." }\n";
112}
113
6d377074 114sub _check_isa {
115 my ($self, $spec) = @_;
116 my $acc = $self->accessor_generator;
117 my $captures = $self->{captures};
118 my $check = '';
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
125 );
126 @{$captures}{keys %$add_captures} = values %$add_captures;
649ac264 127 $check .= " ${code}".(
128 (not($spec->{lazy}) and ($spec->{default} or $spec->{builder})
129 ? ";\n"
130 : "if exists \$args->{${init_str}};\n"
131 )
132 );
6d377074 133 }
134 return $check;
135}
136
a16d301e 137sub _fire_triggers {
138 my ($self, $spec) = @_;
a16d301e 139 my $acc = $self->accessor_generator;
140 my $captures = $self->{captures};
141 my $fire = '';
6d377074 142 foreach my $name (sort keys %$spec) {
a16d301e 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
147 );
148 @{$captures}{keys %$add_captures} = values %$add_captures;
149 $fire .= " ${code} if exists \$args->{${\perlstring $init}};\n";
150 }
151 return $fire;
152}
153
6f68f022 1541;