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