2 package Moose::Meta::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
10 our $VERSION = '0.50';
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 $Class::MOP::Method::UPGRADE_ERROR_TEXT";
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'} }
59 # this was changed in 0.41, but broke MooseX::Singleton, so try to catch
60 # any other code using the original broken spelling
61 sub intialize_body { confess "Please correct the spelling of 'intialize_body' to 'initialize_body'" }
66 # the %options should also include a both
67 # a call 'initializer' and call 'SUPER::'
68 # options, which should cover approx 90%
69 # of the possible use cases (even if it
70 # requires some adaption on the part of
71 # the author, after all, nothing is free)
73 $source .= "\n" . 'my $class = shift;';
75 $source .= "\n" . 'return $class->Moose::Object::new(@_)';
76 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
78 $source .= "\n" . 'my $params = ' . $self->_generate_BUILDARGS('$class', '@_');
80 $source .= ";\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
82 $source .= ";\n" . (join ";\n" => map {
83 $self->_generate_slot_initializer($_)
84 } 0 .. (@{$self->attributes} - 1));
86 $source .= ";\n" . $self->_generate_triggers();
87 $source .= ";\n" . $self->_generate_BUILDALL();
89 $source .= ";\n" . 'return $instance';
90 $source .= ";\n" . '}';
91 warn $source if $self->options->{debug};
96 # create the nessecary lexicals
97 # to be picked up in the eval
98 my $attrs = $self->attributes;
100 # We need to check if the attribute ->can('type_constraint')
101 # since we may be trying to immutabilize a Moose meta class,
102 # which in turn has attributes which are Class::MOP::Attribute
103 # objects, rather than Moose::Meta::Attribute. And
104 # Class::MOP::Attribute attributes have no type constraints.
105 # However we need to make sure we leave an undef value there
106 # because the inlined code is using the index of the attributes
107 # to determine where to find the type constraint
109 my @type_constraints = map {
110 $_->can('type_constraint') ? $_->type_constraint : undef
113 my @type_constraint_bodies = map {
114 defined $_ ? $_->_compiled_type_constraint : undef;
117 $code = eval $source;
118 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
120 $self->{'&!body'} = $code;
123 sub _generate_BUILDARGS {
124 my ( $self, $class, $args ) = @_;
126 my $buildargs = $self->associated_metaclass->find_method_by_name("BUILDARGS");
128 if ( $args eq '@_' and ( !$buildargs or $buildargs->body == \&Moose::Object::BUILDARGS ) ) {
131 'no warnings "uninitialized";',
132 'confess "Single parameters to new() must be a HASH ref"',
133 ' if scalar @_ == 1 && defined $_[0] && ref($_[0]) ne q{HASH};',
134 '(scalar @_ == 1) ? {%{$_[0]}} : {@_};',
138 return $class . "->BUILDARGS($args)";
142 sub _generate_BUILDALL {
145 foreach my $method (reverse $self->associated_metaclass->find_all_methods_by_name('BUILD')) {
146 push @BUILD_calls => '$instance->' . $method->{class} . '::BUILD($params)';
148 return join ";\n" => @BUILD_calls;
151 sub _generate_triggers {
154 foreach my $i (0 .. $#{ $self->attributes }) {
155 my $attr = $self->attributes->[$i];
156 if ($attr->can('has_trigger') && $attr->has_trigger) {
157 if (defined(my $init_arg = $attr->init_arg)) {
158 push @trigger_calls => (
159 '(exists $params->{\'' . $init_arg . '\'}) && do {' . "\n "
160 . '$attrs->[' . $i . ']->trigger->('
162 . $self->meta_instance->inline_get_slot_value(
164 ("'" . $attr->name . "'")
167 . '$attrs->[' . $i . ']'
174 return join ";\n" => @trigger_calls;
177 sub _generate_slot_initializer {
181 my $attr = $self->attributes->[$index];
183 my @source = ('## ' . $attr->name);
185 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
187 if ($is_moose && defined($attr->init_arg) && $attr->is_required && !$attr->has_default && !$attr->has_builder) {
188 push @source => ('(exists $params->{\'' . $attr->init_arg . '\'}) ' .
189 '|| confess "Attribute (' . $attr->name . ') is required";');
192 if (($attr->has_default || $attr->has_builder) && !($is_moose && $attr->is_lazy)) {
194 if ( defined( my $init_arg = $attr->init_arg ) ) {
195 push @source => 'if (exists $params->{\'' . $init_arg . '\'}) {';
197 push @source => ('my $val = $params->{\'' . $init_arg . '\'};');
199 if ($is_moose && $attr->has_type_constraint) {
200 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
201 push @source => $self->_generate_type_coercion(
203 '$type_constraints[' . $index . ']',
208 push @source => $self->_generate_type_constraint_check(
210 '$type_constraint_bodies[' . $index . ']',
211 '$type_constraints[' . $index . ']',
215 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
217 push @source => "} else {";
220 if ( $attr->has_default ) {
221 $default = $self->_generate_default_value($attr, $index);
224 my $builder = $attr->builder;
225 $default = '$instance->' . $builder;
228 push @source => '{'; # wrap this to avoid my $val overwrite warnings
229 push @source => ('my $val = ' . $default . ';');
230 push @source => $self->_generate_type_constraint_check(
232 ('$type_constraint_bodies[' . $index . ']'),
233 ('$type_constraints[' . $index . ']'),
235 ) if ($is_moose && $attr->has_type_constraint);
237 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
238 push @source => '}'; # close - wrap this to avoid my $val overrite warnings
240 push @source => "}" if defined $attr->init_arg;
242 elsif ( defined( my $init_arg = $attr->init_arg ) ) {
243 push @source => '(exists $params->{\'' . $init_arg . '\'}) && do {';
245 push @source => ('my $val = $params->{\'' . $init_arg . '\'};');
246 if ($is_moose && $attr->has_type_constraint) {
247 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
248 push @source => $self->_generate_type_coercion(
250 '$type_constraints[' . $index . ']',
255 push @source => $self->_generate_type_constraint_check(
257 '$type_constraint_bodies[' . $index . ']',
258 '$type_constraints[' . $index . ']',
262 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
267 return join "\n" => @source;
270 sub _generate_slot_assignment {
271 my ($self, $attr, $value, $index) = @_;
275 if ($attr->has_initializer) {
277 '$attrs->[' . $index . ']->set_initial_value($instance, ' . $value . ');'
282 $self->meta_instance->inline_set_slot_value(
284 ("'" . $attr->name . "'"),
290 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
292 if ($is_moose && $attr->is_weak_ref) {
295 $self->meta_instance->inline_weaken_slot_value(
297 ("'" . $attr->name . "'")
299 ' if ref ' . $value . ';'
306 sub _generate_type_coercion {
307 my ($self, $attr, $type_constraint_name, $value_name, $return_value_name) = @_;
308 return ($return_value_name . ' = ' . $type_constraint_name . '->coerce(' . $value_name . ');');
311 sub _generate_type_constraint_check {
312 my ($self, $attr, $type_constraint_cv, $type_constraint_obj, $value_name) = @_;
314 $type_constraint_cv . '->(' . $value_name . ')'
315 . "\n\t" . '|| confess "Attribute ('
317 . ') does not pass the type constraint because: " . '
318 . $type_constraint_obj . '->get_message(' . $value_name . ');'
322 sub _generate_default_value {
323 my ($self, $attr, $index) = @_;
325 # default values can either be CODE refs
326 # in which case we need to call them. Or
327 # they can be scalars (strings/numbers)
328 # in which case we can just deal with them
329 # in the code we eval.
330 if ($attr->is_default_a_coderef) {
331 return '$attrs->[' . $index . ']->default($instance)';
334 my $default = $attr->default;
335 # make sure to quote strings ...
336 unless (looks_like_number($default)) {
337 $default = "'$default'";
352 Moose::Meta::Method::Constructor - Method Meta Object for constructors
356 This is a subclass of L<Class::MOP::Method> which handles
357 constructing an approprate Constructor methods. This is primarily
358 used in the making of immutable metaclasses, otherwise it is
359 not particularly useful.
369 =item B<meta_instance>
373 =item B<initialize_body>
375 =item B<associated_metaclass>
381 Stevan Little E<lt>stevan@iinteractive.comE<gt>
383 =head1 COPYRIGHT AND LICENSE
385 Copyright 2006-2008 by Infinity Interactive, Inc.
387 L<http://www.iinteractive.com>
389 This library is free software; you can redistribute it and/or modify
390 it under the same terms as Perl itself.