some refactoring of handles
[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
330dbb07 12our $VERSION = '0.55';
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;
8a157bab 25 if (scalar @_ == 1) {
86629f93 26 if (defined $_[0]) {
27 (ref($_[0]) eq 'HASH')
28 || confess "Single parameters to new() must be a HASH ref";
7bec196c 29 return {%{$_[0]}};
68b6146c 30 }
31 else {
aa7472be 32 return {}; # FIXME this is compat behavior, but is it correct?
86629f93 33 }
68b6146c 34 }
35 else {
7bec196c 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
68b6146c 76# support for UNIVERSAL::DOES ...
8b837f8c 77BEGIN {
78 my $does = UNIVERSAL->can("DOES") ? "SUPER::DOES" : "isa";
79 eval 'sub DOES {
80 my ( $self, $class_or_role_name ) = @_;
81 return $self->'.$does.'($class_or_role_name)
68b6146c 82 || $self->does($class_or_role_name);
8b837f8c 83 }';
d0e9d54e 84}
85
ef333f17 86# new does() methods will be created
87# as approiate see Moose::Meta::Role
0677220d 88sub does {
bdabd620 89 my ($self, $role_name) = @_;
0677220d 90 (defined $role_name)
0db4f1d7 91 || confess "You must supply a role name to does()";
bdabd620 92 my $meta = $self->meta;
93 foreach my $class ($meta->class_precedence_list) {
587e457d 94 my $m = $meta->initialize($class);
bdabd620 95 return 1
587e457d 96 if $m->can('does_role') && $m->does_role($role_name);
bdabd620 97 }
98 return 0;
0677220d 99}
ef333f17 100
f742dfef 101# RANT:
102# Cmon, how many times have you written
103# the following code while debugging:
104#
105# use Data::Dumper;
106# warn Dumper \%thing;
107#
108# It can get seriously annoying, so why
109# not just do this ...
110sub dump {
111 my $self = shift;
112 require Data::Dumper;
1a386a6c 113 local $Data::Dumper::Maxdepth = shift if @_;
f742dfef 114 Data::Dumper::Dumper $self;
115}
116
fcd84ca9 1171;
118
119__END__
120
121=pod
122
123=head1 NAME
124
e522431d 125Moose::Object - The base object for Moose
fcd84ca9 126
fcd84ca9 127=head1 DESCRIPTION
128
6ba6d68c 129This serves as the base object for all Moose classes. Every
130effort will be made to ensure that all classes which C<use Moose>
131will inherit from this class. It provides a default constructor
132and destructor, which run all the BUILD and DEMOLISH methods in
133the class tree.
134
135You don't actually I<need> to inherit from this in order to
136use Moose though. It is just here to make life easier.
137
fcd84ca9 138=head1 METHODS
139
140=over 4
141
142=item B<meta>
143
6ba6d68c 144This will return the metaclass associated with the given class.
145
fcd84ca9 146=item B<new>
147
7bec196c 148This will call C<BUILDARGS>, create a new instance and call C<BUILDALL>.
149
150=item B<BUILDARGS>
151
152This method processes an argument list into a hash reference. It is used by
153C<new>.
e522431d 154
c0e30cf5 155=item B<BUILDALL>
156
d7f17ebb 157This will call every C<BUILD> method in the inheritance hierarchy,
158and pass it a hash-ref of the the C<%params> passed to C<new>.
e522431d 159
c0e30cf5 160=item B<DEMOLISHALL>
161
e522431d 162This will call every C<DEMOLISH> method in the inheritance hierarchy.
163
ef333f17 164=item B<does ($role_name)>
165
02a0fb52 166This will check if the invocant's class C<does> a given C<$role_name>.
167This is similar to C<isa> for object, but it checks the roles instead.
168
d0e9d54e 169=item B<DOES ($class_or_role_name)>
170
171A Moose Role aware implementation of L<UNIVERSAL/DOES>.
172
173C<DOES> is equivalent to C<isa> or C<does>.
174
f742dfef 175=item B<dump ($maxdepth)>
176
177Cmon, how many times have you written the following code while debugging:
178
179 use Data::Dumper;
180 warn Dumper $obj;
181
182It can get seriously annoying, so why not just use this.
183
fcd84ca9 184=back
185
186=head1 BUGS
187
188All complex software has bugs lurking in it, and this module is no
189exception. If you find a bug please either email me, or add the bug
190to cpan-RT.
191
fcd84ca9 192=head1 AUTHOR
193
194Stevan Little E<lt>stevan@iinteractive.comE<gt>
195
196=head1 COPYRIGHT AND LICENSE
197
778db3ac 198Copyright 2006-2008 by Infinity Interactive, Inc.
fcd84ca9 199
200L<http://www.iinteractive.com>
201
202This library is free software; you can redistribute it and/or modify
203it under the same terms as Perl itself.
204
9922fa6f 205=cut