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