2 package Moose::Meta::Method::Destructor;
7 use Scalar::Util 'blessed', 'weaken';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Moose::Meta::Method',
14 'Class::MOP::Method::Generated';
20 (ref $options{options} eq 'HASH')
21 || $class->throw_error("You must pass a hash of options", data => $options{options});
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");
29 'package_name' => $options{package_name},
30 'name' => $options{name},
32 'options' => $options{options},
33 'associated_metaclass' => $options{metaclass},
36 # we don't want this creating
37 # a cycle in the code, if not
39 weaken($self->{'associated_metaclass'});
41 $self->initialize_body;
48 sub options { (shift)->{'options'} }
54 my $metaclass = shift;
56 ( blessed $metaclass && $metaclass->isa('Class::MOP::Class') )
57 || $self->throw_error(
58 "The is_needed method expected a metaclass object as its arugment");
60 return $metaclass->meta->can('DEMOLISH');
66 # the %options should also include a both
67 # a call 'initializer' and call 'SUPER::'
68 # options, which should cover approx 90%
69 # of the possible use cases (even if it
70 # requires some adaption on the part of
71 # the author, after all, nothing is free)
73 my @DEMOLISH_methods = $self->associated_metaclass->find_all_methods_by_name('DEMOLISH');
75 return unless @DEMOLISH_methods;
80 foreach my $method (@DEMOLISH_methods) {
81 push @DEMOLISH_calls => '$_[0]->' . $method->{class} . '::DEMOLISH()';
84 $source .= join ";\n" => @DEMOLISH_calls;
86 $source .= ";\n" . '}';
87 warn $source if $self->options->{debug};
89 my $code = $self->_compile_code(
92 ) or $self->throw_error("Could not eval the destructor :\n\n$source\n\nbecause :\n\n$@", error => $@, data => $source);
94 $self->{'body'} = $code;
106 Moose::Meta::Method::Destructor - Method Meta Object for destructors
110 This class is a subclass of L<Class::MOP::Class::Generated> that
111 provides Moose-specific functionality for inlining destructors.
113 To understand this class, you should read the the
114 L<Class::MOP::Class::Generated> documentation as well.
118 C<Moose::Meta::Method::Destructor> is a subclass of
119 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Generated>.
125 =item B<< Moose::Meta;:Method::Destructor->new(%options) >>
127 This constructs a new object. It accepts the following options:
133 The package for the class in which the destructor is being
134 inlined. This option is required.
138 The name of the destructor method. This option is required.
142 The metaclass for the class this destructor belongs to. This is
143 optional, as it can be set later by calling C<<
144 $metamethod->attach_to_class >>.
148 =item B<< Moose::Meta;:Method::Destructor->is_needed($metaclass) >>
150 Given a L<Moose::Meta::Class> object, this method returns a boolean
151 indicating whether the class needs a destructor. If the class or any
152 of its parents defines a C<DEMOLISH> method, it needs a destructor.
158 Stevan Little E<lt>stevan@iinteractive.comE<gt>
160 =head1 COPYRIGHT AND LICENSE
162 Copyright 2006-2009 by Infinity Interactive, Inc.
164 L<http://www.iinteractive.com>
166 This library is free software; you can redistribute it and/or modify
167 it under the same terms as Perl itself.