make ::Constructor readable
[gitmo/Moose.git] / lib / Moose / Meta / Method / Destructor.pm
CommitLineData
acf10771 1
2package Moose::Meta::Method::Destructor;
3
4use strict;
5use warnings;
6
b288593e 7use Devel::GlobalDestruction ();
acf10771 8use Scalar::Util 'blessed', 'weaken';
55c361dc 9use Try::Tiny;
acf10771 10
245478d5 11our $VERSION = '1.19';
e606ae5f 12$VERSION = eval $VERSION;
acf10771 13our $AUTHORITY = 'cpan:STEVAN';
14
badb7e89 15use base 'Moose::Meta::Method',
f4aef447 16 'Class::MOP::Method::Inlined';
acf10771 17
18sub new {
19 my $class = shift;
20 my %options = @_;
d03bd989 21
46cb090f 22 (ref $options{options} eq 'HASH')
23 || $class->throw_error("You must pass a hash of options", data => $options{options});
24
1b2aea39 25 ($options{package_name} && $options{name})
46cb090f 26 || $class->throw_error("You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT");
27
acf10771 28 my $self = bless {
29 # from our superclass
d03bd989 30 'body' => undef,
e606ae5f 31 'package_name' => $options{package_name},
d03bd989 32 'name' => $options{name},
acf10771 33 # ...
d03bd989 34 'options' => $options{options},
e606ae5f 35 'associated_metaclass' => $options{metaclass},
acf10771 36 } => $class;
37
d03bd989 38 # we don't want this creating
39 # a cycle in the code, if not
acf10771 40 # needed
d03bd989 41 weaken($self->{'associated_metaclass'});
acf10771 42
bfaa304c 43 $self->_initialize_body;
acf10771 44
d03bd989 45 return $self;
acf10771 46}
47
d03bd989 48## accessors
acf10771 49
e606ae5f 50sub options { (shift)->{'options'} }
acf10771 51
52## method
53
a7b097bb 54sub 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
8c86556c 62 return $metaclass->find_method_by_name("DEMOLISHALL");
9d22affb 63}
acf10771 64
415e6f85 65sub initialize_body {
8b7cb9ab 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");
bfaa304c 68 shift->_initialize_body;
69}
70
71sub _initialize_body {
acf10771 72 my $self = shift;
73 # TODO:
d03bd989 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
acf10771 79 # the author, after all, nothing is free)
d03bd989 80
9d22affb 81 my @DEMOLISH_methods = $self->associated_metaclass->find_all_methods_by_name('DEMOLISH');
d03bd989 82
8c86556c 83 my $source;
25818121 84 $source = 'sub {' . "\n";
85 $source .= 'my $self = shift;' . "\n";
86 $source .= 'return $self->Moose::Object::DESTROY(@_)' . "\n";
87 $source .= ' if Scalar::Util::blessed($self) ne ';
88 $source .= "'" . $self->associated_metaclass->name . "'";
89 $source .= ';' . "\n";
d03bd989 90
25818121 91 if ( @DEMOLISH_methods ) {
b288593e 92 $source .= 'local $?;' . "\n";
d03bd989 93
b288593e 94 $source .= 'my $in_global_destruction = Devel::GlobalDestruction::in_global_destruction;' . "\n";
95
96 $source .= 'Try::Tiny::try {' . "\n";
97
98 $source .= '$self->' . $_->{class} . '::DEMOLISH($in_global_destruction);' . "\n"
99 for @DEMOLISH_methods;
100
101 $source .= '}';
102 $source .= q[ Try::Tiny::catch { no warnings 'misc'; die $_ };] . "\n";
103 $source .= 'return;' . "\n";
104
8c86556c 105 }
acf10771 106
25818121 107 $source .= '}';
108
d03bd989 109 warn $source if $self->options->{debug};
110
55c361dc 111 my $code = try {
112 $self->_compile_code(source => $source);
113 }
114 catch {
115 $self->throw_error(
116 "Could not eval the destructor :\n\n$source\n\nbecause :\n\n$_",
117 error => $_,
118 data => $source,
119 );
120 };
497442e8 121
e606ae5f 122 $self->{'body'} = $code;
acf10771 123}
124
125
1261;
127
128__END__
129
130=pod
131
d03bd989 132=head1 NAME
587ae0d2 133
134Moose::Meta::Method::Destructor - Method Meta Object for destructors
135
587ae0d2 136=head1 DESCRIPTION
137
bcb81995 138This class is a subclass of L<Class::MOP::Class::Generated> that
139provides Moose-specific functionality for inlining destructors.
140
141To understand this class, you should read the the
142L<Class::MOP::Class::Generated> documentation as well.
143
144=head1 INHERITANCE
145
146C<Moose::Meta::Method::Destructor> is a subclass of
147L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Generated>.
d44714be 148
587ae0d2 149=head1 METHODS
150
151=over 4
152
bcac984a 153=item B<< Moose::Meta::Method::Destructor->new(%options) >>
bcb81995 154
155This constructs a new object. It accepts the following options:
156
157=over 8
587ae0d2 158
bcb81995 159=item * package_name
587ae0d2 160
bcb81995 161The package for the class in which the destructor is being
162inlined. This option is required.
587ae0d2 163
bcb81995 164=item * name
587ae0d2 165
bcb81995 166The name of the destructor method. This option is required.
167
168=item * metaclass
169
170The metaclass for the class this destructor belongs to. This is
171optional, as it can be set later by calling C<<
172$metamethod->attach_to_class >>.
173
174=back
587ae0d2 175
bcb81995 176=item B<< Moose::Meta;:Method::Destructor->is_needed($metaclass) >>
587ae0d2 177
bcb81995 178Given a L<Moose::Meta::Class> object, this method returns a boolean
179indicating whether the class needs a destructor. If the class or any
180of its parents defines a C<DEMOLISH> method, it needs a destructor.
587ae0d2 181
182=back
183
c5fc2c21 184=head1 BUGS
185
186See L<Moose/BUGS> for details on reporting bugs.
187
587ae0d2 188=head1 AUTHORS
189
190Stevan Little E<lt>stevan@iinteractive.comE<gt>
191
192=head1 COPYRIGHT AND LICENSE
193
7e0492d3 194Copyright 2006-2010 by Infinity Interactive, Inc.
587ae0d2 195
196L<http://www.iinteractive.com>
197
198This library is free software; you can redistribute it and/or modify
d03bd989 199it under the same terms as Perl itself.
587ae0d2 200
201=cut
202