class_of for does
[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
56d7c745 10our $VERSION = '0.73';
e606ae5f 11$VERSION = eval $VERSION;
d44714be 12our $AUTHORITY = 'cpan:STEVAN';
fcd84ca9 13
14sub new {
2c0cbef7 15 my $class = shift;
e606ae5f 16 my $params = $class->BUILDARGS(@_);
e601e434 17 my $self = Class::MOP::Class->initialize($class)->new_object($params);
e606ae5f 18 $self->BUILDALL($params);
19 return $self;
20}
21
22sub BUILDARGS {
23 my $class = shift;
a62dcd43 24 if ( scalar @_ == 1 ) {
25 unless ( defined $_[0] && ref $_[0] eq 'HASH' ) {
46047bea 26 Class::MOP::class_of($class)->throw_error(
a62dcd43 27 "Single parameters to new() must be a HASH ref",
28 data => $_[0] );
86629f93 29 }
a62dcd43 30 return { %{ $_[0] } };
31 }
8a157bab 32 else {
e606ae5f 33 return {@_};
8a157bab 34 }
fcd84ca9 35}
36
c0e30cf5 37sub BUILDALL {
d44714be 38 # NOTE: we ask Perl if we even
39 # need to do this first, to avoid
40 # extra meta level calls
fb1e11d5 41 return unless $_[0]->can('BUILD');
42 my ($self, $params) = @_;
505033d9 43 foreach my $method (reverse Class::MOP::class_of($self)->find_all_methods_by_name('BUILD')) {
b8f014e7 44 $method->{code}->execute($self, $params);
fb1e11d5 45 }
c0e30cf5 46}
47
48sub DEMOLISHALL {
3a0c064a 49 my $self = shift;
cb5c79c9 50 # NOTE: we ask Perl if we even
51 # need to do this first, to avoid
52 # extra meta level calls
53 return unless $self->can('DEMOLISH');
794caf80 54 foreach my $method (Class::MOP::class_of($self)->find_all_methods_by_name('DEMOLISH')) {
b8f014e7 55 $method->{code}->execute($self);
3a0c064a 56 }
57}
58
59sub DESTROY {
3a0c064a 60 # if we have an exception here ...
ca0e380d 61 if ($@) {
62 # localize the $@ ...
63 local $@;
3a0c064a 64 # run DEMOLISHALL ourselves, ...
ca0e380d 65 $_[0]->DEMOLISHALL;
3a0c064a 66 # and return ...
67 return;
68 }
ca0e380d 69 # otherwise it is normal destruction
f93f7be9 70 $_[0]->DEMOLISHALL;
c0e30cf5 71}
72
e606ae5f 73# support for UNIVERSAL::DOES ...
74BEGIN {
75 my $does = UNIVERSAL->can("DOES") ? "SUPER::DOES" : "isa";
76 eval 'sub DOES {
77 my ( $self, $class_or_role_name ) = @_;
78 return $self->'.$does.'($class_or_role_name)
79 || $self->does($class_or_role_name);
80 }';
81}
82
ef333f17 83# new does() methods will be created
50bc108b 84# as appropiate see Moose::Meta::Role
0677220d 85sub does {
bdabd620 86 my ($self, $role_name) = @_;
66d33a5c 87 my $meta = Class::MOP::class_of($self);
be05faea 88 (defined $role_name)
89 || $meta->throw_error("You much supply a role name to does()");
bdabd620 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 98sub dump {
99 my $self = shift;
100 require Data::Dumper;
1a386a6c 101 local $Data::Dumper::Maxdepth = shift if @_;
f742dfef 102 Data::Dumper::Dumper $self;
103}
104
fcd84ca9 1051;
106
107__END__
108
109=pod
110
111=head1 NAME
112
e522431d 113Moose::Object - The base object for Moose
fcd84ca9 114
fcd84ca9 115=head1 DESCRIPTION
116
080e3614 117This class is the default base class for all Moose-using classes. When
118you C<use Moose> in this class, your class will inherit from this
119class.
6ba6d68c 120
080e3614 121It provides a default constructor and destructor, which run the
122C<BUILDALL> and C<DEMOLISHALL> methods respectively.
123
124You don't actually I<need> to inherit from this in order to use Moose,
125but it makes it easier to take advantage of all of Moose's features.
6ba6d68c 126
fcd84ca9 127=head1 METHODS
128
129=over 4
130
080e3614 131=item B<< Moose::Object->new(%params) >>
132
133This method calls C<< $class->BUILDARGS(@_) >>, and then creates a new
134instance of the appropriate class. Once the instance is created, it
135calls C<< $instance->BUILDALL($params) >>.
fcd84ca9 136
080e3614 137=item B<< Moose::Object->BUILDARGS(%params) >>
6ba6d68c 138
080e3614 139The default implementation of this method accepts a hash or hash
140reference of named parameters. If it receives a single argument that
141I<isn't> a hash reference it throws an error.
fcd84ca9 142
080e3614 143You can override this method in your class to handle other types of
144options passed to the constructor.
e606ae5f 145
080e3614 146This method should always return a hash reference of named options.
e606ae5f 147
080e3614 148=item B<< $object->BUILDALL($params) >>
e522431d 149
080e3614 150This method will call every C<BUILD> method in the inheritance
151hierarchy, starting with the most distant parent class and ending with
152the object's class.
c0e30cf5 153
080e3614 154The C<BUILD> method will be passed the hash reference returned by
155C<BUILDARGS>.
e522431d 156
080e3614 157=item B<< $object->DEMOLISHALL >>
c0e30cf5 158
080e3614 159This will call every C<DEMOLISH> method in the inheritance hierarchy,
160starting with the object's class and ending with the most distant
161parent.
e522431d 162
080e3614 163=item B<< $object->does($role_name) >>
ef333f17 164
080e3614 165This returns true if the object does the given role.
02a0fb52 166
e606ae5f 167=item B<DOES ($class_or_role_name)>
168
080e3614 169This is a a Moose role-aware implementation of L<UNIVERSAL/DOES>.
e606ae5f 170
080e3614 171This is effectively the same as writing:
e606ae5f 172
080e3614 173 $object->does($name) || $object->isa($name)
f742dfef 174
080e3614 175This method will work with Perl 5.8, which did not implement
176C<UNIVERSAL::DOES>.
f742dfef 177
080e3614 178=item B<< $object->dump($maxdepth) >>
f742dfef 179
080e3614 180This is a handy utility for C<Data::Dumper>ing an object. By default,
181the maximum depth is 1, to avoid making a mess.
f742dfef 182
fcd84ca9 183=back
184
185=head1 BUGS
186
187All complex software has bugs lurking in it, and this module is no
188exception. If you find a bug please either email me, or add the bug
189to cpan-RT.
190
fcd84ca9 191=head1 AUTHOR
192
193Stevan Little E<lt>stevan@iinteractive.comE<gt>
194
195=head1 COPYRIGHT AND LICENSE
196
2840a3b2 197Copyright 2006-2009 by Infinity Interactive, Inc.
fcd84ca9 198
199L<http://www.iinteractive.com>
200
201This library is free software; you can redistribute it and/or modify
202it under the same terms as Perl itself.
203
9922fa6f 204=cut