Move attribute triggers from Moose, breaking no compatibility
[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';
8use Scalar::Util 'blessed', 'weaken', 'looks_like_number';
9
4b18c1b3 10our $VERSION = '0.91';
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
8d2d4c67 75sub meta_instance {
c7e28c19 76 Carp::cluck('The meta_instance method has been made private.'
77 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 78 shift->_meta_instance;
5a49fb79 79}
80
81sub _meta_instance {
565f0cbb 82 my $self = shift;
8683db0e 83 $self->{'meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
565f0cbb 84}
c23184fc 85
8d2d4c67 86sub attributes {
c7e28c19 87 Carp::cluck('The attributes method has been made private.'
88 . " The public version is deprecated and will be removed in a future release.\n");
6e2f5a81 89
780f14c2 90 return shift->_attributes;
91}
92
93sub _attributes {
565f0cbb 94 my $self = shift;
2620be77 95 $self->{'attributes'} ||= [ $self->associated_metaclass->get_all_attributes ]
565f0cbb 96}
d90b42a6 97
98## method
99
565f0cbb 100sub initialize_body {
c7e28c19 101 Carp::cluck('The initialize_body method has been made private.'
102 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 103 shift->_initialize_body;
8f7852d8 104}
105
106sub _initialize_body {
565f0cbb 107 my $self = shift;
ecb874a0 108 my $method_name = '_generate_constructor_method';
8d2d4c67 109
565f0cbb 110 $method_name .= '_inline' if $self->is_inline;
8d2d4c67 111
8683db0e 112 $self->{'body'} = $self->$method_name;
565f0cbb 113}
114
115sub generate_constructor_method {
c7e28c19 116 Carp::cluck('The generate_constructor_method method has been made private.'
117 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 118 shift->_generate_constructor_method;
ecb874a0 119}
120
121sub _generate_constructor_method {
2a2b8458 122 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
565f0cbb 123}
124
7813a0c5 125sub _inline_set_slot_value{
126 my($self,$attr, $instance, $attr_var, $value) = @_;
127
128 if($attr->has_trigger){
129 return sprintf q{ my $value = %s; %s->call_trigger(%s, $value); },
130 $self->_meta_instance->inline_set_slot_value($instance, $attr->name, $value),
131 $attr_var, $instance, $value;
132 }
133 else{
134 return $self->_meta_instance->inline_set_slot_value($instance, $attr->name, $value);
135 }
136}
137
565f0cbb 138sub generate_constructor_method_inline {
c7e28c19 139 Carp::cluck('The generate_constructor_method_inline method has been made private.'
140 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 141 shift->_generate_constructor_method_inline;
ecb874a0 142}
143
144sub _generate_constructor_method_inline {
d90b42a6 145 my $self = shift;
565f0cbb 146
0c6f3280 147 my $close_over = {};
148
d90b42a6 149 my $source = 'sub {';
26ffbb36 150 $source .= "\n" . 'my $class = shift;';
8d2d4c67 151
26ffbb36 152 $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)';
8d2d4c67 153 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
154
26ffbb36 155 $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};';
156
5a49fb79 157 $source .= "\n" . 'my $instance = ' . $self->_meta_instance->inline_create_instance('$class');
8d2d4c67 158 $source .= ";\n" . (join ";\n" => map {
82cff1ae 159 $self->_generate_slot_initializer($_, $close_over)
780f14c2 160 } @{ $self->_attributes });
d90b42a6 161 $source .= ";\n" . 'return $instance';
8d2d4c67 162 $source .= ";\n" . '}';
163 warn $source if $self->options->{debug};
164
e24b19fb 165 my ( $code, $e ) = $self->_eval_closure(
ffe92c8b 166 $close_over,
167 $source
168 );
e24b19fb 169 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e" if $e;
a6eef5a3 170
171 return $code;
d90b42a6 172}
173
174sub _generate_slot_initializer {
175 my $self = shift;
64adcd8d 176 my $attr = shift;
82cff1ae 177 my $close = shift;
8d2d4c67 178
7813a0c5 179
180 my $attr_var = do{
181 my $attrs = ($close->{'@attrs'} ||= []);
182 push @{$attrs}, $attr;
183 sprintf q{$attrs[%d]}, scalar(@{$attrs}) - 1;
184 };
185
d90b42a6 186 my $default;
187 if ($attr->has_default) {
188 # NOTE:
189 # default values can either be CODE refs
8d2d4c67 190 # in which case we need to call them. Or
d90b42a6 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) {
82cff1ae 195 my $idx = @{$close->{'@defaults'}||=[]};
196 push(@{$close->{'@defaults'}}, $attr->default);
197 $default = '$defaults[' . $idx . ']->($instance)';
d90b42a6 198 }
199 else {
200 $default = $attr->default;
201 # make sure to quote strings ...
202 unless (looks_like_number($default)) {
203 $default = "'$default'";
204 }
205 }
8d2d4c67 206 } elsif( $attr->has_builder ) {
207 $default = '$instance->'.$attr->builder;
d90b42a6 208 }
8d2d4c67 209
2f2329a4 210 if ( defined(my $init_arg = $attr->init_arg) ) {
c16c9c1b 211 return (
2f2329a4 212 'if(exists $params->{\'' . $init_arg . '\'}){' . "\n" .
7813a0c5 213 $self->_inline_set_slot_value(
214 $attr,
c16c9c1b 215 '$instance',
7813a0c5 216 $attr_var,
2f2329a4 217 '$params->{\'' . $init_arg . '\'}' ) . "\n" .
c16c9c1b 218 '} ' . (!defined $default ? '' : 'else {' . "\n" .
7813a0c5 219 $self->_inline_set_slot_value(
220 $attr,
c16c9c1b 221 '$instance',
7813a0c5 222 $attr_var,
c16c9c1b 223 $default ) . "\n" .
224 '}')
225 );
226 } elsif ( defined $default ) {
227 return (
7813a0c5 228 $self->_inline_set_slot_value(
229 $attr,
c16c9c1b 230 '$instance',
7813a0c5 231 $attr_var,
c16c9c1b 232 $default ) . "\n"
233 );
234 } else { return '' }
d90b42a6 235}
236
2371;
238
d90b42a6 239__END__
240
241=pod
242
8d2d4c67 243=head1 NAME
d90b42a6 244
245Class::MOP::Method::Constructor - Method Meta Object for constructors
246
247=head1 SYNOPSIS
248
96e38ba6 249 use Class::MOP::Method::Constructor;
8d2d4c67 250
96e38ba6 251 my $constructor = Class::MOP::Method::Constructor->new(
8d2d4c67 252 metaclass => $metaclass,
96e38ba6 253 options => {
254 debug => 1, # this is all for now
8d2d4c67 255 },
96e38ba6 256 );
8d2d4c67 257
96e38ba6 258 # calling the constructor ...
b7045e66 259 $constructor->body->execute($metaclass->name, %params);
8d2d4c67 260
d90b42a6 261=head1 DESCRIPTION
262
3fd960d9 263This is a subclass of C<Class::MOP::Method> which generates
264constructor methods.
96e38ba6 265
d90b42a6 266=head1 METHODS
267
268=over 4
269
3fd960d9 270=item B<< Class::MOP::Method::Constructor->new(%options) >>
d90b42a6 271
3fd960d9 272This creates a new constructor object. It accepts a hash reference of
273options.
96e38ba6 274
3fd960d9 275=over 8
96e38ba6 276
3fd960d9 277=item * metaclass
96e38ba6 278
3fd960d9 279This should be a L<Class::MOP::Class> object. It is required.
c23184fc 280
3fd960d9 281=item * name
d90b42a6 282
3fd960d9 283The method name (without a package name). This is required.
96e38ba6 284
3fd960d9 285=item * package_name
d90b42a6 286
3fd960d9 287The package name for the method. This is required.
c23184fc 288
3fd960d9 289=item * is_inline
96e38ba6 290
3fd960d9 291This indicates whether or not the constructor should be inlined. This
292defaults to false.
d90b42a6 293
3fd960d9 294=back
f0de47d9 295
3fd960d9 296=item B<< $metamethod->is_inline >>
f0de47d9 297
3fd960d9 298Returns a boolean indicating whether or not the constructor is
299inlined.
d90b42a6 300
3fd960d9 301=item B<< $metamethod->associated_metaclass >>
96e38ba6 302
3fd960d9 303This returns the L<Class::MOP::Class> object for the method.
d90b42a6 304
565f0cbb 305=back
306
d90b42a6 307=head1 AUTHORS
308
309Stevan Little E<lt>stevan@iinteractive.comE<gt>
310
311=head1 COPYRIGHT AND LICENSE
312
070bb6c9 313Copyright 2006-2009 by Infinity Interactive, Inc.
d90b42a6 314
315L<http://www.iinteractive.com>
316
317This library is free software; you can redistribute it and/or modify
8d2d4c67 318it under the same terms as Perl itself.
d90b42a6 319
320=cut
321