2 package Moose::Meta::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
10 our $VERSION = '0.11';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Moose::Meta::Method',
14 'Class::MOP::Method::Generated';
20 (exists $options{options} && ref $options{options} eq 'HASH')
21 || confess "You must pass a hash of options";
23 ($options{package_name} && $options{name})
24 || confess "You must supply the package_name and name parameters";
29 '$!package_name' => $options{package_name},
30 '$!name' => $options{name},
31 # specific to this subclass
32 '%!options' => $options{options},
33 '$!meta_instance' => $options{metaclass}->get_meta_instance,
34 '@!attributes' => [ $options{metaclass}->compute_all_applicable_attributes ],
36 '$!associated_metaclass' => $options{metaclass},
39 # we don't want this creating
40 # a cycle in the code, if not
42 weaken($self->{'$!associated_metaclass'});
44 $self->initialize_body;
51 sub options { (shift)->{'%!options'} }
52 sub meta_instance { (shift)->{'$!meta_instance'} }
53 sub attributes { (shift)->{'@!attributes'} }
55 sub associated_metaclass { (shift)->{'$!associated_metaclass'} }
62 # the %options should also include a both
63 # a call 'initializer' and call 'SUPER::'
64 # options, which should cover approx 90%
65 # of the possible use cases (even if it
66 # requires some adaption on the part of
67 # the author, after all, nothing is free)
69 $source .= "\n" . 'my $class = shift;';
71 $source .= "\n" . 'return $class->Moose::Object::new(@_)';
72 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
74 $source .= "\n" . 'my %params = (scalar @_ == 1) ? %{$_[0]} : @_;';
76 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
78 $source .= ";\n" . (join ";\n" => map {
79 $self->_generate_slot_initializer($_)
80 } 0 .. (@{$self->attributes} - 1));
82 $source .= ";\n" . $self->_generate_triggers();
83 $source .= ";\n" . $self->_generate_BUILDALL();
85 $source .= ";\n" . 'return $instance';
86 $source .= ";\n" . '}';
87 warn $source if $self->options->{debug};
92 # create the nessecary lexicals
93 # to be picked up in the eval
94 my $attrs = $self->attributes;
96 # We need to check if the attribute ->can('type_constraint')
97 # since we may be trying to immutabilize a Moose meta class,
98 # which in turn has attributes which are Class::MOP::Attribute
99 # objects, rather than Moose::Meta::Attribute. And
100 # Class::MOP::Attribute attributes have no type constraints.
101 # However we need to make sure we leave an undef value there
102 # because the inlined code is using the index of the attributes
103 # to determine where to find the type constraint
105 my @type_constraints = map {
106 $_->can('type_constraint') ? $_->type_constraint : undef
109 my @type_constraint_bodies = map {
110 defined $_ ? $_->_compiled_type_constraint : undef;
113 $code = eval $source;
114 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
116 $self->{'&!body'} = $code;
119 sub _generate_BUILDALL {
122 foreach my $method (reverse $self->associated_metaclass->find_all_methods_by_name('BUILD')) {
123 push @BUILD_calls => '$instance->' . $method->{class} . '::BUILD(\%params)';
125 return join ";\n" => @BUILD_calls;
128 sub _generate_triggers {
131 foreach my $i (0 .. $#{ $self->attributes }) {
132 my $attr = $self->attributes->[$i];
133 if ($attr->can('has_trigger') && $attr->has_trigger) {
134 if (defined(my $init_arg = $attr->init_arg)) {
135 push @trigger_calls => (
136 '(exists $params{\'' . $init_arg . '\'}) && do {' . "\n "
137 . '$attrs->[' . $i . ']->trigger->('
139 . $self->meta_instance->inline_get_slot_value(
141 ("'" . $attr->name . "'")
144 . '$attrs->[' . $i . ']'
151 return join ";\n" => @trigger_calls;
154 sub _generate_slot_initializer {
158 my $attr = $self->attributes->[$index];
160 my @source = ('## ' . $attr->name);
162 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
164 if ($is_moose && defined($attr->init_arg) && $attr->is_required && !$attr->has_default && !$attr->has_builder) {
165 push @source => ('(exists $params{\'' . $attr->init_arg . '\'}) ' .
166 '|| confess "Attribute (' . $attr->name . ') is required";');
169 if (($attr->has_default || $attr->has_builder) && !($is_moose && $attr->is_lazy)) {
171 if ( defined( my $init_arg = $attr->init_arg ) ) {
172 push @source => 'if (exists $params{\'' . $init_arg . '\'}) {';
174 push @source => ('my $val = $params{\'' . $init_arg . '\'};');
176 if ($is_moose && $attr->has_type_constraint) {
177 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
178 push @source => $self->_generate_type_coercion(
180 '$type_constraints[' . $index . ']',
185 push @source => $self->_generate_type_constraint_check(
187 '$type_constraint_bodies[' . $index . ']',
188 '$type_constraints[' . $index . ']',
192 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
194 push @source => "} else {";
197 if ( $attr->has_default ) {
198 $default = $self->_generate_default_value($attr, $index);
201 my $builder = $attr->builder;
202 $default = '$instance->' . $builder;
205 push @source => '{'; # wrap this to avoid my $val overwrite warnings
206 push @source => ('my $val = ' . $default . ';');
207 push @source => $self->_generate_type_constraint_check(
209 ('$type_constraint_bodies[' . $index . ']'),
210 ('$type_constraints[' . $index . ']'),
212 ) if ($is_moose && $attr->has_type_constraint);
214 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
215 push @source => '}'; # close - wrap this to avoid my $val overrite warnings
217 push @source => "}" if defined $attr->init_arg;
219 elsif ( defined( my $init_arg = $attr->init_arg ) ) {
220 push @source => '(exists $params{\'' . $init_arg . '\'}) && do {';
222 push @source => ('my $val = $params{\'' . $init_arg . '\'};');
223 if ($is_moose && $attr->has_type_constraint) {
224 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
225 push @source => $self->_generate_type_coercion(
227 '$type_constraints[' . $index . ']',
232 push @source => $self->_generate_type_constraint_check(
234 '$type_constraint_bodies[' . $index . ']',
235 '$type_constraints[' . $index . ']',
239 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
244 return join "\n" => @source;
247 sub _generate_slot_assignment {
248 my ($self, $attr, $value, $index) = @_;
252 if ($attr->has_initializer) {
254 '$attrs->[' . $index . ']->set_initial_value($instance, ' . $value . ');'
259 $self->meta_instance->inline_set_slot_value(
261 ("'" . $attr->name . "'"),
267 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
269 if ($is_moose && $attr->is_weak_ref) {
272 $self->meta_instance->inline_weaken_slot_value(
274 ("'" . $attr->name . "'")
276 ' if ref ' . $value . ';'
283 sub _generate_type_coercion {
284 my ($self, $attr, $type_constraint_name, $value_name, $return_value_name) = @_;
285 return ($return_value_name . ' = ' . $type_constraint_name . '->coerce(' . $value_name . ');');
288 sub _generate_type_constraint_check {
289 my ($self, $attr, $type_constraint_cv, $type_constraint_obj, $value_name) = @_;
291 $type_constraint_cv . '->(' . $value_name . ')'
292 . "\n\t" . '|| confess "Attribute ('
294 . ') does not pass the type constraint because: " . '
295 . $type_constraint_obj . '->get_message(' . $value_name . ');'
299 sub _generate_default_value {
300 my ($self, $attr, $index) = @_;
302 # default values can either be CODE refs
303 # in which case we need to call them. Or
304 # they can be scalars (strings/numbers)
305 # in which case we can just deal with them
306 # in the code we eval.
307 if ($attr->is_default_a_coderef) {
308 return '$attrs->[' . $index . ']->default($instance)';
311 my $default = $attr->default;
312 # make sure to quote strings ...
313 unless (looks_like_number($default)) {
314 $default = "'$default'";
329 Moose::Meta::Method::Constructor - Method Meta Object for constructors
333 This is a subclass of L<Class::MOP::Method> which handles
334 constructing an approprate Constructor methods. This is primarily
335 used in the making of immutable metaclasses, otherwise it is
336 not particularly useful.
346 =item B<meta_instance>
350 =item B<initialize_body>
352 =item B<associated_metaclass>
358 Stevan Little E<lt>stevan@iinteractive.comE<gt>
360 =head1 COPYRIGHT AND LICENSE
362 Copyright 2006-2008 by Infinity Interactive, Inc.
364 L<http://www.iinteractive.com>
366 This library is free software; you can redistribute it and/or modify
367 it under the same terms as Perl itself.