2 package Class::MOP::Method::Constructor;
8 use Scalar::Util 'blessed', 'weaken';
10 our $VERSION = '1.04';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Class::MOP::Method::Inlined';
20 (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
21 || confess "You must pass a metaclass instance if you want to inline"
22 if $options{is_inline};
24 ($options{package_name} && $options{name})
25 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
27 my $self = $class->_new(\%options);
29 # we don't want this creating
30 # a cycle in the code, if not
32 weaken($self->{'associated_metaclass'});
34 $self->_initialize_body;
42 return Class::MOP::Class->initialize($class)->new_object(@_)
43 if $class ne __PACKAGE__;
45 my $params = @_ == 1 ? $_[0] : {@_};
48 # inherited from Class::MOP::Method
49 body => $params->{body},
50 # associated_metaclass => $params->{associated_metaclass}, # overriden
51 package_name => $params->{package_name},
52 name => $params->{name},
53 original_method => $params->{original_method},
55 # inherited from Class::MOP::Generated
56 is_inline => $params->{is_inline} || 0,
57 definition_context => $params->{definition_context},
59 # inherited from Class::MOP::Inlined
60 _expected_method_class => $params->{_expected_method_class},
62 # defined in this subclass
63 options => $params->{options} || {},
64 associated_metaclass => $params->{metaclass},
70 sub options { (shift)->{'options'} }
71 sub associated_metaclass { (shift)->{'associated_metaclass'} }
77 $self->{'meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
82 $self->{'attributes'} ||= [ $self->associated_metaclass->get_all_attributes ]
87 sub _initialize_body {
89 my $method_name = '_generate_constructor_method';
91 $method_name .= '_inline' if $self->is_inline;
93 $self->{'body'} = $self->$method_name;
96 sub _generate_constructor_method {
97 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
100 sub _generate_constructor_method_inline {
103 my $defaults = [map { $_->default } @{ $self->_attributes }];
106 '$defaults' => \$defaults,
109 my $source = 'sub {';
110 $source .= "\n" . 'my $class = shift;';
112 $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)';
113 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
115 $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};';
117 $source .= "\n" . 'my $instance = ' . $self->_meta_instance->inline_create_instance('$class');
119 $source .= ";\n" . (join ";\n" => map {
120 $self->_generate_slot_initializer($_, $idx++)
121 } @{ $self->_attributes });
122 $source .= ";\n" . 'return $instance';
123 $source .= ";\n" . '}';
124 warn $source if $self->options->{debug};
126 my ( $code, $e ) = $self->_eval_closure(
130 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e" if $e;
135 sub _generate_slot_initializer {
141 if ($attr->has_default) {
142 $default = $self->_generate_default_value($attr, $idx);
143 } elsif( $attr->has_builder ) {
144 $default = '$instance->'.$attr->builder;
147 if ( defined(my $init_arg = $attr->init_arg) ) {
149 'if(exists $params->{\'' . $init_arg . '\'}){' . "\n" .
150 $self->_meta_instance->inline_set_slot_value(
153 '$params->{\'' . $init_arg . '\'}' ) . "\n" .
154 '} ' . (!defined $default ? '' : 'else {' . "\n" .
155 $self->_meta_instance->inline_set_slot_value(
161 } elsif ( defined $default ) {
163 $self->_meta_instance->inline_set_slot_value(
171 sub _generate_default_value {
172 my ($self, $attr, $index) = @_;
174 # default values can either be CODE refs
175 # in which case we need to call them. Or
176 # they can be scalars (strings/numbers)
177 # in which case we can just deal with them
178 # in the code we eval.
179 if ($attr->is_default_a_coderef) {
180 return '$defaults->[' . $index . ']->($instance)';
183 return '$defaults->[' . $index . ']';
195 Class::MOP::Method::Constructor - Method Meta Object for constructors
199 use Class::MOP::Method::Constructor;
201 my $constructor = Class::MOP::Method::Constructor->new(
202 metaclass => $metaclass,
204 debug => 1, # this is all for now
208 # calling the constructor ...
209 $constructor->body->execute($metaclass->name, %params);
213 This is a subclass of C<Class::MOP::Method> which generates
220 =item B<< Class::MOP::Method::Constructor->new(%options) >>
222 This creates a new constructor object. It accepts a hash reference of
229 This should be a L<Class::MOP::Class> object. It is required.
233 The method name (without a package name). This is required.
237 The package name for the method. This is required.
241 This indicates whether or not the constructor should be inlined. This
246 =item B<< $metamethod->is_inline >>
248 Returns a boolean indicating whether or not the constructor is
251 =item B<< $metamethod->associated_metaclass >>
253 This returns the L<Class::MOP::Class> object for the method.
259 Stevan Little E<lt>stevan@iinteractive.comE<gt>
261 =head1 COPYRIGHT AND LICENSE
263 Copyright 2006-2010 by Infinity Interactive, Inc.
265 L<http://www.iinteractive.com>
267 This library is free software; you can redistribute it and/or modify
268 it under the same terms as Perl itself.