Commit | Line | Data |
fcd84ca9 |
1 | |
2 | package Moose::Object; |
3 | |
4 | use strict; |
5 | use warnings; |
648e79ae |
6 | |
9922fa6f |
7 | use if ( not our $__mx_is_compiled ), 'Moose::Meta::Class'; |
8 | use if ( not our $__mx_is_compiled ), metaclass => 'Moose::Meta::Class'; |
bc1e29b5 |
9 | |
0677220d |
10 | use Carp 'confess'; |
11 | |
a7e9b05b |
12 | our $VERSION = '0.55_04'; |
75b95414 |
13 | $VERSION = eval $VERSION; |
d44714be |
14 | our $AUTHORITY = 'cpan:STEVAN'; |
fcd84ca9 |
15 | |
16 | sub new { |
2c0cbef7 |
17 | my $class = shift; |
7bec196c |
18 | my $params = $class->BUILDARGS(@_); |
9eb6a943 |
19 | my $self = $class->meta->new_object($params); |
7bec196c |
20 | $self->BUILDALL($params); |
21 | return $self; |
22 | } |
23 | |
24 | sub BUILDARGS { |
25 | my $class = shift; |
8a157bab |
26 | if (scalar @_ == 1) { |
86629f93 |
27 | if (defined $_[0]) { |
28 | (ref($_[0]) eq 'HASH') |
29 | || confess "Single parameters to new() must be a HASH ref"; |
7bec196c |
30 | return {%{$_[0]}}; |
68b6146c |
31 | } |
32 | else { |
aa7472be |
33 | return {}; # FIXME this is compat behavior, but is it correct? |
86629f93 |
34 | } |
68b6146c |
35 | } |
36 | else { |
7bec196c |
37 | return {@_}; |
8a157bab |
38 | } |
fcd84ca9 |
39 | } |
40 | |
c0e30cf5 |
41 | sub BUILDALL { |
d44714be |
42 | # NOTE: we ask Perl if we even |
43 | # need to do this first, to avoid |
44 | # extra meta level calls |
fb1e11d5 |
45 | return unless $_[0]->can('BUILD'); |
46 | my ($self, $params) = @_; |
47 | foreach my $method (reverse $self->meta->find_all_methods_by_name('BUILD')) { |
ac0824ee |
48 | $method->{code}->body->($self, $params); |
fb1e11d5 |
49 | } |
c0e30cf5 |
50 | } |
51 | |
52 | sub DEMOLISHALL { |
3a0c064a |
53 | my $self = shift; |
54 | foreach my $method ($self->meta->find_all_methods_by_name('DEMOLISH')) { |
55 | $method->{code}->body->($self); |
56 | } |
57 | } |
58 | |
59 | sub DESTROY { |
d44714be |
60 | # NOTE: we ask Perl if we even |
61 | # need to do this first, to avoid |
62 | # extra meta level calls |
3a0c064a |
63 | return unless $_[0]->can('DEMOLISH'); |
64 | # if we have an exception here ... |
ca0e380d |
65 | if ($@) { |
66 | # localize the $@ ... |
67 | local $@; |
3a0c064a |
68 | # run DEMOLISHALL ourselves, ... |
ca0e380d |
69 | $_[0]->DEMOLISHALL; |
3a0c064a |
70 | # and return ... |
71 | return; |
72 | } |
ca0e380d |
73 | # otherwise it is normal destruction |
f93f7be9 |
74 | $_[0]->DEMOLISHALL; |
c0e30cf5 |
75 | } |
76 | |
68b6146c |
77 | # support for UNIVERSAL::DOES ... |
8b837f8c |
78 | BEGIN { |
79 | my $does = UNIVERSAL->can("DOES") ? "SUPER::DOES" : "isa"; |
80 | eval 'sub DOES { |
81 | my ( $self, $class_or_role_name ) = @_; |
82 | return $self->'.$does.'($class_or_role_name) |
68b6146c |
83 | || $self->does($class_or_role_name); |
8b837f8c |
84 | }'; |
d0e9d54e |
85 | } |
86 | |
ef333f17 |
87 | # new does() methods will be created |
88 | # as approiate see Moose::Meta::Role |
0677220d |
89 | sub does { |
bdabd620 |
90 | my ($self, $role_name) = @_; |
0677220d |
91 | (defined $role_name) |
0db4f1d7 |
92 | || confess "You must supply a role name to does()"; |
bdabd620 |
93 | my $meta = $self->meta; |
94 | foreach my $class ($meta->class_precedence_list) { |
587e457d |
95 | my $m = $meta->initialize($class); |
bdabd620 |
96 | return 1 |
587e457d |
97 | if $m->can('does_role') && $m->does_role($role_name); |
bdabd620 |
98 | } |
99 | return 0; |
0677220d |
100 | } |
ef333f17 |
101 | |
f742dfef |
102 | # RANT: |
103 | # Cmon, how many times have you written |
104 | # the following code while debugging: |
105 | # |
106 | # use Data::Dumper; |
107 | # warn Dumper \%thing; |
108 | # |
109 | # It can get seriously annoying, so why |
110 | # not just do this ... |
111 | sub dump { |
112 | my $self = shift; |
113 | require Data::Dumper; |
1a386a6c |
114 | local $Data::Dumper::Maxdepth = shift if @_; |
f742dfef |
115 | Data::Dumper::Dumper $self; |
116 | } |
117 | |
fcd84ca9 |
118 | 1; |
119 | |
120 | __END__ |
121 | |
122 | =pod |
123 | |
124 | =head1 NAME |
125 | |
e522431d |
126 | Moose::Object - The base object for Moose |
fcd84ca9 |
127 | |
fcd84ca9 |
128 | =head1 DESCRIPTION |
129 | |
6ba6d68c |
130 | This serves as the base object for all Moose classes. Every |
131 | effort will be made to ensure that all classes which C<use Moose> |
132 | will inherit from this class. It provides a default constructor |
133 | and destructor, which run all the BUILD and DEMOLISH methods in |
134 | the class tree. |
135 | |
136 | You don't actually I<need> to inherit from this in order to |
137 | use Moose though. It is just here to make life easier. |
138 | |
fcd84ca9 |
139 | =head1 METHODS |
140 | |
141 | =over 4 |
142 | |
143 | =item B<meta> |
144 | |
6ba6d68c |
145 | This will return the metaclass associated with the given class. |
146 | |
fcd84ca9 |
147 | =item B<new> |
148 | |
7bec196c |
149 | This will call C<BUILDARGS>, create a new instance and call C<BUILDALL>. |
150 | |
151 | =item B<BUILDARGS> |
152 | |
153 | This method processes an argument list into a hash reference. It is used by |
154 | C<new>. |
e522431d |
155 | |
c0e30cf5 |
156 | =item B<BUILDALL> |
157 | |
d7f17ebb |
158 | This will call every C<BUILD> method in the inheritance hierarchy, |
159 | and pass it a hash-ref of the the C<%params> passed to C<new>. |
e522431d |
160 | |
c0e30cf5 |
161 | =item B<DEMOLISHALL> |
162 | |
e522431d |
163 | This will call every C<DEMOLISH> method in the inheritance hierarchy. |
164 | |
ef333f17 |
165 | =item B<does ($role_name)> |
166 | |
02a0fb52 |
167 | This will check if the invocant's class C<does> a given C<$role_name>. |
168 | This is similar to C<isa> for object, but it checks the roles instead. |
169 | |
d0e9d54e |
170 | =item B<DOES ($class_or_role_name)> |
171 | |
172 | A Moose Role aware implementation of L<UNIVERSAL/DOES>. |
173 | |
174 | C<DOES> is equivalent to C<isa> or C<does>. |
175 | |
f742dfef |
176 | =item B<dump ($maxdepth)> |
177 | |
178 | Cmon, how many times have you written the following code while debugging: |
179 | |
180 | use Data::Dumper; |
181 | warn Dumper $obj; |
182 | |
183 | It can get seriously annoying, so why not just use this. |
184 | |
fcd84ca9 |
185 | =back |
186 | |
187 | =head1 BUGS |
188 | |
189 | All complex software has bugs lurking in it, and this module is no |
190 | exception. If you find a bug please either email me, or add the bug |
191 | to cpan-RT. |
192 | |
fcd84ca9 |
193 | =head1 AUTHOR |
194 | |
195 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
196 | |
197 | =head1 COPYRIGHT AND LICENSE |
198 | |
778db3ac |
199 | Copyright 2006-2008 by Infinity Interactive, Inc. |
fcd84ca9 |
200 | |
201 | L<http://www.iinteractive.com> |
202 | |
203 | This library is free software; you can redistribute it and/or modify |
204 | it under the same terms as Perl itself. |
205 | |
9922fa6f |
206 | =cut |