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