Commit | Line | Data |
fcd84ca9 |
1 | |
2 | package Moose::Object; |
3 | |
4 | use strict; |
5 | use warnings; |
648e79ae |
6 | |
2eaf25da |
7 | use Scalar::Util; |
8 | |
9922fa6f |
9 | use if ( not our $__mx_is_compiled ), 'Moose::Meta::Class'; |
10 | use if ( not our $__mx_is_compiled ), metaclass => 'Moose::Meta::Class'; |
bc1e29b5 |
11 | |
6302a7e8 |
12 | our $VERSION = '0.73_02'; |
e606ae5f |
13 | $VERSION = eval $VERSION; |
d44714be |
14 | our $AUTHORITY = 'cpan:STEVAN'; |
fcd84ca9 |
15 | |
16 | sub new { |
2c0cbef7 |
17 | my $class = shift; |
2eaf25da |
18 | |
e606ae5f |
19 | my $params = $class->BUILDARGS(@_); |
2eaf25da |
20 | |
21 | # We want to support passing $self->new, but initialize |
22 | # takes only an unblessed class name |
23 | my $real_class = Scalar::Util::blessed($class) || $class; |
24 | my $self = Class::MOP::Class->initialize($real_class)->new_object($params); |
25 | |
e606ae5f |
26 | $self->BUILDALL($params); |
2eaf25da |
27 | |
e606ae5f |
28 | return $self; |
29 | } |
30 | |
31 | sub BUILDARGS { |
32 | my $class = shift; |
a62dcd43 |
33 | if ( scalar @_ == 1 ) { |
34 | unless ( defined $_[0] && ref $_[0] eq 'HASH' ) { |
46047bea |
35 | Class::MOP::class_of($class)->throw_error( |
a62dcd43 |
36 | "Single parameters to new() must be a HASH ref", |
37 | data => $_[0] ); |
86629f93 |
38 | } |
a62dcd43 |
39 | return { %{ $_[0] } }; |
40 | } |
8a157bab |
41 | else { |
e606ae5f |
42 | return {@_}; |
8a157bab |
43 | } |
fcd84ca9 |
44 | } |
45 | |
c0e30cf5 |
46 | sub BUILDALL { |
d44714be |
47 | # NOTE: we ask Perl if we even |
48 | # need to do this first, to avoid |
49 | # extra meta level calls |
fb1e11d5 |
50 | return unless $_[0]->can('BUILD'); |
51 | my ($self, $params) = @_; |
505033d9 |
52 | foreach my $method (reverse Class::MOP::class_of($self)->find_all_methods_by_name('BUILD')) { |
b8f014e7 |
53 | $method->{code}->execute($self, $params); |
fb1e11d5 |
54 | } |
c0e30cf5 |
55 | } |
56 | |
57 | sub DEMOLISHALL { |
8955a780 |
58 | my $self = shift; |
59 | |
60 | # NOTE: we ask Perl if we even |
cb5c79c9 |
61 | # need to do this first, to avoid |
8955a780 |
62 | # extra meta level calls |
cb5c79c9 |
63 | return unless $self->can('DEMOLISH'); |
8955a780 |
64 | |
65 | # This is a hack, because Moose::Meta::Class may not be the right |
66 | # metaclass, but class_of may return undef during global |
67 | # destruction, if the metaclass object has already been cleaned |
68 | # up. |
69 | my $meta = Class::MOP::class_of($self) |
70 | || Moose::Meta::Class->initialize( ref $self ); |
71 | |
72 | foreach my $method ( $meta->find_all_methods_by_name('DEMOLISH') ) { |
b8f014e7 |
73 | $method->{code}->execute($self); |
3a0c064a |
74 | } |
75 | } |
76 | |
77 | sub DESTROY { |
3a0c064a |
78 | # if we have an exception here ... |
ca0e380d |
79 | if ($@) { |
80 | # localize the $@ ... |
81 | local $@; |
3a0c064a |
82 | # run DEMOLISHALL ourselves, ... |
ca0e380d |
83 | $_[0]->DEMOLISHALL; |
3a0c064a |
84 | # and return ... |
85 | return; |
86 | } |
ca0e380d |
87 | # otherwise it is normal destruction |
f93f7be9 |
88 | $_[0]->DEMOLISHALL; |
c0e30cf5 |
89 | } |
90 | |
e606ae5f |
91 | # support for UNIVERSAL::DOES ... |
92 | BEGIN { |
93 | my $does = UNIVERSAL->can("DOES") ? "SUPER::DOES" : "isa"; |
94 | eval 'sub DOES { |
95 | my ( $self, $class_or_role_name ) = @_; |
96 | return $self->'.$does.'($class_or_role_name) |
97 | || $self->does($class_or_role_name); |
98 | }'; |
99 | } |
100 | |
ef333f17 |
101 | # new does() methods will be created |
50bc108b |
102 | # as appropiate see Moose::Meta::Role |
0677220d |
103 | sub does { |
bdabd620 |
104 | my ($self, $role_name) = @_; |
66d33a5c |
105 | my $meta = Class::MOP::class_of($self); |
be05faea |
106 | (defined $role_name) |
107 | || $meta->throw_error("You much supply a role name to does()"); |
bdabd620 |
108 | foreach my $class ($meta->class_precedence_list) { |
587e457d |
109 | my $m = $meta->initialize($class); |
bdabd620 |
110 | return 1 |
587e457d |
111 | if $m->can('does_role') && $m->does_role($role_name); |
bdabd620 |
112 | } |
113 | return 0; |
0677220d |
114 | } |
ef333f17 |
115 | |
f742dfef |
116 | sub dump { |
117 | my $self = shift; |
118 | require Data::Dumper; |
1a386a6c |
119 | local $Data::Dumper::Maxdepth = shift if @_; |
f742dfef |
120 | Data::Dumper::Dumper $self; |
121 | } |
122 | |
fcd84ca9 |
123 | 1; |
124 | |
125 | __END__ |
126 | |
127 | =pod |
128 | |
129 | =head1 NAME |
130 | |
e522431d |
131 | Moose::Object - The base object for Moose |
fcd84ca9 |
132 | |
fcd84ca9 |
133 | =head1 DESCRIPTION |
134 | |
080e3614 |
135 | This class is the default base class for all Moose-using classes. When |
136 | you C<use Moose> in this class, your class will inherit from this |
137 | class. |
6ba6d68c |
138 | |
080e3614 |
139 | It provides a default constructor and destructor, which run the |
140 | C<BUILDALL> and C<DEMOLISHALL> methods respectively. |
141 | |
142 | You don't actually I<need> to inherit from this in order to use Moose, |
143 | but it makes it easier to take advantage of all of Moose's features. |
6ba6d68c |
144 | |
fcd84ca9 |
145 | =head1 METHODS |
146 | |
147 | =over 4 |
148 | |
080e3614 |
149 | =item B<< Moose::Object->new(%params) >> |
150 | |
151 | This method calls C<< $class->BUILDARGS(@_) >>, and then creates a new |
152 | instance of the appropriate class. Once the instance is created, it |
153 | calls C<< $instance->BUILDALL($params) >>. |
fcd84ca9 |
154 | |
080e3614 |
155 | =item B<< Moose::Object->BUILDARGS(%params) >> |
6ba6d68c |
156 | |
080e3614 |
157 | The default implementation of this method accepts a hash or hash |
158 | reference of named parameters. If it receives a single argument that |
159 | I<isn't> a hash reference it throws an error. |
fcd84ca9 |
160 | |
080e3614 |
161 | You can override this method in your class to handle other types of |
162 | options passed to the constructor. |
e606ae5f |
163 | |
080e3614 |
164 | This method should always return a hash reference of named options. |
e606ae5f |
165 | |
080e3614 |
166 | =item B<< $object->BUILDALL($params) >> |
e522431d |
167 | |
080e3614 |
168 | This method will call every C<BUILD> method in the inheritance |
169 | hierarchy, starting with the most distant parent class and ending with |
170 | the object's class. |
c0e30cf5 |
171 | |
080e3614 |
172 | The C<BUILD> method will be passed the hash reference returned by |
173 | C<BUILDARGS>. |
e522431d |
174 | |
080e3614 |
175 | =item B<< $object->DEMOLISHALL >> |
c0e30cf5 |
176 | |
080e3614 |
177 | This will call every C<DEMOLISH> method in the inheritance hierarchy, |
178 | starting with the object's class and ending with the most distant |
179 | parent. |
e522431d |
180 | |
080e3614 |
181 | =item B<< $object->does($role_name) >> |
ef333f17 |
182 | |
080e3614 |
183 | This returns true if the object does the given role. |
02a0fb52 |
184 | |
e606ae5f |
185 | =item B<DOES ($class_or_role_name)> |
186 | |
080e3614 |
187 | This is a a Moose role-aware implementation of L<UNIVERSAL/DOES>. |
e606ae5f |
188 | |
080e3614 |
189 | This is effectively the same as writing: |
e606ae5f |
190 | |
080e3614 |
191 | $object->does($name) || $object->isa($name) |
f742dfef |
192 | |
080e3614 |
193 | This method will work with Perl 5.8, which did not implement |
194 | C<UNIVERSAL::DOES>. |
f742dfef |
195 | |
080e3614 |
196 | =item B<< $object->dump($maxdepth) >> |
f742dfef |
197 | |
080e3614 |
198 | This is a handy utility for C<Data::Dumper>ing an object. By default, |
199 | the maximum depth is 1, to avoid making a mess. |
f742dfef |
200 | |
fcd84ca9 |
201 | =back |
202 | |
203 | =head1 BUGS |
204 | |
205 | All complex software has bugs lurking in it, and this module is no |
206 | exception. If you find a bug please either email me, or add the bug |
207 | to cpan-RT. |
208 | |
fcd84ca9 |
209 | =head1 AUTHOR |
210 | |
211 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
212 | |
213 | =head1 COPYRIGHT AND LICENSE |
214 | |
2840a3b2 |
215 | Copyright 2006-2009 by Infinity Interactive, Inc. |
fcd84ca9 |
216 | |
217 | L<http://www.iinteractive.com> |
218 | |
219 | This library is free software; you can redistribute it and/or modify |
220 | it under the same terms as Perl itself. |
221 | |
9922fa6f |
222 | =cut |