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