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