add _compile_code, a wrapper for _eval_closure and _add_line_directive
[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 sub _new {
30     my $class = shift;
31     my $options = @_ == 1 ? $_[0] : {@_};
32
33     $options->{is_inline} ||= 0;
34     $options->{body} ||= undef;
35
36     bless $options, $class;
37 }
38
39 ## accessors
40
41 sub is_inline { $_[0]{is_inline} }
42
43 sub definition_context { $_[0]{definition_context} }
44
45 sub initialize_body {
46     confess "No body to initialize, " . __PACKAGE__ . " is an abstract base class";
47 }
48
49 sub _eval_closure {
50     # my ($self, $captures, $sub_body) = @_;
51     my $__captures = $_[1];
52     eval join(
53         "\n",
54         (map {
55             /^([\@\%\$])/
56                 or die "capture key should start with \@, \% or \$: $_";
57             q!my !.$_.q! = !.$1.q!{$__captures->{'!.$_.q!'}};!;
58         } keys %$__captures),
59         $_[2]
60     );
61 }
62
63 sub _add_line_directive {
64     my ( $self, %args ) = @_;
65
66     my ( $line, $file );
67
68     if ( my $ctx = ( $args{context} || $self->definition_context ) ) {
69         $line = $ctx->{line};
70         if ( my $desc = $ctx->{description} ) {
71             $file = "$desc defined at $ctx->{file}";
72         } else {
73             $file = $ctx->{file};
74         }
75     } else {
76         ( $line, $file ) = ( 0, "generated method (unknown origin)" );
77     }
78
79     my $code = $args{code};
80
81     # if it's an array of lines, join it up
82     # don't use newlines so that the definition context is more meaningful
83     $code = join(@$code, ' ') if ref $code;
84
85     return qq{#line $line "$file"\n} . $code;
86 }
87
88 sub _compile_code {
89     my ( $self, %args ) = @_;
90
91     my $code = $self->_add_line_directive(%args);
92
93     $self->_eval_closure($args{environment}, $code);
94 }
95
96 1;
97
98 __END__
99
100 =pod
101
102 =head1 NAME 
103
104 Class::MOP::Method::Generated - Abstract base class for generated methods
105
106 =head1 DESCRIPTION
107
108 This is a C<Class::MOP::Method> subclass which is used interally 
109 by C<Class::MOP::Method::Accessor> and C<Class::MOP::Method::Constructor>.
110
111 =head1 METHODS
112
113 =over 4
114
115 =item B<new (%options)>
116
117 This creates the method based on the criteria in C<%options>, 
118 these options are:
119
120 =over 4
121
122 =item I<is_inline>
123
124 This is a boolean to indicate if the method should be generated
125 as a closure, or as a more optimized inline version.
126
127 =back
128
129 =item B<is_inline>
130
131 This returns the boolean which was passed into C<new>.
132
133 =item B<initialize_body>
134
135 This is an abstract method and will throw an exception if called.
136
137 =back
138
139 =head1 AUTHORS
140
141 Stevan Little E<lt>stevan@iinteractive.comE<gt>
142
143 =head1 COPYRIGHT AND LICENSE
144
145 Copyright 2006-2008 by Infinity Interactive, Inc.
146
147 L<http://www.iinteractive.com>
148
149 This library is free software; you can redistribute it and/or modify
150 it under the same terms as Perl itself. 
151
152 =cut
153