add with qw( Role1 Role2 ) support
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Constructor.pm
CommitLineData
fc1d8369 1package Mouse::Meta::Method::Constructor;
2use strict;
3use warnings;
4
5sub 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
32sub _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
f262f14e 40 my $set_value = do {
fc1d8369 41 my @code;
c91862e8 42
fc1d8369 43 if ($attr->should_coerce) {
7bf66a9b 44 push @code, "my \$value = \$attr->coerce_constraint( \$args->{'$from'});";
45 }
46 else {
47 push @code, "my \$value = \$args->{'$from'};";
fc1d8369 48 }
c91862e8 49
fc1d8369 50 if ($attr->has_type_constraint) {
c91862e8 51 push @code, "\$attr->verify_type_constraint( \$value );";
fc1d8369 52 }
c91862e8 53
54 push @code, "\$instance->{'$key'} = \$value;";
55
41cdacce 56 if ($attr->is_weak_ref) {
c91862e8 57 push @code, "weaken( \$instance->{'$key'} ) if ref( \$value );";
41cdacce 58 }
c91862e8 59
fc1d8369 60 if ( $attr->has_trigger ) {
c91862e8 61 push @code, "\$attr->trigger->( \$instance, \$value, \$attr );";
fc1d8369 62 }
c91862e8 63
fc1d8369 64 join "\n", @code;
65 };
c91862e8 66
f262f14e 67 my $make_default_value = do {
fc1d8369 68 my @code;
e8ba7b26 69
fc1d8369 70 if ( $attr->has_default || $attr->has_builder ) {
71 unless ( $attr->is_lazy ) {
72 my $default = $attr->default;
73 my $builder = $attr->builder;
e8ba7b26 74
75 push @code, "my \$value = ";
76
77 if ($attr->should_coerce) {
78 push @code, "\$attr->coerce_constraint(";
fc1d8369 79 }
e8ba7b26 80
81 if ($attr->has_builder) {
82 push @code, "\$instance->$builder";
83 }
84 elsif (ref($default) eq 'CODE') {
85 push @code, "\$attr->default()->()";
86 }
87 else {
88 push @code, "\$attr->default()";
89 }
90
fc1d8369 91 if ($attr->should_coerce) {
e8ba7b26 92 push @code, ");";
93 }
94 else {
95 push @code, ";";
fc1d8369 96 }
e8ba7b26 97
fc1d8369 98 if ($attr->has_type_constraint) {
99 push @code, "\$attr->verify_type_constraint(\$value);";
100 }
e8ba7b26 101
8dcbf65d 102 push @code, "\$instance->{'$key'} = \$value;";
e8ba7b26 103
fc1d8369 104 if ($attr->is_weak_ref) {
e8ba7b26 105 push @code, "weaken( \$instance->{'$key'} ) if ref( \$value );";
fc1d8369 106 }
107 }
108 join "\n", @code;
109 }
110 else {
111 if ( $attr->is_required ) {
95e0838c 112 qq{Carp::confess("Attribute ($key) is required");};
fc1d8369 113 } else {
114 ""
115 }
116 }
117 };
118 my $code = <<"...";
119 {
c12edd9a 120 my \$attr = \$attrs[$index];
95e0838c 121 if (exists(\$args->{'$from'})) {
f262f14e 122 $set_value;
fc1d8369 123 } else {
f262f14e 124 $make_default_value;
fc1d8369 125 }
126 }
127...
128 push @res, $code;
129 }
130 return join "\n", @res;
131}
132
133sub _generate_BUILDARGS {
134 <<'...';
135 do {
136 if ( scalar @_ == 1 ) {
137 if ( defined $_[0] ) {
138 ( ref( $_[0] ) eq 'HASH' )
139 || Carp::confess "Single parameters to new() must be a HASH ref";
140 +{ %{ $_[0] } };
141 }
142 else {
143 +{};
144 }
145 }
146 else {
147 +{@_};
148 }
149 };
150...
151}
152
153sub _generate_BUILDALL {
154 my ($class, $meta) = @_;
155 return '' unless $meta->name->can('BUILD');
156
157 my @code = ();
158 push @code, q{no strict 'refs';};
159 push @code, q{no warnings 'once';};
160 no strict 'refs';
161 for my $class ($meta->linearized_isa) {
162 if (*{ $class . '::BUILD' }{CODE}) {
163 push @code, qq{${class}::BUILD->(\$instance, \$args);};
164 }
165 }
166 return join "\n", @code;
167}
168
1691;