"requires" no longer requires non-generated methods
[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
4b2189ce 9our $VERSION = '0.72';
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'} }
49sub associated_metaclass { (shift)->{'associated_metaclass'} }
acf10771 50
51## method
52
a7b097bb 53sub is_needed {
54 my $self = shift;
55 my $metaclass = shift;
56
57 ( blessed $metaclass && $metaclass->isa('Class::MOP::Class') )
58 || $self->throw_error(
59 "The is_needed method expected a metaclass object as its arugment");
60
61 return $metaclass->meta->can('DEMOLISH');
9d22affb 62}
acf10771 63
415e6f85 64sub initialize_body {
acf10771 65 my $self = shift;
66 # TODO:
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)
9d22affb 73
74 my @DEMOLISH_methods = $self->associated_metaclass->find_all_methods_by_name('DEMOLISH');
75
76 return unless @DEMOLISH_methods;
77
acf10771 78 my $source = 'sub {';
79
80 my @DEMOLISH_calls;
9d22affb 81 foreach my $method (@DEMOLISH_methods) {
acf10771 82 push @DEMOLISH_calls => '$_[0]->' . $method->{class} . '::DEMOLISH()';
83 }
84
5431d21a 85 $source .= join ";\n" => @DEMOLISH_calls;
acf10771 86
87 $source .= ";\n" . '}';
88 warn $source if $self->options->{debug};
89
497442e8 90 my $code = $self->_compile_code(
91 environment => {},
92 code => $source,
93 ) or $self->throw_error("Could not eval the destructor :\n\n$source\n\nbecause :\n\n$@", error => $@, data => $source);
94
e606ae5f 95 $self->{'body'} = $code;
acf10771 96}
97
98
991;
100
101__END__
102
103=pod
104
587ae0d2 105=head1 NAME
106
107Moose::Meta::Method::Destructor - Method Meta Object for destructors
108
587ae0d2 109=head1 DESCRIPTION
110
d44714be 111This is a subclass of L<Class::MOP::Method> which handles
6549b0d1 112constructing an appropriate Destructor method. This is primarily
d44714be 113used in the making of immutable metaclasses, otherwise it is
114not particularly useful.
115
587ae0d2 116=head1 METHODS
117
118=over 4
119
120=item B<new>
121
122=item B<attributes>
123
124=item B<meta_instance>
125
126=item B<options>
127
128=item B<is_needed>
129
415e6f85 130=item B<initialize_body>
587ae0d2 131
132=item B<associated_metaclass>
133
134=back
135
136=head1 AUTHORS
137
138Stevan Little E<lt>stevan@iinteractive.comE<gt>
139
140=head1 COPYRIGHT AND LICENSE
141
2840a3b2 142Copyright 2006-2009 by Infinity Interactive, Inc.
587ae0d2 143
144L<http://www.iinteractive.com>
145
146This library is free software; you can redistribute it and/or modify
147it under the same terms as Perl itself.
148
149=cut
150