bump version to 0.65
[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
30350cb4 10our $VERSION = '0.65';
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(@_);
17 my $self = $class->meta->new_object($params);
18 $self->BUILDALL($params);
19 return $self;
20}
21
22sub BUILDARGS {
23 my $class = shift;
8a157bab 24 if (scalar @_ == 1) {
86629f93 25 if (defined $_[0]) {
26 (ref($_[0]) eq 'HASH')
3ce51439 27 || $class->meta->throw_error("Single parameters to new() must be a HASH ref", data => $_[0]);
e606ae5f 28 return {%{$_[0]}};
29 }
30 else {
31 return {}; # FIXME this is compat behavior, but is it correct?
86629f93 32 }
e606ae5f 33 }
8a157bab 34 else {
e606ae5f 35 return {@_};
8a157bab 36 }
fcd84ca9 37}
38
c0e30cf5 39sub BUILDALL {
d44714be 40 # NOTE: we ask Perl if we even
41 # need to do this first, to avoid
42 # extra meta level calls
fb1e11d5 43 return unless $_[0]->can('BUILD');
44 my ($self, $params) = @_;
45 foreach my $method (reverse $self->meta->find_all_methods_by_name('BUILD')) {
b8f014e7 46 $method->{code}->execute($self, $params);
fb1e11d5 47 }
c0e30cf5 48}
49
50sub DEMOLISHALL {
3a0c064a 51 my $self = shift;
52 foreach my $method ($self->meta->find_all_methods_by_name('DEMOLISH')) {
b8f014e7 53 $method->{code}->execute($self);
3a0c064a 54 }
55}
56
57sub DESTROY {
d44714be 58 # NOTE: we ask Perl if we even
59 # need to do this first, to avoid
60 # extra meta level calls
3a0c064a 61 return unless $_[0]->can('DEMOLISH');
62 # if we have an exception here ...
ca0e380d 63 if ($@) {
64 # localize the $@ ...
65 local $@;
3a0c064a 66 # run DEMOLISHALL ourselves, ...
ca0e380d 67 $_[0]->DEMOLISHALL;
3a0c064a 68 # and return ...
69 return;
70 }
ca0e380d 71 # otherwise it is normal destruction
f93f7be9 72 $_[0]->DEMOLISHALL;
c0e30cf5 73}
74
e606ae5f 75# support for UNIVERSAL::DOES ...
76BEGIN {
77 my $does = UNIVERSAL->can("DOES") ? "SUPER::DOES" : "isa";
78 eval 'sub DOES {
79 my ( $self, $class_or_role_name ) = @_;
80 return $self->'.$does.'($class_or_role_name)
81 || $self->does($class_or_role_name);
82 }';
83}
84
ef333f17 85# new does() methods will be created
50bc108b 86# as appropiate see Moose::Meta::Role
0677220d 87sub does {
bdabd620 88 my ($self, $role_name) = @_;
bdabd620 89 my $meta = $self->meta;
be05faea 90 (defined $role_name)
91 || $meta->throw_error("You much supply a role name to does()");
bdabd620 92 foreach my $class ($meta->class_precedence_list) {
587e457d 93 my $m = $meta->initialize($class);
bdabd620 94 return 1
587e457d 95 if $m->can('does_role') && $m->does_role($role_name);
bdabd620 96 }
97 return 0;
0677220d 98}
ef333f17 99
f742dfef 100# RANT:
101# Cmon, how many times have you written
102# the following code while debugging:
103#
104# use Data::Dumper;
105# warn Dumper \%thing;
106#
107# It can get seriously annoying, so why
108# not just do this ...
109sub dump {
110 my $self = shift;
111 require Data::Dumper;
1a386a6c 112 local $Data::Dumper::Maxdepth = shift if @_;
f742dfef 113 Data::Dumper::Dumper $self;
114}
115
fcd84ca9 1161;
117
118__END__
119
120=pod
121
122=head1 NAME
123
e522431d 124Moose::Object - The base object for Moose
fcd84ca9 125
fcd84ca9 126=head1 DESCRIPTION
127
6ba6d68c 128This serves as the base object for all Moose classes. Every
129effort will be made to ensure that all classes which C<use Moose>
130will inherit from this class. It provides a default constructor
131and destructor, which run all the BUILD and DEMOLISH methods in
132the class tree.
133
134You don't actually I<need> to inherit from this in order to
135use Moose though. It is just here to make life easier.
136
fcd84ca9 137=head1 METHODS
138
139=over 4
140
141=item B<meta>
142
6ba6d68c 143This will return the metaclass associated with the given class.
144
fcd84ca9 145=item B<new>
146
e606ae5f 147This will call C<BUILDARGS>, create a new instance and call C<BUILDALL>.
148
149=item B<BUILDARGS>
150
151This method processes an argument list into a hash reference. It is used by
152C<new>.
e522431d 153
c0e30cf5 154=item B<BUILDALL>
155
d7f17ebb 156This will call every C<BUILD> method in the inheritance hierarchy,
157and pass it a hash-ref of the the C<%params> passed to C<new>.
e522431d 158
c0e30cf5 159=item B<DEMOLISHALL>
160
e522431d 161This will call every C<DEMOLISH> method in the inheritance hierarchy.
162
ef333f17 163=item B<does ($role_name)>
164
02a0fb52 165This will check if the invocant's class C<does> a given C<$role_name>.
166This is similar to C<isa> for object, but it checks the roles instead.
167
e606ae5f 168=item B<DOES ($class_or_role_name)>
169
170A Moose Role aware implementation of L<UNIVERSAL/DOES>.
171
172C<DOES> is equivalent to C<isa> or C<does>.
173
f742dfef 174=item B<dump ($maxdepth)>
175
176Cmon, how many times have you written the following code while debugging:
177
178 use Data::Dumper;
179 warn Dumper $obj;
180
181It can get seriously annoying, so why not just use this.
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
778db3ac 197Copyright 2006-2008 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