Commit | Line | Data |
fc1d8369 |
1 | package Mouse::Meta::Method::Constructor; |
2 | use strict; |
3 | use warnings; |
4 | |
5 | sub generate_constructor_method_inline { |
41cdacce |
6 | my ($class, $meta) = @_; |
24ad3f66 |
7 | |
c12edd9a |
8 | my @attrs = $meta->compute_all_applicable_attributes; # this one is using by evaled code |
fc1d8369 |
9 | my $buildall = $class->_generate_BUILDALL($meta); |
10 | my $buildargs = $class->_generate_BUILDARGS(); |
c12edd9a |
11 | my $processattrs = $class->_generate_processattrs($meta, \@attrs); |
fc1d8369 |
12 | |
24ad3f66 |
13 | my $code = <<"..."; |
fc1d8369 |
14 | sub { |
15 | my \$class = shift; |
16 | my \$args = $buildargs; |
24ad3f66 |
17 | my \$instance = bless {}, \$class; |
fc1d8369 |
18 | $processattrs; |
19 | $buildall; |
20 | return \$instance; |
21 | } |
22 | ... |
24ad3f66 |
23 | |
24 | warn $code if $ENV{DEBUG}; |
25 | |
26 | local $@; |
27 | my $res = eval $code; |
28 | die $@ if $@; |
29 | $res; |
fc1d8369 |
30 | } |
31 | |
32 | sub _generate_processattrs { |
c12edd9a |
33 | my ($class, $meta, $attrs) = @_; |
fc1d8369 |
34 | my @res; |
c12edd9a |
35 | for my $index (0..scalar(@$attrs)-1) { |
36 | my $attr = $attrs->[$index]; |
fc1d8369 |
37 | my $from = $attr->init_arg; |
38 | my $key = $attr->name; |
c91862e8 |
39 | |
fc1d8369 |
40 | my $part1 = do { |
41 | my @code; |
c91862e8 |
42 | |
43 | push @code, "my \$value = \$args->{'$from'};"; |
44 | |
fc1d8369 |
45 | if ($attr->should_coerce) { |
c91862e8 |
46 | push @code, "\$value = \$attr->coerce_constraint( \$value );"; |
fc1d8369 |
47 | } |
c91862e8 |
48 | |
fc1d8369 |
49 | if ($attr->has_type_constraint) { |
c91862e8 |
50 | push @code, "\$attr->verify_type_constraint( \$value );"; |
fc1d8369 |
51 | } |
c91862e8 |
52 | |
53 | push @code, "\$instance->{'$key'} = \$value;"; |
54 | |
41cdacce |
55 | if ($attr->is_weak_ref) { |
c91862e8 |
56 | push @code, "weaken( \$instance->{'$key'} ) if ref( \$value );"; |
41cdacce |
57 | } |
c91862e8 |
58 | |
fc1d8369 |
59 | if ( $attr->has_trigger ) { |
c91862e8 |
60 | push @code, "\$attr->trigger->( \$instance, \$value, \$attr );"; |
fc1d8369 |
61 | } |
c91862e8 |
62 | |
fc1d8369 |
63 | join "\n", @code; |
64 | }; |
c91862e8 |
65 | |
fc1d8369 |
66 | my $part2 = do { |
67 | my @code; |
e8ba7b26 |
68 | |
fc1d8369 |
69 | if ( $attr->has_default || $attr->has_builder ) { |
70 | unless ( $attr->is_lazy ) { |
71 | my $default = $attr->default; |
72 | my $builder = $attr->builder; |
e8ba7b26 |
73 | |
74 | push @code, "my \$value = "; |
75 | |
76 | if ($attr->should_coerce) { |
77 | push @code, "\$attr->coerce_constraint("; |
fc1d8369 |
78 | } |
e8ba7b26 |
79 | |
80 | if ($attr->has_builder) { |
81 | push @code, "\$instance->$builder"; |
82 | } |
83 | elsif (ref($default) eq 'CODE') { |
84 | push @code, "\$attr->default()->()"; |
85 | } |
86 | else { |
87 | push @code, "\$attr->default()"; |
88 | } |
89 | |
fc1d8369 |
90 | if ($attr->should_coerce) { |
e8ba7b26 |
91 | push @code, ");"; |
92 | } |
93 | else { |
94 | push @code, ";"; |
fc1d8369 |
95 | } |
e8ba7b26 |
96 | |
fc1d8369 |
97 | if ($attr->has_type_constraint) { |
98 | push @code, "\$attr->verify_type_constraint(\$value);"; |
99 | } |
e8ba7b26 |
100 | |
8dcbf65d |
101 | push @code, "\$instance->{'$key'} = \$value;"; |
e8ba7b26 |
102 | |
fc1d8369 |
103 | if ($attr->is_weak_ref) { |
e8ba7b26 |
104 | push @code, "weaken( \$instance->{'$key'} ) if ref( \$value );"; |
fc1d8369 |
105 | } |
106 | } |
107 | join "\n", @code; |
108 | } |
109 | else { |
110 | if ( $attr->is_required ) { |
95e0838c |
111 | qq{Carp::confess("Attribute ($key) is required");}; |
fc1d8369 |
112 | } else { |
113 | "" |
114 | } |
115 | } |
116 | }; |
117 | my $code = <<"..."; |
118 | { |
c12edd9a |
119 | my \$attr = \$attrs[$index]; |
95e0838c |
120 | if (exists(\$args->{'$from'})) { |
fc1d8369 |
121 | $part1; |
122 | } else { |
123 | $part2; |
124 | } |
125 | } |
126 | ... |
127 | push @res, $code; |
128 | } |
129 | return join "\n", @res; |
130 | } |
131 | |
132 | sub _generate_BUILDARGS { |
133 | <<'...'; |
134 | do { |
135 | if ( scalar @_ == 1 ) { |
136 | if ( defined $_[0] ) { |
137 | ( ref( $_[0] ) eq 'HASH' ) |
138 | || Carp::confess "Single parameters to new() must be a HASH ref"; |
139 | +{ %{ $_[0] } }; |
140 | } |
141 | else { |
142 | +{}; |
143 | } |
144 | } |
145 | else { |
146 | +{@_}; |
147 | } |
148 | }; |
149 | ... |
150 | } |
151 | |
152 | sub _generate_BUILDALL { |
153 | my ($class, $meta) = @_; |
154 | return '' unless $meta->name->can('BUILD'); |
155 | |
156 | my @code = (); |
157 | push @code, q{no strict 'refs';}; |
158 | push @code, q{no warnings 'once';}; |
159 | no strict 'refs'; |
160 | for my $class ($meta->linearized_isa) { |
161 | if (*{ $class . '::BUILD' }{CODE}) { |
162 | push @code, qq{${class}::BUILD->(\$instance, \$args);}; |
163 | } |
164 | } |
165 | return join "\n", @code; |
166 | } |
167 | |
168 | 1; |