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