bump version to 0.73_01
[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
a3319906 9our $VERSION = '0.73_01';
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 = @_;
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
e606ae5f 28 'body' => undef,
29 'package_name' => $options{package_name},
30 'name' => $options{name},
acf10771 31 # ...
e606ae5f 32 'options' => $options{options},
33 'associated_metaclass' => $options{metaclass},
acf10771 34 } => $class;
35
36 # we don't want this creating
37 # a cycle in the code, if not
38 # needed
e606ae5f 39 weaken($self->{'associated_metaclass'});
acf10771 40
415e6f85 41 $self->initialize_body;
acf10771 42
43 return $self;
44}
45
46## accessors
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
cbf9d1a1 60 return Class::MOP::class_of($metaclass)->can('DEMOLISH');
9d22affb 61}
acf10771 62
415e6f85 63sub initialize_body {
acf10771 64 my $self = shift;
65 # TODO:
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)
9d22affb 72
73 my @DEMOLISH_methods = $self->associated_metaclass->find_all_methods_by_name('DEMOLISH');
74
75 return unless @DEMOLISH_methods;
76
acf10771 77 my $source = 'sub {';
78
79 my @DEMOLISH_calls;
9d22affb 80 foreach my $method (@DEMOLISH_methods) {
acf10771 81 push @DEMOLISH_calls => '$_[0]->' . $method->{class} . '::DEMOLISH()';
82 }
83
5431d21a 84 $source .= join ";\n" => @DEMOLISH_calls;
acf10771 85
86 $source .= ";\n" . '}';
87 warn $source if $self->options->{debug};
88
497442e8 89 my $code = $self->_compile_code(
90 environment => {},
91 code => $source,
92 ) or $self->throw_error("Could not eval the destructor :\n\n$source\n\nbecause :\n\n$@", error => $@, data => $source);
93
e606ae5f 94 $self->{'body'} = $code;
acf10771 95}
96
97
981;
99
100__END__
101
102=pod
103
587ae0d2 104=head1 NAME
105
106Moose::Meta::Method::Destructor - Method Meta Object for destructors
107
587ae0d2 108=head1 DESCRIPTION
109
bcb81995 110This class is a subclass of L<Class::MOP::Class::Generated> that
111provides Moose-specific functionality for inlining destructors.
112
113To understand this class, you should read the the
114L<Class::MOP::Class::Generated> documentation as well.
115
116=head1 INHERITANCE
117
118C<Moose::Meta::Method::Destructor> is a subclass of
119L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Generated>.
d44714be 120
587ae0d2 121=head1 METHODS
122
123=over 4
124
bcb81995 125=item B<< Moose::Meta;:Method::Destructor->new(%options) >>
126
127This constructs a new object. It accepts the following options:
128
129=over 8
587ae0d2 130
bcb81995 131=item * package_name
587ae0d2 132
bcb81995 133The package for the class in which the destructor is being
134inlined. This option is required.
587ae0d2 135
bcb81995 136=item * name
587ae0d2 137
bcb81995 138The name of the destructor method. This option is required.
139
140=item * metaclass
141
142The metaclass for the class this destructor belongs to. This is
143optional, as it can be set later by calling C<<
144$metamethod->attach_to_class >>.
145
146=back
587ae0d2 147
bcb81995 148=item B<< Moose::Meta;:Method::Destructor->is_needed($metaclass) >>
587ae0d2 149
bcb81995 150Given a L<Moose::Meta::Class> object, this method returns a boolean
151indicating whether the class needs a destructor. If the class or any
152of its parents defines a C<DEMOLISH> method, it needs a destructor.
587ae0d2 153
154=back
155
156=head1 AUTHORS
157
158Stevan Little E<lt>stevan@iinteractive.comE<gt>
159
160=head1 COPYRIGHT AND LICENSE
161
2840a3b2 162Copyright 2006-2009 by Infinity Interactive, Inc.
587ae0d2 163
164L<http://www.iinteractive.com>
165
166This library is free software; you can redistribute it and/or modify
167it under the same terms as Perl itself.
168
169=cut
170