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