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