Small refactoring to simplify generating code a bit
[gitmo/Moose.git] / lib / Moose / Meta / Method / Destructor.pm
1
2 package Moose::Meta::Method::Destructor;
3
4 use strict;
5 use warnings;
6
7 use Scalar::Util 'blessed', 'weaken';
8
9 our $VERSION   = '0.89_02';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Moose::Meta::Method',
14          'Class::MOP::Method::Inlined';
15
16 sub new {
17     my $class   = shift;
18     my %options = @_;
19
20     (ref $options{options} eq 'HASH')
21         || $class->throw_error("You must pass a hash of options", data => $options{options});
22
23     ($options{package_name} && $options{name})
24         || $class->throw_error("You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT");
25
26     my $self = bless {
27         # from our superclass
28         'body'                 => undef,
29         'package_name'         => $options{package_name},
30         'name'                 => $options{name},
31         # ...
32         'options'              => $options{options},
33         'associated_metaclass' => $options{metaclass},
34     } => $class;
35
36     # we don't want this creating
37     # a cycle in the code, if not
38     # needed
39     weaken($self->{'associated_metaclass'});
40
41     $self->_initialize_body;
42
43     return $self;
44 }
45
46 ## accessors
47
48 sub options              { (shift)->{'options'}              }
49
50 ## method
51
52 sub is_needed {
53     my $self      = shift;
54     my $metaclass = shift;
55
56     ( blessed $metaclass && $metaclass->isa('Class::MOP::Class') )
57         || $self->throw_error(
58         "The is_needed method expected a metaclass object as its arugment");
59
60     return $metaclass->find_method_by_name("DEMOLISHALL");
61 }
62
63 sub initialize_body {
64     Carp::cluck('The initialize_body method has been made private.'
65         . " The public version is deprecated and will be removed in a future release.\n");
66     shift->_initialize_body;
67 }
68
69 sub _initialize_body {
70     my $self = shift;
71     # TODO:
72     # the %options should also include a both
73     # a call 'initializer' and call 'SUPER::'
74     # options, which should cover approx 90%
75     # of the possible use cases (even if it
76     # requires some adaption on the part of
77     # the author, after all, nothing is free)
78
79     my @DEMOLISH_methods = $self->associated_metaclass->find_all_methods_by_name('DEMOLISH');
80
81     my $source;
82     if ( @DEMOLISH_methods ) {
83         $source = 'sub {';
84         $source .= 'local ( $., $@, $!, $^E, $? );' . "\n";
85
86         $source
87             .= join ";\n" =>
88             map { '$_[0]->' . $_->{class} . '::DEMOLISH()' }
89             @DEMOLISH_methods;
90
91         $source .= ";\n" . '}';
92     } else {
93         $source = 'sub { }';
94     }
95
96     warn $source if $self->options->{debug};
97
98     my ( $code, $e ) = $self->_compile_code(
99         environment => {},
100         code => $source,
101     );
102
103     $self->throw_error(
104         "Could not eval the destructor :\n\n$source\n\nbecause :\n\n$e",
105         error => $e, data => $source )
106         if $e;
107
108     $self->{'body'} = $code;
109 }
110
111
112 1;
113
114 __END__
115
116 =pod
117
118 =head1 NAME
119
120 Moose::Meta::Method::Destructor - Method Meta Object for destructors
121
122 =head1 DESCRIPTION
123
124 This class is a subclass of L<Class::MOP::Class::Generated> that
125 provides Moose-specific functionality for inlining destructors.
126
127 To understand this class, you should read the the
128 L<Class::MOP::Class::Generated> documentation as well.
129
130 =head1 INHERITANCE
131
132 C<Moose::Meta::Method::Destructor> is a subclass of
133 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Generated>.
134
135 =head1 METHODS
136
137 =over 4
138
139 =item B<< Moose::Meta;:Method::Destructor->new(%options) >>
140
141 This constructs a new object. It accepts the following options:
142
143 =over 8
144
145 =item * package_name
146
147 The package for the class in which the destructor is being
148 inlined. This option is required.
149
150 =item * name
151
152 The name of the destructor method. This option is required.
153
154 =item * metaclass
155
156 The metaclass for the class this destructor belongs to. This is
157 optional, as it can be set later by calling C<<
158 $metamethod->attach_to_class >>.
159
160 =back
161
162 =item B<< Moose::Meta;:Method::Destructor->is_needed($metaclass) >>
163
164 Given a L<Moose::Meta::Class> object, this method returns a boolean
165 indicating whether the class needs a destructor. If the class or any
166 of its parents defines a C<DEMOLISH> method, it needs a destructor.
167
168 =back
169
170 =head1 AUTHORS
171
172 Stevan Little E<lt>stevan@iinteractive.comE<gt>
173
174 =head1 COPYRIGHT AND LICENSE
175
176 Copyright 2006-2009 by Infinity Interactive, Inc.
177
178 L<http://www.iinteractive.com>
179
180 This library is free software; you can redistribute it and/or modify
181 it under the same terms as Perl itself.
182
183 =cut
184