factor out eager default calculation
[gitmo/Role-Tiny.git] / lib / Method / Generate / Constructor.pm
1 package Method::Generate::Constructor;
2
3 use strictures 1;
4 use Sub::Quote;
5 use base qw(Moo::Object);
6 use Sub::Defer;
7 use B 'perlstring';
8
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}
17 }
18
19 sub accessor_generator {
20   $_[0]->{accessor_generator}
21 }
22
23 sub construction_string {
24   my ($self) = @_;
25   $self->{construction_string} or 'bless({}, $class);'
26 }
27
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 }
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   }
44   local $self->{captures} = {};
45   my $body = '    my $class = shift;'."\n";
46   $body .= $self->_generate_args;
47   $body .= $self->_check_required($spec);
48   $body .= '    my $new = '.$self->construction_string.";\n";
49   $body .= $self->_assign_new($spec);
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   }
56   $body .= '    return $new;'."\n";
57   quote_sub
58     "${into}::${name}" => $body,
59     $self->{captures}, $quote_opts||{}
60   ;
61 }
62
63 sub _cap_call {
64   my ($self, $code, $captures) = @_;
65   @{$self->{captures}}{keys %$captures} = values %$captures if $captures;
66   $code;
67 }
68
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) = @_;
76   my (@init, @slots, %test);
77   my $ag = $self->accessor_generator;
78   NAME: foreach my $name (sort keys %$spec) {
79     my $attr_spec = $spec->{$name};
80     unless ($ag->is_simple_attribute($name, $attr_spec)) {
81       next NAME unless defined($attr_spec->{init_arg})
82                          or $ag->has_eager_default($name, $attr_spec);
83       $test{$name} = $attr_spec->{init_arg};
84       next NAME;
85     }
86     next NAME unless defined(my $i = $attr_spec->{init_arg});
87     push @init, $i;
88     push @slots, $name;
89   }
90   return '' unless @init or %test;
91   join '', (
92     @init
93       ? '    '.$self->_cap_call($ag->generate_multi_set(
94           '$new', [ @slots ], '@{$args}{qw('.join(' ',@init).')}'
95         )).";\n"
96       : ''
97   ), map {
98     my $arg_key = perlstring($test{$_});
99     my $test = "exists \$args->{$arg_key}";
100     my $source = "\$args->{$arg_key}";
101     my $attr_spec = $spec->{$_};
102     $self->_cap_call($ag->generate_populate_set(
103       '$new', $_, $attr_spec, $source, $test
104     ));
105   } sort keys %test;
106 }
107
108 sub _check_required {
109   my ($self, $spec) = @_;
110   my @required_init =
111     map $spec->{$_}{init_arg},
112       grep $spec->{$_}{required},
113         sort keys %$spec;
114   return '' unless @required_init;
115   '    if (my @missing = grep !exists $args->{$_}, qw('
116     .join(' ',@required_init).')) {'."\n"
117     .q{      die "Missing required arguments: ".join(', ', sort @missing);}."\n"
118     ."    }\n";
119 }
120
121 sub _check_isa {
122   my ($self, $spec) = @_;
123   my $acc = $self->accessor_generator;
124   my $captures = $self->{captures};
125   my $check = '';
126   foreach my $name (sort keys %$spec) {
127     my ($init, $isa) = @{$spec->{$name}}{qw(init_arg isa)};
128     next unless $init and $isa;
129     my $init_str = perlstring($init);
130     my ($code, $add_captures) = $acc->generate_isa_check(
131       $name, "\$args->{${init_str}}", $isa
132     );
133     @{$captures}{keys %$add_captures} = values %$add_captures;
134     $check .= "    ${code}".(
135       (not($spec->{lazy}) and ($spec->{default} or $spec->{builder})
136         ? ";\n"
137         : "if exists \$args->{${init_str}};\n"
138       )
139     );
140   }
141   return $check;
142 }
143
144 sub _fire_triggers {
145   my ($self, $spec) = @_;
146   my $acc = $self->accessor_generator;
147   my $captures = $self->{captures};
148   my $fire = '';
149   foreach my $name (sort keys %$spec) {
150     my ($init, $trigger) = @{$spec->{$name}}{qw(init_arg trigger)};
151     next unless $init && $trigger;
152     my ($code, $add_captures) = $acc->generate_trigger(
153       $name, '$new', $acc->generate_simple_get('$new', $name), $trigger
154     );
155     @{$captures}{keys %$add_captures} = values %$add_captures;
156     $fire .= "    ${code} if exists \$args->{${\perlstring $init}};\n";
157   }
158   return $fire;
159 }
160
161 1;