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