d62545d573a6259076bf1783f45c86fc7fa74ba0
[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   $body .= '    return $new;'."\n";
48   quote_sub
49     "${into}::${name}" => $body,
50     $self->{captures}, $quote_opts||{}
51   ;
52 }
53
54 sub _generate_args {
55   my ($self) = @_;
56   q{    my $args = ref($_[0]) eq 'HASH' ? $_[0] : { @_ };}."\n";
57 }
58
59 sub _assign_new {
60   my ($self, $spec) = @_;
61   my (@init, @slots, %test);
62   NAME: foreach my $name (sort keys %$spec) {
63     my $attr_spec = $spec->{$name};
64     next NAME unless defined(my $i = $attr_spec->{init_arg});
65     if ($attr_spec->{lazy} or $attr_spec->{default} or $attr_spec->{builder}) {
66       $test{$name} = $i;
67       next NAME;
68     }
69     push @init, $i;
70     push @slots, $name;
71   }
72   return '' unless @init or %test;
73   join '', (
74     @init
75       ? '    @{$new}{qw('.join(' ',@slots).')}'
76         .' = @{$args}{qw('.join(' ',@init).')};'."\n"
77       : ''
78   ), map {
79     my $arg_key = perlstring($test{$_});
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;
109   } sort keys %test;
110 }
111
112 sub _check_required {
113   my ($self, $spec) = @_;
114   my @required_init =
115     map $spec->{$_}{init_arg},
116       grep $spec->{$_}{required},
117         sort keys %$spec;
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
125 sub _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;
138     $check .= "    ${code}".(
139       (not($spec->{lazy}) and ($spec->{default} or $spec->{builder})
140         ? ";\n"
141         : "if exists \$args->{${init_str}};\n"
142       )
143     );
144   }
145   return $check;
146 }
147
148 sub _fire_triggers {
149   my ($self, $spec) = @_;
150   my $acc = $self->accessor_generator;
151   my $captures = $self->{captures};
152   my $fire = '';
153   foreach my $name (sort keys %$spec) {
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
165 1;