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