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