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