Bump version to 1.08
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Constructor.pm
CommitLineData
d90b42a6 1
2package Class::MOP::Method::Constructor;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8343d501 8use Scalar::Util 'blessed', 'weaken';
d90b42a6 9
2440993d 10our $VERSION = '1.08';
d519662a 11$VERSION = eval $VERSION;
d90b42a6 12our $AUTHORITY = 'cpan:STEVAN';
13
29d4e92a 14use base 'Class::MOP::Method::Inlined';
d90b42a6 15
16sub new {
17 my $class = shift;
18 my %options = @_;
8d2d4c67 19
ad315b75 20 (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
21 || confess "You must pass a metaclass instance if you want to inline"
8d2d4c67 22 if $options{is_inline};
23
b38f3848 24 ($options{package_name} && $options{name})
32202ce2 25 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
b38f3848 26
0bfc85b8 27 my $self = $class->_new(\%options);
d90b42a6 28
8d2d4c67 29 # we don't want this creating
30 # a cycle in the code, if not
d90b42a6 31 # needed
8683db0e 32 weaken($self->{'associated_metaclass'});
d90b42a6 33
8f7852d8 34 $self->_initialize_body;
d90b42a6 35
8d2d4c67 36 return $self;
d90b42a6 37}
38
28b97bef 39sub _new {
0bfc85b8 40 my $class = shift;
812d58f9 41
ec9e38e5 42 return Class::MOP::Class->initialize($class)->new_object(@_)
812d58f9 43 if $class ne __PACKAGE__;
ec9e38e5 44
45 my $params = @_ == 1 ? $_[0] : {@_};
46
47 return bless {
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},
54
55 # inherited from Class::MOP::Generated
56 is_inline => $params->{is_inline} || 0,
57 definition_context => $params->{definition_context},
58
59 # inherited from Class::MOP::Inlined
60 _expected_method_class => $params->{_expected_method_class},
61
62 # defined in this subclass
63 options => $params->{options} || {},
64 associated_metaclass => $params->{metaclass},
28b97bef 65 }, $class;
66}
67
8d2d4c67 68## accessors
c23184fc 69
8683db0e 70sub options { (shift)->{'options'} }
71sub associated_metaclass { (shift)->{'associated_metaclass'} }
c23184fc 72
565f0cbb 73## cached values ...
d90b42a6 74
5a49fb79 75sub _meta_instance {
565f0cbb 76 my $self = shift;
8683db0e 77 $self->{'meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
565f0cbb 78}
c23184fc 79
780f14c2 80sub _attributes {
565f0cbb 81 my $self = shift;
2620be77 82 $self->{'attributes'} ||= [ $self->associated_metaclass->get_all_attributes ]
565f0cbb 83}
d90b42a6 84
85## method
86
8f7852d8 87sub _initialize_body {
565f0cbb 88 my $self = shift;
ecb874a0 89 my $method_name = '_generate_constructor_method';
8d2d4c67 90
565f0cbb 91 $method_name .= '_inline' if $self->is_inline;
8d2d4c67 92
8683db0e 93 $self->{'body'} = $self->$method_name;
565f0cbb 94}
95
ecb874a0 96sub _generate_constructor_method {
2a2b8458 97 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
565f0cbb 98}
99
ecb874a0 100sub _generate_constructor_method_inline {
d90b42a6 101 my $self = shift;
565f0cbb 102
8343d501 103 my $defaults = [map { $_->default } @{ $self->_attributes }];
104
105 my $close_over = {
106 '$defaults' => \$defaults,
107 };
0c6f3280 108
d90b42a6 109 my $source = 'sub {';
26ffbb36 110 $source .= "\n" . 'my $class = shift;';
8d2d4c67 111
26ffbb36 112 $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)';
8d2d4c67 113 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
114
26ffbb36 115 $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};';
116
5a49fb79 117 $source .= "\n" . 'my $instance = ' . $self->_meta_instance->inline_create_instance('$class');
8343d501 118 my $idx = 0;
8d2d4c67 119 $source .= ";\n" . (join ";\n" => map {
8343d501 120 $self->_generate_slot_initializer($_, $idx++)
780f14c2 121 } @{ $self->_attributes });
d90b42a6 122 $source .= ";\n" . 'return $instance';
8d2d4c67 123 $source .= ";\n" . '}';
124 warn $source if $self->options->{debug};
125
e24b19fb 126 my ( $code, $e ) = $self->_eval_closure(
ffe92c8b 127 $close_over,
128 $source
129 );
e24b19fb 130 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e" if $e;
a6eef5a3 131
132 return $code;
d90b42a6 133}
134
135sub _generate_slot_initializer {
136 my $self = shift;
64adcd8d 137 my $attr = shift;
8343d501 138 my $idx = shift;
8d2d4c67 139
d90b42a6 140 my $default;
141 if ($attr->has_default) {
8343d501 142 $default = $self->_generate_default_value($attr, $idx);
8d2d4c67 143 } elsif( $attr->has_builder ) {
144 $default = '$instance->'.$attr->builder;
d90b42a6 145 }
8d2d4c67 146
e42e1c47 147 if ( defined( my $init_arg = $attr->init_arg ) ) {
63fc4b5a 148 my $mi = $self->_meta_instance;
149 my $attr_name = $attr->name;
150
e42e1c47 151 return (
152 'if(exists $params->{\''
153 . $init_arg . '\'}){' . "\n"
63fc4b5a 154 . $mi->inline_set_slot_value(
e42e1c47 155 '$instance',
63fc4b5a 156 $attr_name,
e42e1c47 157 '$params->{\'' . $init_arg . '\'}'
158 )
159 . "\n" . '} '
160 . (
161 !defined $default ? '' : 'else {' . "\n"
63fc4b5a 162 . $mi->inline_set_slot_value(
c16c9c1b 163 '$instance',
63fc4b5a 164 $attr_name,
e42e1c47 165 $default
166 )
167 . "\n" . '}'
168 )
c16c9c1b 169 );
e42e1c47 170 }
171 elsif ( defined $default ) {
c16c9c1b 172 return (
5a49fb79 173 $self->_meta_instance->inline_set_slot_value(
c16c9c1b 174 '$instance',
e9a19694 175 $attr->name,
e42e1c47 176 $default
177 )
178 . "\n"
c16c9c1b 179 );
e42e1c47 180 }
181 else {
182 return '';
183 }
d90b42a6 184}
185
8343d501 186sub _generate_default_value {
187 my ($self, $attr, $index) = @_;
188 # NOTE:
189 # default values can either be CODE refs
190 # in which case we need to call them. Or
191 # they can be scalars (strings/numbers)
192 # in which case we can just deal with them
193 # in the code we eval.
194 if ($attr->is_default_a_coderef) {
195 return '$defaults->[' . $index . ']->($instance)';
196 }
197 else {
198 return '$defaults->[' . $index . ']';
199 }
200}
201
d90b42a6 2021;
203
d90b42a6 204__END__
205
206=pod
207
8d2d4c67 208=head1 NAME
d90b42a6 209
210Class::MOP::Method::Constructor - Method Meta Object for constructors
211
212=head1 SYNOPSIS
213
96e38ba6 214 use Class::MOP::Method::Constructor;
8d2d4c67 215
96e38ba6 216 my $constructor = Class::MOP::Method::Constructor->new(
8d2d4c67 217 metaclass => $metaclass,
96e38ba6 218 options => {
219 debug => 1, # this is all for now
8d2d4c67 220 },
96e38ba6 221 );
8d2d4c67 222
96e38ba6 223 # calling the constructor ...
b7045e66 224 $constructor->body->execute($metaclass->name, %params);
8d2d4c67 225
d90b42a6 226=head1 DESCRIPTION
227
3fd960d9 228This is a subclass of C<Class::MOP::Method> which generates
229constructor methods.
96e38ba6 230
d90b42a6 231=head1 METHODS
232
233=over 4
234
3fd960d9 235=item B<< Class::MOP::Method::Constructor->new(%options) >>
d90b42a6 236
3fd960d9 237This creates a new constructor object. It accepts a hash reference of
238options.
96e38ba6 239
3fd960d9 240=over 8
96e38ba6 241
3fd960d9 242=item * metaclass
96e38ba6 243
3fd960d9 244This should be a L<Class::MOP::Class> object. It is required.
c23184fc 245
3fd960d9 246=item * name
d90b42a6 247
3fd960d9 248The method name (without a package name). This is required.
96e38ba6 249
3fd960d9 250=item * package_name
d90b42a6 251
3fd960d9 252The package name for the method. This is required.
c23184fc 253
3fd960d9 254=item * is_inline
96e38ba6 255
3fd960d9 256This indicates whether or not the constructor should be inlined. This
257defaults to false.
d90b42a6 258
3fd960d9 259=back
f0de47d9 260
3fd960d9 261=item B<< $metamethod->is_inline >>
f0de47d9 262
3fd960d9 263Returns a boolean indicating whether or not the constructor is
264inlined.
d90b42a6 265
3fd960d9 266=item B<< $metamethod->associated_metaclass >>
96e38ba6 267
3fd960d9 268This returns the L<Class::MOP::Class> object for the method.
d90b42a6 269
565f0cbb 270=back
271
d90b42a6 272=head1 AUTHORS
273
274Stevan Little E<lt>stevan@iinteractive.comE<gt>
275
276=head1 COPYRIGHT AND LICENSE
277
3e2c8600 278Copyright 2006-2010 by Infinity Interactive, Inc.
d90b42a6 279
280L<http://www.iinteractive.com>
281
282This library is free software; you can redistribute it and/or modify
8d2d4c67 283it under the same terms as Perl itself.
d90b42a6 284
285=cut
286