2ce956d950efbba5d1b770737def5555416cb68a
[gitmo/Moose.git] / lib / Moose / Meta / Method / Destructor.pm
1
2 package Moose::Meta::Method::Destructor;
3
4 use strict;
5 use warnings;
6
7 use Devel::GlobalDestruction ();
8 use Scalar::Util 'blessed', 'weaken';
9 use Try::Tiny;
10
11 use base 'Moose::Meta::Method',
12          'Class::MOP::Method::Inlined';
13
14 sub new {
15     my $class   = shift;
16     my %options = @_;
17
18     (ref $options{options} eq 'HASH')
19         || $class->throw_error("You must pass a hash of options", data => $options{options});
20
21     ($options{package_name} && $options{name})
22         || $class->throw_error("You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT");
23
24     my $self = bless {
25         # from our superclass
26         'body'                 => undef,
27         'package_name'         => $options{package_name},
28         'name'                 => $options{name},
29         # ...
30         'options'              => $options{options},
31         'associated_metaclass' => $options{metaclass},
32     } => $class;
33
34     # we don't want this creating
35     # a cycle in the code, if not
36     # needed
37     weaken($self->{'associated_metaclass'});
38
39     $self->_initialize_body;
40
41     return $self;
42 }
43
44 ## accessors
45
46 sub options              { (shift)->{'options'}              }
47
48 ## method
49
50 sub is_needed {
51     my $self      = shift;
52     my $metaclass = shift;
53
54     ( blessed $metaclass && $metaclass->isa('Class::MOP::Class') )
55         || $self->throw_error(
56         "The is_needed method expected a metaclass object as its arugment");
57
58     return $metaclass->find_method_by_name("DEMOLISHALL");
59 }
60
61 sub initialize_body {
62     Carp::cluck('The initialize_body method has been made private.'
63         . " The public version is deprecated and will be removed in a future release.\n");
64     shift->_initialize_body;
65 }
66
67 sub _initialize_body {
68     my $self = shift;
69     # TODO:
70     # the %options should also include a both
71     # a call 'initializer' and call 'SUPER::'
72     # options, which should cover approx 90%
73     # of the possible use cases (even if it
74     # requires some adaption on the part of
75     # the author, after all, nothing is free)
76
77     my $class = $self->associated_metaclass->name;
78     my @source = (
79         'sub {',
80             'my $self = shift;',
81             'return ' . $self->_generate_fallback_destructor('$self'),
82                 'if Scalar::Util::blessed($self) ne \'' . $class . '\';',
83             'local $?;',
84             $self->_generate_DEMOLISHALL('$self'),
85             'return;',
86         '}',
87     );
88     warn join("\n", @source) if $self->options->{debug};
89
90     my $code = try {
91         $self->_compile_code(source => \@source);
92     }
93     catch {
94         my $source = join("\n", @source);
95         $self->throw_error(
96             "Could not eval the destructor :\n\n$source\n\nbecause :\n\n$_",
97             error => $_,
98             data  => $source,
99         );
100     };
101
102     $self->{'body'} = $code;
103 }
104
105 sub _generate_fallback_destructor {
106     my $self = shift;
107     my ($inv) = @_;
108
109     return $inv . '->Moose::Object::DESTROY(@_)';
110 }
111
112 sub _generate_DEMOLISHALL {
113     my $self = shift;
114     my ($inv) = @_;
115
116     my @methods = $self->associated_metaclass->find_all_methods_by_name('DEMOLISH');
117     return unless @methods;
118
119     return (
120         'my $igd = Devel::GlobalDestruction::in_global_destruction;',
121         'Try::Tiny::try {',
122             (map { $inv . '->' . $_->{class} . '::DEMOLISH($igd);' } @methods),
123         '}',
124         'Try::Tiny::catch {',
125             'die $_;',
126         '};',
127     );
128 }
129
130
131 1;
132
133 # ABSTRACT: Method Meta Object for destructors
134
135 __END__
136
137 =pod
138
139 =head1 DESCRIPTION
140
141 This class is a subclass of L<Class::MOP::Class::Generated> that
142 provides Moose-specific functionality for inlining destructors.
143
144 To understand this class, you should read the the
145 L<Class::MOP::Class::Generated> documentation as well.
146
147 =head1 INHERITANCE
148
149 C<Moose::Meta::Method::Destructor> is a subclass of
150 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Generated>.
151
152 =head1 METHODS
153
154 =over 4
155
156 =item B<< Moose::Meta::Method::Destructor->new(%options) >>
157
158 This constructs a new object. It accepts the following options:
159
160 =over 8
161
162 =item * package_name
163
164 The package for the class in which the destructor is being
165 inlined. This option is required.
166
167 =item * name
168
169 The name of the destructor method. This option is required.
170
171 =item * metaclass
172
173 The metaclass for the class this destructor belongs to. This is
174 optional, as it can be set later by calling C<<
175 $metamethod->attach_to_class >>.
176
177 =back
178
179 =item B<< Moose::Meta;:Method::Destructor->is_needed($metaclass) >>
180
181 Given a L<Moose::Meta::Class> object, this method returns a boolean
182 indicating whether the class needs a destructor. If the class or any
183 of its parents defines a C<DEMOLISH> method, it needs a destructor.
184
185 =back
186
187 =head1 BUGS
188
189 See L<Moose/BUGS> for details on reporting bugs.
190
191 =cut
192