support for default at construction time
[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);
6d377074 43 $body .= $self->_check_isa($spec);
6f68f022 44 $body .= ' my $new = bless({}, $class);'."\n";
45 $body .= $self->_assign_new($spec);
a16d301e 46 $body .= $self->_fire_triggers($spec);
4053679e 47 $body .= ' return $new;'."\n";
6f68f022 48 quote_sub
4053679e 49 "${into}::${name}" => $body,
a16d301e 50 $self->{captures}, $quote_opts||{}
6f68f022 51 ;
52}
53
54sub _generate_args {
55 my ($self) = @_;
56 q{ my $args = ref($_[0]) eq 'HASH' ? $_[0] : { @_ };}."\n";
57}
58
59sub _assign_new {
60 my ($self, $spec) = @_;
46389f86 61 my (@init, @slots, %test);
62 NAME: foreach my $name (sort keys %$spec) {
6f68f022 63 my $attr_spec = $spec->{$name};
46389f86 64 next NAME unless defined(my $i = $attr_spec->{init_arg});
649ac264 65 if ($attr_spec->{lazy} or $attr_spec->{default} or $attr_spec->{builder}) {
46389f86 66 $test{$name} = $i;
67 next NAME;
68 }
69 push @init, $i;
6f68f022 70 push @slots, $name;
71 }
649ac264 72 return '' unless @init or %test;
46389f86 73 join '', (
74 @init
75 ? ' @{$new}{qw('.join(' ',@slots).')}'
76 .' = @{$args}{qw('.join(' ',@init).')};'."\n"
77 : ''
78 ), map {
79 my $arg_key = perlstring($test{$_});
649ac264 80 my $ag = $self->accessor_generator;
81 my $test = "exists \$args->{$arg_key}";
82 my $source = "\$args->{$arg_key}";
83 my $attr_spec = $spec->{$_};
84 my ($code, $add_captures);
85 if (!$attr_spec->{lazy} and
86 ($attr_spec->{default} or $attr_spec->{builder})) {
87 my $get_captures;
88 ($code, $add_captures) = $ag->generate_simple_set(
89 '$new', $_,
90 "(\n ${test}\n ? ${source}\n : "
91 .do {
92 (my $get, $get_captures) = $ag->generate_get_default(
93 '$new', $_, $attr_spec
94 );
95 $get;
96 }
97 ."\n )"
98 );
99 @{$add_captures}{keys %$get_captures} = values %$get_captures;
100 $code .= ";\n";
101 } else {
102 ($code, $add_captures) = $ag->generate_simple_set(
103 '$new', $_, "\$args->{$arg_key}"
104 );
105 $code .= " if ${test};\n";
106 }
107 @{$self->{captures}}{keys %$add_captures} = values %$add_captures;
108 ' '.$code;
46389f86 109 } sort keys %test;
6f68f022 110}
111
112sub _check_required {
113 my ($self, $spec) = @_;
114 my @required_init =
115 map $spec->{$_}{init_arg},
116 grep $spec->{$_}{required},
6d377074 117 sort keys %$spec;
6f68f022 118 return '' unless @required_init;
119 ' if (my @missing = grep !exists $args->{$_}, qw('
120 .join(' ',@required_init).')) {'."\n"
121 .q{ die "Missing required arguments: ".join(', ', sort @missing);}."\n"
122 ." }\n";
123}
124
6d377074 125sub _check_isa {
126 my ($self, $spec) = @_;
127 my $acc = $self->accessor_generator;
128 my $captures = $self->{captures};
129 my $check = '';
130 foreach my $name (sort keys %$spec) {
131 my ($init, $isa) = @{$spec->{$name}}{qw(init_arg isa)};
132 next unless $init and $isa;
133 my $init_str = perlstring($init);
134 my ($code, $add_captures) = $acc->generate_isa_check(
135 $name, "\$args->{${init_str}}", $isa
136 );
137 @{$captures}{keys %$add_captures} = values %$add_captures;
649ac264 138 $check .= " ${code}".(
139 (not($spec->{lazy}) and ($spec->{default} or $spec->{builder})
140 ? ";\n"
141 : "if exists \$args->{${init_str}};\n"
142 )
143 );
6d377074 144 }
145 return $check;
146}
147
a16d301e 148sub _fire_triggers {
149 my ($self, $spec) = @_;
a16d301e 150 my $acc = $self->accessor_generator;
151 my $captures = $self->{captures};
152 my $fire = '';
6d377074 153 foreach my $name (sort keys %$spec) {
a16d301e 154 my ($init, $trigger) = @{$spec->{$name}}{qw(init_arg trigger)};
155 next unless $init && $trigger;
156 my ($code, $add_captures) = $acc->generate_trigger(
157 $name, '$new', $acc->generate_simple_get('$new', $name), $trigger
158 );
159 @{$captures}{keys %$add_captures} = values %$add_captures;
160 $fire .= " ${code} if exists \$args->{${\perlstring $init}};\n";
161 }
162 return $fire;
163}
164
6f68f022 1651;