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