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