error message for failed delegation should use the attribute name, not the delegate...
[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
d344c3fe 9our $VERSION = '0.68';
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
9d22affb 53sub is_needed {
54 my $self = shift;
55 # if called as a class method
56 # then must pass in a class name
57 unless (blessed $self) {
58 (blessed $_[0] && $_[0]->isa('Class::MOP::Class'))
46cb090f 59 || $self->throw_error("When calling is_needed as a class method you must pass a class name");
9d22affb 60 return $_[0]->meta->can('DEMOLISH');
61 }
e606ae5f 62 defined $self->{'body'} ? 1 : 0
9d22affb 63}
acf10771 64
415e6f85 65sub initialize_body {
acf10771 66 my $self = shift;
67 # TODO:
68 # the %options should also include a both
69 # a call 'initializer' and call 'SUPER::'
70 # options, which should cover approx 90%
71 # of the possible use cases (even if it
72 # requires some adaption on the part of
73 # the author, after all, nothing is free)
9d22affb 74
75 my @DEMOLISH_methods = $self->associated_metaclass->find_all_methods_by_name('DEMOLISH');
76
77 return unless @DEMOLISH_methods;
78
acf10771 79 my $source = 'sub {';
80
81 my @DEMOLISH_calls;
9d22affb 82 foreach my $method (@DEMOLISH_methods) {
acf10771 83 push @DEMOLISH_calls => '$_[0]->' . $method->{class} . '::DEMOLISH()';
84 }
85
5431d21a 86 $source .= join ";\n" => @DEMOLISH_calls;
acf10771 87
88 $source .= ";\n" . '}';
89 warn $source if $self->options->{debug};
90
497442e8 91 my $code = $self->_compile_code(
92 environment => {},
93 code => $source,
94 ) or $self->throw_error("Could not eval the destructor :\n\n$source\n\nbecause :\n\n$@", error => $@, data => $source);
95
e606ae5f 96 $self->{'body'} = $code;
acf10771 97}
98
99
1001;
101
102__END__
103
104=pod
105
587ae0d2 106=head1 NAME
107
108Moose::Meta::Method::Destructor - Method Meta Object for destructors
109
587ae0d2 110=head1 DESCRIPTION
111
d44714be 112This is a subclass of L<Class::MOP::Method> which handles
6549b0d1 113constructing an appropriate Destructor method. This is primarily
d44714be 114used in the making of immutable metaclasses, otherwise it is
115not particularly useful.
116
587ae0d2 117=head1 METHODS
118
119=over 4
120
121=item B<new>
122
123=item B<attributes>
124
125=item B<meta_instance>
126
127=item B<options>
128
129=item B<is_needed>
130
415e6f85 131=item B<initialize_body>
587ae0d2 132
133=item B<associated_metaclass>
134
135=back
136
137=head1 AUTHORS
138
139Stevan Little E<lt>stevan@iinteractive.comE<gt>
140
141=head1 COPYRIGHT AND LICENSE
142
2840a3b2 143Copyright 2006-2009 by Infinity Interactive, Inc.
587ae0d2 144
145L<http://www.iinteractive.com>
146
147This library is free software; you can redistribute it and/or modify
148it under the same terms as Perl itself.
149
150=cut
151