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