2 package Moose::Meta::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
10 our $VERSION = '0.10';
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";
26 # specific to this subclass
27 '%!options' => $options{options},
28 '$!meta_instance' => $options{metaclass}->get_meta_instance,
29 '@!attributes' => [ $options{metaclass}->compute_all_applicable_attributes ],
31 '$!associated_metaclass' => $options{metaclass},
34 # we don't want this creating
35 # a cycle in the code, if not
37 weaken($self->{'$!associated_metaclass'});
39 $self->initialize_body;
46 sub options { (shift)->{'%!options'} }
47 sub meta_instance { (shift)->{'$!meta_instance'} }
48 sub attributes { (shift)->{'@!attributes'} }
50 sub associated_metaclass { (shift)->{'$!associated_metaclass'} }
57 # the %options should also include a both
58 # a call 'initializer' and call 'SUPER::'
59 # options, which should cover approx 90%
60 # of the possible use cases (even if it
61 # requires some adaption on the part of
62 # the author, after all, nothing is free)
64 $source .= "\n" . 'my $class = shift;';
66 $source .= "\n" . 'return $class->Moose::Object::new(@_)';
67 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
69 $source .= "\n" . 'my %params = (scalar @_ == 1) ? %{$_[0]} : @_;';
71 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
73 $source .= ";\n" . (join ";\n" => map {
74 $self->_generate_slot_initializer($_)
75 } 0 .. (@{$self->attributes} - 1));
77 $source .= ";\n" . $self->_generate_triggers();
78 $source .= ";\n" . $self->_generate_BUILDALL();
80 $source .= ";\n" . 'return $instance';
81 $source .= ";\n" . '}';
82 warn $source if $self->options->{debug};
87 # create the nessecary lexicals
88 # to be picked up in the eval
89 my $attrs = $self->attributes;
91 # We need to check if the attribute ->can('type_constraint')
92 # since we may be trying to immutabilize a Moose meta class,
93 # which in turn has attributes which are Class::MOP::Attribute
94 # objects, rather than Moose::Meta::Attribute. And
95 # Class::MOP::Attribute attributes have no type constraints.
96 # However we need to make sure we leave an undef value there
97 # because the inlined code is using the index of the attributes
98 # to determine where to find the type constraint
100 my @type_constraints = map {
101 $_->can('type_constraint') ? $_->type_constraint : undef
104 my @type_constraint_bodies = map {
105 defined $_ ? $_->_compiled_type_constraint : undef;
108 $code = eval $source;
109 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
111 $self->{'&!body'} = $code;
114 sub _generate_BUILDALL {
117 foreach my $method (reverse $self->associated_metaclass->find_all_methods_by_name('BUILD')) {
118 push @BUILD_calls => '$instance->' . $method->{class} . '::BUILD(\%params)';
120 return join ";\n" => @BUILD_calls;
123 sub _generate_triggers {
126 foreach my $i (0 .. $#{ $self->attributes }) {
127 my $attr = $self->attributes->[$i];
128 if ($attr->can('has_trigger') && $attr->has_trigger) {
129 if (defined(my $init_arg = $attr->init_arg)) {
130 push @trigger_calls => (
131 '(exists $params{\'' . $init_arg . '\'}) && do {' . "\n "
132 . '$attrs->[' . $i . ']->trigger->('
134 . $self->meta_instance->inline_get_slot_value(
136 ("'" . $attr->name . "'")
139 . '$attrs->[' . $i . ']'
146 return join ";\n" => @trigger_calls;
149 sub _generate_slot_initializer {
153 my $attr = $self->attributes->[$index];
155 my @source = ('## ' . $attr->name);
157 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
159 if ($is_moose && defined($attr->init_arg) && $attr->is_required && !$attr->has_default && !$attr->has_builder) {
160 push @source => ('(exists $params{\'' . $attr->init_arg . '\'}) ' .
161 '|| confess "Attribute (' . $attr->name . ') is required";');
164 if (($attr->has_default || $attr->has_builder) && !($is_moose && $attr->is_lazy)) {
166 if ( defined( my $init_arg = $attr->init_arg ) ) {
167 push @source => 'if (exists $params{\'' . $init_arg . '\'}) {';
169 push @source => ('my $val = $params{\'' . $init_arg . '\'};');
171 if ($is_moose && $attr->has_type_constraint) {
172 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
173 push @source => $self->_generate_type_coercion(
175 '$type_constraints[' . $index . ']',
180 push @source => $self->_generate_type_constraint_check(
182 '$type_constraint_bodies[' . $index . ']',
183 '$type_constraints[' . $index . ']',
187 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
189 push @source => "} else {";
192 if ( $attr->has_default ) {
193 $default = $self->_generate_default_value($attr, $index);
196 my $builder = $attr->builder;
197 $default = '$instance->' . $builder;
200 push @source => '{'; # wrap this to avoid my $val overwrite warnings
201 push @source => ('my $val = ' . $default . ';');
202 push @source => $self->_generate_type_constraint_check(
204 ('$type_constraint_bodies[' . $index . ']'),
205 ('$type_constraints[' . $index . ']'),
207 ) if ($is_moose && $attr->has_type_constraint);
209 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
210 push @source => '}'; # close - wrap this to avoid my $val overrite warnings
212 push @source => "}" if defined $attr->init_arg;
214 elsif ( defined( my $init_arg = $attr->init_arg ) ) {
215 push @source => '(exists $params{\'' . $init_arg . '\'}) && do {';
217 push @source => ('my $val = $params{\'' . $init_arg . '\'};');
218 if ($is_moose && $attr->has_type_constraint) {
219 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
220 push @source => $self->_generate_type_coercion(
222 '$type_constraints[' . $index . ']',
227 push @source => $self->_generate_type_constraint_check(
229 '$type_constraint_bodies[' . $index . ']',
230 '$type_constraints[' . $index . ']',
234 push @source => $self->_generate_slot_assignment($attr, '$val', $index);
239 return join "\n" => @source;
242 sub _generate_slot_assignment {
243 my ($self, $attr, $value, $index) = @_;
247 if ($attr->has_initializer) {
249 '$attrs->[' . $index . ']->set_initial_value($instance, ' . $value . ');'
254 $self->meta_instance->inline_set_slot_value(
256 ("'" . $attr->name . "'"),
262 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
264 if ($is_moose && $attr->is_weak_ref) {
267 $self->meta_instance->inline_weaken_slot_value(
269 ("'" . $attr->name . "'")
271 ' if ref ' . $value . ';'
278 sub _generate_type_coercion {
279 my ($self, $attr, $type_constraint_name, $value_name, $return_value_name) = @_;
280 return ($return_value_name . ' = ' . $type_constraint_name . '->coerce(' . $value_name . ');');
283 sub _generate_type_constraint_check {
284 my ($self, $attr, $type_constraint_cv, $type_constraint_obj, $value_name) = @_;
286 $type_constraint_cv . '->(' . $value_name . ')'
287 . "\n\t" . '|| confess "Attribute ('
289 . ') does not pass the type constraint because: " . '
290 . $type_constraint_obj . '->get_message(' . $value_name . ');'
294 sub _generate_default_value {
295 my ($self, $attr, $index) = @_;
297 # default values can either be CODE refs
298 # in which case we need to call them. Or
299 # they can be scalars (strings/numbers)
300 # in which case we can just deal with them
301 # in the code we eval.
302 if ($attr->is_default_a_coderef) {
303 return '$attrs->[' . $index . ']->default($instance)';
306 my $default = $attr->default;
307 # make sure to quote strings ...
308 unless (looks_like_number($default)) {
309 $default = "'$default'";
324 Moose::Meta::Method::Constructor - Method Meta Object for constructors
328 This is a subclass of L<Class::MOP::Method> which handles
329 constructing an approprate Constructor methods. This is primarily
330 used in the making of immutable metaclasses, otherwise it is
331 not particularly useful.
341 =item B<meta_instance>
345 =item B<initialize_body>
347 =item B<associated_metaclass>
353 Stevan Little E<lt>stevan@iinteractive.comE<gt>
355 =head1 COPYRIGHT AND LICENSE
357 Copyright 2006-2008 by Infinity Interactive, Inc.
359 L<http://www.iinteractive.com>
361 This library is free software; you can redistribute it and/or modify
362 it under the same terms as Perl itself.