Merge from trunk to method_generation_cleanup branch (all tests pass)
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Generated.pm
CommitLineData
565f0cbb 1
2package Class::MOP::Method::Generated;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8
db4c4962 9our $VERSION = '0.75';
d519662a 10$VERSION = eval $VERSION;
565f0cbb 11our $AUTHORITY = 'cpan:STEVAN';
12
13use base 'Class::MOP::Method';
14
15sub new {
16 my $class = shift;
17 my %options = @_;
18
b38f3848 19 ($options{package_name} && $options{name})
32202ce2 20 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
b38f3848 21
0bfc85b8 22 my $self = $class->_new(\%options);
565f0cbb 23
24 $self->initialize_body;
25
26 return $self;
27}
28
d9d99689 29
30sub _prepare_code {
31 my ( $self, %args ) = @_;
32
33 my ( $line, $file );
34
35 if ( my $ctx = ( $args{context} || $self->definition_context ) ) {
36 $line = $ctx->{line};
37 if ( my $desc = $ctx->{description} ) {
38 $file = "$desc defined at $ctx->{file}";
39 } else {
40 $file = $ctx->{file};
41 }
42 } else {
43 ( $line, $file ) = ( 0, "generated method (unknown origin)" );
44 }
45
46 my $code = $args{code};
47
48 # if it's an array of lines, join it up
49 # don't use newlines so that the definition context is more meaningful
50 $code = join(@$code, ' ') if ref $code;
51
52 return qq{#line $line "$file"\n} . $code;
53}
54
e3a72dbc 55sub _new {
0bfc85b8 56 my $class = shift;
57 my $options = @_ == 1 ? $_[0] : {@_};
e3a72dbc 58
0bfc85b8 59 $options->{is_inline} ||= 0;
60 $options->{body} ||= undef;
e3a72dbc 61
0bfc85b8 62 bless $options, $class;
e3a72dbc 63}
64
565f0cbb 65## accessors
66
d9d99689 67sub is_inline { $_[0]{is_inline} }
68
69sub definition_context { $_[0]{definition_context} }
565f0cbb 70
71sub initialize_body {
72 confess "No body to initialize, " . __PACKAGE__ . " is an abstract base class";
73}
74
7f8de9b4 75sub _eval_closure {
0c6f3280 76 # my ($self, $captures, $sub_body) = @_;
77 my $__captures = $_[1];
78 eval join(
79 "\n",
80 (map {
81 /^([\@\%\$])/
82 or die "capture key should start with \@, \% or \$: $_";
83 q!my !.$_.q! = !.$1.q!{$__captures->{'!.$_.q!'}};!;
84 } keys %$__captures),
85 $_[2]
86 );
7f8de9b4 87}
565f0cbb 88
891;
90
91__END__
92
93=pod
94
95=head1 NAME
96
97Class::MOP::Method::Generated - Abstract base class for generated methods
98
99=head1 DESCRIPTION
100
101This is a C<Class::MOP::Method> subclass which is used interally
102by C<Class::MOP::Method::Accessor> and C<Class::MOP::Method::Constructor>.
103
104=head1 METHODS
105
106=over 4
107
108=item B<new (%options)>
109
110This creates the method based on the criteria in C<%options>,
111these options are:
112
113=over 4
114
115=item I<is_inline>
116
117This is a boolean to indicate if the method should be generated
118as a closure, or as a more optimized inline version.
119
120=back
121
122=item B<is_inline>
123
124This returns the boolean which was passed into C<new>.
125
126=item B<initialize_body>
127
128This is an abstract method and will throw an exception if called.
129
130=back
131
132=head1 AUTHORS
133
134Stevan Little E<lt>stevan@iinteractive.comE<gt>
135
136=head1 COPYRIGHT AND LICENSE
137
69e3ab0a 138Copyright 2006-2008 by Infinity Interactive, Inc.
565f0cbb 139
140L<http://www.iinteractive.com>
141
142This library is free software; you can redistribute it and/or modify
143it under the same terms as Perl itself.
144
145=cut
146