Merge from trunk to method_generation_cleanup branch (all tests pass)
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Generated.pm
1
2 package Class::MOP::Method::Generated;
3
4 use strict;
5 use warnings;
6
7 use Carp 'confess';
8
9 our $VERSION   = '0.75';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Class::MOP::Method';
14
15 sub new {
16     my $class   = shift;
17     my %options = @_;  
18         
19     ($options{package_name} && $options{name})
20         || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";     
21         
22     my $self = $class->_new(\%options);
23     
24     $self->initialize_body;
25     
26     return $self;
27 }
28
29
30 sub _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
55 sub _new {
56     my $class = shift;
57     my $options = @_ == 1 ? $_[0] : {@_};
58
59     $options->{is_inline} ||= 0;
60     $options->{body} ||= undef;
61
62     bless $options, $class;
63 }
64
65 ## accessors
66
67 sub is_inline { $_[0]{is_inline} }
68
69 sub definition_context { $_[0]{definition_context} }
70
71 sub initialize_body {
72     confess "No body to initialize, " . __PACKAGE__ . " is an abstract base class";
73 }
74
75 sub _eval_closure {
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     );
87 }
88
89 1;
90
91 __END__
92
93 =pod
94
95 =head1 NAME 
96
97 Class::MOP::Method::Generated - Abstract base class for generated methods
98
99 =head1 DESCRIPTION
100
101 This is a C<Class::MOP::Method> subclass which is used interally 
102 by 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
110 This creates the method based on the criteria in C<%options>, 
111 these options are:
112
113 =over 4
114
115 =item I<is_inline>
116
117 This is a boolean to indicate if the method should be generated
118 as a closure, or as a more optimized inline version.
119
120 =back
121
122 =item B<is_inline>
123
124 This returns the boolean which was passed into C<new>.
125
126 =item B<initialize_body>
127
128 This is an abstract method and will throw an exception if called.
129
130 =back
131
132 =head1 AUTHORS
133
134 Stevan Little E<lt>stevan@iinteractive.comE<gt>
135
136 =head1 COPYRIGHT AND LICENSE
137
138 Copyright 2006-2008 by Infinity Interactive, Inc.
139
140 L<http://www.iinteractive.com>
141
142 This library is free software; you can redistribute it and/or modify
143 it under the same terms as Perl itself. 
144
145 =cut
146