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'} }
49 sub associated_metaclass { (shift)->{'associated_metaclass'} }
55 my $metaclass = shift;
57 ( blessed $metaclass && $metaclass->isa('Class::MOP::Class') )
58 || $self->throw_error(
59 "The is_needed method expected a metaclass object as its arugment");
61 return $metaclass->meta->can('DEMOLISH');
67 # the %options should also include a both
68 # a call 'initializer' and call 'SUPER::'
69 # options, which should cover approx 90%
70 # of the possible use cases (even if it
71 # requires some adaption on the part of
72 # the author, after all, nothing is free)
74 my @DEMOLISH_methods = $self->associated_metaclass->find_all_methods_by_name('DEMOLISH');
76 return unless @DEMOLISH_methods;
81 foreach my $method (@DEMOLISH_methods) {
82 push @DEMOLISH_calls => '$_[0]->' . $method->{class} . '::DEMOLISH()';
85 $source .= join ";\n" => @DEMOLISH_calls;
87 $source .= ";\n" . '}';
88 warn $source if $self->options->{debug};
90 my $code = $self->_compile_code(
93 ) or $self->throw_error("Could not eval the destructor :\n\n$source\n\nbecause :\n\n$@", error => $@, data => $source);
95 $self->{'body'} = $code;
107 Moose::Meta::Method::Destructor - Method Meta Object for destructors
111 This is a subclass of L<Class::MOP::Method> which handles
112 constructing an appropriate Destructor method. This is primarily
113 used in the making of immutable metaclasses, otherwise it is
114 not particularly useful.
124 =item B<meta_instance>
130 =item B<initialize_body>
132 =item B<associated_metaclass>
138 Stevan Little E<lt>stevan@iinteractive.comE<gt>
140 =head1 COPYRIGHT AND LICENSE
142 Copyright 2006-2009 by Infinity Interactive, Inc.
144 L<http://www.iinteractive.com>
146 This library is free software; you can redistribute it and/or modify
147 it under the same terms as Perl itself.