Beginning of dzilization
[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 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Moose::Meta::Method',
14          'Class::MOP::Method::Inlined';
15
16 sub new {
17     my $class   = shift;
18     my %options = @_;
19
20     (ref $options{options} eq 'HASH')
21         || $class->throw_error("You must pass a hash of options", data => $options{options});
22
23     ($options{package_name} && $options{name})
24         || $class->throw_error("You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT");
25
26     my $self = bless {
27         # from our superclass
28         'body'                 => undef,
29         'package_name'         => $options{package_name},
30         'name'                 => $options{name},
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
41     $self->_initialize_body;
42
43     return $self;
44 }
45
46 ## accessors
47
48 sub options              { (shift)->{'options'}              }
49
50 ## method
51
52 sub is_needed {
53     my $self      = shift;
54     my $metaclass = shift;
55
56     ( blessed $metaclass && $metaclass->isa('Class::MOP::Class') )
57         || $self->throw_error(
58         "The is_needed method expected a metaclass object as its arugment");
59
60     return $metaclass->find_method_by_name("DEMOLISHALL");
61 }
62
63 sub initialize_body {
64     Carp::cluck('The initialize_body method has been made private.'
65         . " The public version is deprecated and will be removed in a future release.\n");
66     shift->_initialize_body;
67 }
68
69 sub _initialize_body {
70     my $self = shift;
71     # TODO:
72     # the %options should also include a both
73     # a call 'initializer' and call 'SUPER::'
74     # options, which should cover approx 90%
75     # of the possible use cases (even if it
76     # requires some adaption on the part of
77     # the author, after all, nothing is free)
78
79     my $class = $self->associated_metaclass->name;
80     my @source = (
81         'sub {',
82             'my $self = shift;',
83             'return ' . $self->_generate_fallback_destructor('$self'),
84                 'if Scalar::Util::blessed($self) ne \'' . $class . '\';',
85             $self->_generate_DEMOLISHALL('$self'),
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         'local $?;',
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             'no warnings \'misc\';',
127             'die $_;',
128         '};',
129         'return;',
130     );
131 }
132
133
134 1;
135
136 # ABSTRACT: Method Meta Object for destructors
137
138 __END__
139
140 =pod
141
142 =head1 DESCRIPTION
143
144 This class is a subclass of L<Class::MOP::Class::Generated> that
145 provides Moose-specific functionality for inlining destructors.
146
147 To understand this class, you should read the the
148 L<Class::MOP::Class::Generated> documentation as well.
149
150 =head1 INHERITANCE
151
152 C<Moose::Meta::Method::Destructor> is a subclass of
153 L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Generated>.
154
155 =head1 METHODS
156
157 =over 4
158
159 =item B<< Moose::Meta::Method::Destructor->new(%options) >>
160
161 This constructs a new object. It accepts the following options:
162
163 =over 8
164
165 =item * package_name
166
167 The package for the class in which the destructor is being
168 inlined. This option is required.
169
170 =item * name
171
172 The name of the destructor method. This option is required.
173
174 =item * metaclass
175
176 The metaclass for the class this destructor belongs to. This is
177 optional, as it can be set later by calling C<<
178 $metamethod->attach_to_class >>.
179
180 =back
181
182 =item B<< Moose::Meta;:Method::Destructor->is_needed($metaclass) >>
183
184 Given a L<Moose::Meta::Class> object, this method returns a boolean
185 indicating whether the class needs a destructor. If the class or any
186 of its parents defines a C<DEMOLISH> method, it needs a destructor.
187
188 =back
189
190 =head1 BUGS
191
192 See L<Moose/BUGS> for details on reporting bugs.
193
194 =cut
195