add test for modifier order to prevent regressions
[gitmo/Moose.git] / lib / Moose / Meta / Method / Destructor.pm
CommitLineData
acf10771 1
2package Moose::Meta::Method::Destructor;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8use Scalar::Util 'blessed', 'weaken';
9
1b2aea39 10our $VERSION = '0.03';
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
20 (exists $options{options} && ref $options{options} eq 'HASH')
21 || confess "You must pass a hash of options";
1b2aea39 22
23 ($options{package_name} && $options{name})
24 || confess "You must supply the package_name and name parameters";
acf10771 25
26 my $self = bless {
27 # from our superclass
1b2aea39 28 '&!body' => undef,
29 '$!package_name' => $options{package_name},
30 '$!name' => $options{name},
acf10771 31 # ...
32 '%!options' => $options{options},
33 '$!associated_metaclass' => $options{metaclass},
34 } => $class;
35
36 # we don't want this creating
37 # a cycle in the code, if not
38 # needed
39 weaken($self->{'$!associated_metaclass'});
40
415e6f85 41 $self->initialize_body;
acf10771 42
43 return $self;
44}
45
46## accessors
47
48sub options { (shift)->{'%!options'} }
49sub associated_metaclass { (shift)->{'$!associated_metaclass'} }
50
51## method
52
53sub is_needed { defined $_[0]->{'&!body'} ? 1 : 0 }
54
415e6f85 55sub initialize_body {
acf10771 56 my $self = shift;
57 # TODO:
58 # the %options should also include a both
59 # a call 'initializer' and call 'SUPER::'
60 # options, which should cover approx 90%
61 # of the possible use cases (even if it
62 # requires some adaption on the part of
63 # the author, after all, nothing is free)
64 my $source = 'sub {';
65
66 my @DEMOLISH_calls;
67 foreach my $method ($self->associated_metaclass->find_all_methods_by_name('DEMOLISH')) {
68 push @DEMOLISH_calls => '$_[0]->' . $method->{class} . '::DEMOLISH()';
69 }
70
5431d21a 71 $source .= join ";\n" => @DEMOLISH_calls;
acf10771 72
73 $source .= ";\n" . '}';
74 warn $source if $self->options->{debug};
75
76 my $code;
77 {
78 $code = eval $source;
79 confess "Could not eval the destructor :\n\n$source\n\nbecause :\n\n$@" if $@;
80 }
81 $self->{'&!body'} = $code;
82}
83
84
851;
86
87__END__
88
89=pod
90
587ae0d2 91=head1 NAME
92
93Moose::Meta::Method::Destructor - Method Meta Object for destructors
94
587ae0d2 95=head1 DESCRIPTION
96
d44714be 97This is a subclass of L<Class::MOP::Method> which handles
98constructing an approprate Destructor method. This is primarily
99used in the making of immutable metaclasses, otherwise it is
100not particularly useful.
101
587ae0d2 102=head1 METHODS
103
104=over 4
105
106=item B<new>
107
108=item B<attributes>
109
110=item B<meta_instance>
111
112=item B<options>
113
114=item B<is_needed>
115
415e6f85 116=item B<initialize_body>
587ae0d2 117
118=item B<associated_metaclass>
119
120=back
121
122=head1 AUTHORS
123
124Stevan Little E<lt>stevan@iinteractive.comE<gt>
125
126=head1 COPYRIGHT AND LICENSE
127
778db3ac 128Copyright 2006-2008 by Infinity Interactive, Inc.
587ae0d2 129
130L<http://www.iinteractive.com>
131
132This library is free software; you can redistribute it and/or modify
133it under the same terms as Perl itself.
134
135=cut
136