start pushing constructor inlining back into the metaclass
[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
4b921e6b 92sub _eval_environment {
d90b42a6 93 my $self = shift;
8343d501 94 my $defaults = [map { $_->default } @{ $self->_attributes }];
4b921e6b 95 return {
8343d501 96 '$defaults' => \$defaults,
97 };
4b921e6b 98}
99
1322c26b 100sub _generate_constructor_method {
101 return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
102}
103
4b921e6b 104sub _generate_constructor_method_inline {
105 my $self = shift;
0c6f3280 106
777b1f18 107 my $meta = $self->associated_metaclass;
26ffbb36 108
777b1f18 109 my @source = (
110 'sub {',
1322c26b 111 $meta->_inline_new_object,
777b1f18 112 '}',
113 );
114
115 warn join("\n", @source) if $self->options->{debug};
8d2d4c67 116
15961c86 117 my $code = try {
4b921e6b 118 $self->_compile_code(\@source);
15961c86 119 }
120 catch {
777b1f18 121 my $source = join("\n", @source);
15961c86 122 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$_";
123 };
a6eef5a3 124
125 return $code;
d90b42a6 126}
127
d90b42a6 1281;
129
d90b42a6 130__END__
131
132=pod
133
8d2d4c67 134=head1 NAME
d90b42a6 135
136Class::MOP::Method::Constructor - Method Meta Object for constructors
137
138=head1 SYNOPSIS
139
96e38ba6 140 use Class::MOP::Method::Constructor;
8d2d4c67 141
96e38ba6 142 my $constructor = Class::MOP::Method::Constructor->new(
8d2d4c67 143 metaclass => $metaclass,
96e38ba6 144 options => {
145 debug => 1, # this is all for now
8d2d4c67 146 },
96e38ba6 147 );
8d2d4c67 148
96e38ba6 149 # calling the constructor ...
b7045e66 150 $constructor->body->execute($metaclass->name, %params);
8d2d4c67 151
d90b42a6 152=head1 DESCRIPTION
153
3fd960d9 154This is a subclass of C<Class::MOP::Method> which generates
155constructor methods.
96e38ba6 156
d90b42a6 157=head1 METHODS
158
159=over 4
160
3fd960d9 161=item B<< Class::MOP::Method::Constructor->new(%options) >>
d90b42a6 162
3fd960d9 163This creates a new constructor object. It accepts a hash reference of
164options.
96e38ba6 165
3fd960d9 166=over 8
96e38ba6 167
3fd960d9 168=item * metaclass
96e38ba6 169
3fd960d9 170This should be a L<Class::MOP::Class> object. It is required.
c23184fc 171
3fd960d9 172=item * name
d90b42a6 173
3fd960d9 174The method name (without a package name). This is required.
96e38ba6 175
3fd960d9 176=item * package_name
d90b42a6 177
3fd960d9 178The package name for the method. This is required.
c23184fc 179
3fd960d9 180=item * is_inline
96e38ba6 181
3fd960d9 182This indicates whether or not the constructor should be inlined. This
183defaults to false.
d90b42a6 184
3fd960d9 185=back
f0de47d9 186
3fd960d9 187=item B<< $metamethod->is_inline >>
f0de47d9 188
3fd960d9 189Returns a boolean indicating whether or not the constructor is
190inlined.
d90b42a6 191
3fd960d9 192=item B<< $metamethod->associated_metaclass >>
96e38ba6 193
3fd960d9 194This returns the L<Class::MOP::Class> object for the method.
d90b42a6 195
565f0cbb 196=back
197
d90b42a6 198=head1 AUTHORS
199
200Stevan Little E<lt>stevan@iinteractive.comE<gt>
201
202=head1 COPYRIGHT AND LICENSE
203
3e2c8600 204Copyright 2006-2010 by Infinity Interactive, Inc.
d90b42a6 205
206L<http://www.iinteractive.com>
207
208This library is free software; you can redistribute it and/or modify
8d2d4c67 209it under the same terms as Perl itself.
d90b42a6 210
211=cut
212