2 package Moose::Meta::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
10 our $VERSION = '0.01';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Moose::Meta::Method';
19 (exists $options{options} && ref $options{options} eq 'HASH')
20 || confess "You must pass a hash of options";
25 # specific to this subclass
26 '%!options' => $options{options},
27 '$!meta_instance' => $options{metaclass}->get_meta_instance,
28 '@!attributes' => [ $options{metaclass}->compute_all_applicable_attributes ],
30 '$!associated_metaclass' => $options{metaclass},
33 # we don't want this creating
34 # a cycle in the code, if not
36 weaken($self->{'$!associated_metaclass'});
38 $self->intialize_body;
45 sub options { (shift)->{'%!options'} }
46 sub meta_instance { (shift)->{'$!meta_instance'} }
47 sub attributes { (shift)->{'@!attributes'} }
49 sub associated_metaclass { (shift)->{'$!associated_metaclass'} }
56 # the %options should also include a both
57 # a call 'initializer' and call 'SUPER::'
58 # options, which should cover approx 90%
59 # of the possible use cases (even if it
60 # requires some adaption on the part of
61 # the author, after all, nothing is free)
63 $source .= "\n" . 'my $class = shift;';
65 $source .= "\n" . 'return $class->Moose::Object::new(@_)';
66 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
68 $source .= "\n" . 'my %params = (scalar @_ == 1) ? %{$_[0]} : @_;';
70 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
72 $source .= ";\n" . (join ";\n" => map {
73 $self->_generate_slot_initializer($_)
74 } 0 .. (@{$self->attributes} - 1));
76 $source .= ";\n" . $self->_generate_BUILDALL();
78 $source .= ";\n" . 'return $instance';
79 $source .= ";\n" . '}';
80 warn $source if $self->options->{debug};
85 # create the nessecary lexicals
86 # to be picked up in the eval
87 my $attrs = $self->attributes;
90 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
92 $self->{'&!body'} = $code;
95 sub _generate_BUILDALL {
98 foreach my $method (reverse $self->associated_metaclass->find_all_methods_by_name('BUILD')) {
99 push @BUILD_calls => '$instance->' . $method->{class} . '::BUILD(\%params)';
101 return join ";\n" => @BUILD_calls;
104 sub _generate_slot_initializer {
108 my $attr = $self->attributes->[$index];
110 my @source = ('## ' . $attr->name);
112 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
114 if ($is_moose && $attr->is_required && !$attr->has_default) {
115 push @source => ('(exists $params{\'' . $attr->init_arg . '\'}) ' .
116 '|| confess "Attribute (' . $attr->name . ') is required";');
119 if ($attr->has_default && !($is_moose &&$attr->is_lazy)) {
121 push @source => 'if (exists $params{\'' . $attr->init_arg . '\'}) {';
123 push @source => ('my $val = $params{\'' . $attr->init_arg . '\'};');
124 if ($is_moose && $attr->has_type_constraint) {
125 push @source => ('my $type_constraint = $attrs->[' . $index . ']->type_constraint;');
127 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
128 push @source => $self->_generate_type_coercion($attr, '$type_constraint', '$val', '$val');
130 push @source => $self->_generate_type_constraint_check($attr, '$type_constraint', '$val');
132 push @source => $self->_generate_slot_assignment($attr, '$val');
135 push @source => "} else {";
137 my $default = $self->_generate_default_value($attr, $index);
139 push @source => ('my $val = ' . $default . ';');
140 push @source => $self->_generate_type_constraint_check(
142 ('$attrs->[' . $index . ']->type_constraint'),
144 ) if ($is_moose && $attr->has_type_constraint);
145 push @source => $self->_generate_slot_assignment($attr, $default);
150 push @source => '(exists $params{\'' . $attr->init_arg . '\'}) && do {';
152 push @source => ('my $val = $params{\'' . $attr->init_arg . '\'};');
153 if ($is_moose && $attr->has_type_constraint) {
154 push @source => ('my $type_constraint = $attrs->[' . $index . ']->type_constraint;');
156 if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
157 push @source => $self->_generate_type_coercion($attr, '$type_constraint', '$val', '$val');
159 push @source => $self->_generate_type_constraint_check($attr, '$type_constraint', '$val');
161 push @source => $self->_generate_slot_assignment($attr, '$val');
166 return join "\n" => @source;
169 sub _generate_slot_assignment {
170 my ($self, $attr, $value) = @_;
172 $self->meta_instance->inline_set_slot_value(
174 ("'" . $attr->name . "'"),
179 my $is_moose = $attr->isa('Moose::Meta::Attribute'); # XXX FIXME
181 if ($is_moose && $attr->is_weak_ref) {
184 $self->meta_instance->inline_weaken_slot_value(
186 ("'" . $attr->name . "'")
188 ' if ref ' . $value . ';'
195 sub _generate_type_coercion {
196 my ($self, $attr, $type_constraint_name, $value_name, $return_value_name) = @_;
197 return ($return_value_name . ' = ' . $type_constraint_name . '->coerce(' . $value_name . ');');
200 sub _generate_type_constraint_check {
201 my ($self, $attr, $type_constraint_name, $value_name) = @_;
203 'defined(' . $type_constraint_name . '->_compiled_type_constraint->(' . $value_name . '))'
204 . "\n\t" . '|| confess "Attribute (' . $attr->name . ') does not pass the type constraint ('
205 . $attr->type_constraint->name
206 . ') with " . (defined(' . $value_name . ') ? (overload::Overloaded(' . $value_name . ') ? overload::StrVal(' . $value_name . ') : ' . $value_name . ') : "undef");'
210 sub _generate_default_value {
211 my ($self, $attr, $index) = @_;
213 # default values can either be CODE refs
214 # in which case we need to call them. Or
215 # they can be scalars (strings/numbers)
216 # in which case we can just deal with them
217 # in the code we eval.
218 if ($attr->is_default_a_coderef) {
219 return '$attrs->[' . $index . ']->default($instance)';
222 my $default = $attr->default;
223 # make sure to quote strings ...
224 unless (looks_like_number($default)) {
225 $default = "'$default'";
242 Moose::Meta::Method::Constructor - Method Meta Object for constructors
246 This is a subclass of L<Class::MOP::Method> which handles
247 constructing an approprate Constructor methods. This is primarily
248 used in the making of immutable metaclasses, otherwise it is
249 not particularly useful.
259 =item B<meta_instance>
263 =item B<intialize_body>
265 =item B<associated_metaclass>
271 Stevan Little E<lt>stevan@iinteractive.comE<gt>
273 =head1 COPYRIGHT AND LICENSE
275 Copyright 2006, 2007 by Infinity Interactive, Inc.
277 L<http://www.iinteractive.com>
279 This library is free software; you can redistribute it and/or modify
280 it under the same terms as Perl itself.