Commit | Line | Data |
565f0cbb |
1 | |
2 | package Class::MOP::Method::Generated; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
7 | use Carp 'confess'; |
8 | |
db4c4962 |
9 | our $VERSION = '0.75'; |
d519662a |
10 | $VERSION = eval $VERSION; |
565f0cbb |
11 | our $AUTHORITY = 'cpan:STEVAN'; |
12 | |
13 | use base 'Class::MOP::Method'; |
14 | |
15 | sub 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 | |
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 | |
e3a72dbc |
55 | sub _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 |
67 | sub is_inline { $_[0]{is_inline} } |
68 | |
69 | sub definition_context { $_[0]{definition_context} } |
565f0cbb |
70 | |
71 | sub initialize_body { |
72 | confess "No body to initialize, " . __PACKAGE__ . " is an abstract base class"; |
73 | } |
74 | |
75 | |
565f0cbb |
76 | 1; |
77 | |
78 | __END__ |
79 | |
80 | =pod |
81 | |
82 | =head1 NAME |
83 | |
84 | Class::MOP::Method::Generated - Abstract base class for generated methods |
85 | |
86 | =head1 DESCRIPTION |
87 | |
88 | This is a C<Class::MOP::Method> subclass which is used interally |
89 | by C<Class::MOP::Method::Accessor> and C<Class::MOP::Method::Constructor>. |
90 | |
91 | =head1 METHODS |
92 | |
93 | =over 4 |
94 | |
95 | =item B<new (%options)> |
96 | |
97 | This creates the method based on the criteria in C<%options>, |
98 | these options are: |
99 | |
100 | =over 4 |
101 | |
102 | =item I<is_inline> |
103 | |
104 | This is a boolean to indicate if the method should be generated |
105 | as a closure, or as a more optimized inline version. |
106 | |
107 | =back |
108 | |
109 | =item B<is_inline> |
110 | |
111 | This returns the boolean which was passed into C<new>. |
112 | |
113 | =item B<initialize_body> |
114 | |
115 | This is an abstract method and will throw an exception if called. |
116 | |
117 | =back |
118 | |
119 | =head1 AUTHORS |
120 | |
121 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
122 | |
123 | =head1 COPYRIGHT AND LICENSE |
124 | |
69e3ab0a |
125 | Copyright 2006-2008 by Infinity Interactive, Inc. |
565f0cbb |
126 | |
127 | L<http://www.iinteractive.com> |
128 | |
129 | This library is free software; you can redistribute it and/or modify |
130 | it under the same terms as Perl itself. |
131 | |
132 | =cut |
133 | |