Checking in changes prior to tagging of version 0.93. Changelog diff is:
[gitmo/Moose.git] / lib / Moose / Object.pm
CommitLineData
fcd84ca9 1
2package Moose::Object;
3
4use strict;
5use warnings;
648e79ae 6
f25f9912 7use Devel::GlobalDestruction ();
8use MRO::Compat ();
9use Scalar::Util ();
10use Try::Tiny ();
2eaf25da 11
9922fa6f 12use if ( not our $__mx_is_compiled ), 'Moose::Meta::Class';
13use if ( not our $__mx_is_compiled ), metaclass => 'Moose::Meta::Class';
bc1e29b5 14
6d0815b5 15our $VERSION = '0.93';
e606ae5f 16$VERSION = eval $VERSION;
d44714be 17our $AUTHORITY = 'cpan:STEVAN';
fcd84ca9 18
19sub new {
2c0cbef7 20 my $class = shift;
62c8675e 21
e606ae5f 22 my $params = $class->BUILDARGS(@_);
2eaf25da 23
f25f9912 24 my $real_class = Scalar::Util::blessed($class) || $class;
2eaf25da 25 my $self = Class::MOP::Class->initialize($real_class)->new_object($params);
26
e606ae5f 27 $self->BUILDALL($params);
2eaf25da 28
e606ae5f 29 return $self;
30}
31
32sub BUILDARGS {
33 my $class = shift;
a62dcd43 34 if ( scalar @_ == 1 ) {
35 unless ( defined $_[0] && ref $_[0] eq 'HASH' ) {
46047bea 36 Class::MOP::class_of($class)->throw_error(
a62dcd43 37 "Single parameters to new() must be a HASH ref",
38 data => $_[0] );
86629f93 39 }
a62dcd43 40 return { %{ $_[0] } };
41 }
8a157bab 42 else {
e606ae5f 43 return {@_};
8a157bab 44 }
fcd84ca9 45}
46
c0e30cf5 47sub BUILDALL {
d03bd989 48 # NOTE: we ask Perl if we even
d44714be 49 # need to do this first, to avoid
50 # extra meta level calls
d03bd989 51 return unless $_[0]->can('BUILD');
fb1e11d5 52 my ($self, $params) = @_;
505033d9 53 foreach my $method (reverse Class::MOP::class_of($self)->find_all_methods_by_name('BUILD')) {
b8f014e7 54 $method->{code}->execute($self, $params);
fb1e11d5 55 }
c0e30cf5 56}
57
58sub DEMOLISHALL {
8955a780 59 my $self = shift;
d9359278 60 my ($in_global_destruction) = @_;
8955a780 61
62 # NOTE: we ask Perl if we even
cb5c79c9 63 # need to do this first, to avoid
8955a780 64 # extra meta level calls
cb5c79c9 65 return unless $self->can('DEMOLISH');
8955a780 66
2bc1eab6 67 my @isa;
68 if ( my $meta = Class::MOP::class_of($self ) ) {
69 @isa = $meta->linearized_isa;
70 } else {
71 # We cannot count on being able to retrieve a previously made
72 # metaclass, _or_ being able to make a new one during global
73 # destruction. However, we should still be able to use mro at
74 # that time (at least tests suggest so ;)
75 my $class_name = ref $self;
76 @isa = @{ mro::get_linear_isa($class_name) }
77 }
78
79 foreach my $class (@isa) {
c063fc8b 80 no strict 'refs';
81 my $demolish = *{"${class}::DEMOLISH"}{CODE};
82 $self->$demolish($in_global_destruction)
83 if defined $demolish;
3a0c064a 84 }
85}
86
d03bd989 87sub DESTROY {
b288593e 88 my $self = shift;
89
90 local $?;
91
f25f9912 92 Try::Tiny::try {
93 $self->DEMOLISHALL(Devel::GlobalDestruction::in_global_destruction);
b288593e 94 }
f25f9912 95 Try::Tiny::catch {
b288593e 96 # Without this, Perl will warn "\t(in cleanup)$@" because of some
97 # bizarre fucked-up logic deep in the internals.
98 no warnings 'misc';
99 die $_;
100 };
101
c989424b 102 return;
c0e30cf5 103}
104
e606ae5f 105# support for UNIVERSAL::DOES ...
106BEGIN {
107 my $does = UNIVERSAL->can("DOES") ? "SUPER::DOES" : "isa";
108 eval 'sub DOES {
109 my ( $self, $class_or_role_name ) = @_;
110 return $self->'.$does.'($class_or_role_name)
111 || $self->does($class_or_role_name);
112 }';
113}
114
d03bd989 115# new does() methods will be created
50bc108b 116# as appropiate see Moose::Meta::Role
0677220d 117sub does {
bdabd620 118 my ($self, $role_name) = @_;
66d33a5c 119 my $meta = Class::MOP::class_of($self);
be05faea 120 (defined $role_name)
d9de4f04 121 || $meta->throw_error("You must supply a role name to does()");
bdabd620 122 foreach my $class ($meta->class_precedence_list) {
587e457d 123 my $m = $meta->initialize($class);
d03bd989 124 return 1
125 if $m->can('does_role') && $m->does_role($role_name);
bdabd620 126 }
d03bd989 127 return 0;
0677220d 128}
ef333f17 129
d03bd989 130sub dump {
f742dfef 131 my $self = shift;
132 require Data::Dumper;
1a386a6c 133 local $Data::Dumper::Maxdepth = shift if @_;
f742dfef 134 Data::Dumper::Dumper $self;
135}
136
fcd84ca9 1371;
138
139__END__
140
141=pod
142
143=head1 NAME
144
e522431d 145Moose::Object - The base object for Moose
fcd84ca9 146
fcd84ca9 147=head1 DESCRIPTION
148
080e3614 149This class is the default base class for all Moose-using classes. When
150you C<use Moose> in this class, your class will inherit from this
151class.
6ba6d68c 152
080e3614 153It provides a default constructor and destructor, which run the
154C<BUILDALL> and C<DEMOLISHALL> methods respectively.
155
156You don't actually I<need> to inherit from this in order to use Moose,
157but it makes it easier to take advantage of all of Moose's features.
6ba6d68c 158
fcd84ca9 159=head1 METHODS
160
161=over 4
162
080e3614 163=item B<< Moose::Object->new(%params) >>
164
165This method calls C<< $class->BUILDARGS(@_) >>, and then creates a new
166instance of the appropriate class. Once the instance is created, it
167calls C<< $instance->BUILDALL($params) >>.
fcd84ca9 168
080e3614 169=item B<< Moose::Object->BUILDARGS(%params) >>
6ba6d68c 170
080e3614 171The default implementation of this method accepts a hash or hash
172reference of named parameters. If it receives a single argument that
173I<isn't> a hash reference it throws an error.
fcd84ca9 174
080e3614 175You can override this method in your class to handle other types of
176options passed to the constructor.
e606ae5f 177
080e3614 178This method should always return a hash reference of named options.
e606ae5f 179
080e3614 180=item B<< $object->BUILDALL($params) >>
e522431d 181
080e3614 182This method will call every C<BUILD> method in the inheritance
183hierarchy, starting with the most distant parent class and ending with
184the object's class.
c0e30cf5 185
080e3614 186The C<BUILD> method will be passed the hash reference returned by
187C<BUILDARGS>.
e522431d 188
080e3614 189=item B<< $object->DEMOLISHALL >>
c0e30cf5 190
080e3614 191This will call every C<DEMOLISH> method in the inheritance hierarchy,
192starting with the object's class and ending with the most distant
c3fdacda 193parent. C<DEMOLISHALL> and C<DEMOLISH> will receive a boolean
194indicating whether or not we are currently in global destruction.
e522431d 195
080e3614 196=item B<< $object->does($role_name) >>
ef333f17 197
080e3614 198This returns true if the object does the given role.
02a0fb52 199
e606ae5f 200=item B<DOES ($class_or_role_name)>
201
080e3614 202This is a a Moose role-aware implementation of L<UNIVERSAL/DOES>.
e606ae5f 203
080e3614 204This is effectively the same as writing:
e606ae5f 205
080e3614 206 $object->does($name) || $object->isa($name)
f742dfef 207
080e3614 208This method will work with Perl 5.8, which did not implement
209C<UNIVERSAL::DOES>.
f742dfef 210
080e3614 211=item B<< $object->dump($maxdepth) >>
f742dfef 212
080e3614 213This is a handy utility for C<Data::Dumper>ing an object. By default,
214the maximum depth is 1, to avoid making a mess.
f742dfef 215
fcd84ca9 216=back
217
218=head1 BUGS
219
d03bd989 220All complex software has bugs lurking in it, and this module is no
fcd84ca9 221exception. If you find a bug please either email me, or add the bug
222to cpan-RT.
223
fcd84ca9 224=head1 AUTHOR
225
226Stevan Little E<lt>stevan@iinteractive.comE<gt>
227
228=head1 COPYRIGHT AND LICENSE
229
2840a3b2 230Copyright 2006-2009 by Infinity Interactive, Inc.
fcd84ca9 231
232L<http://www.iinteractive.com>
233
234This library is free software; you can redistribute it and/or modify
d03bd989 235it under the same terms as Perl itself.
fcd84ca9 236
9922fa6f 237=cut