ignore generated recipes
[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
dc39b850 10our $VERSION = '0.70';
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;
cb5c79c9 52 # NOTE: we ask Perl if we even
53 # need to do this first, to avoid
54 # extra meta level calls
55 return unless $self->can('DEMOLISH');
3a0c064a 56 foreach my $method ($self->meta->find_all_methods_by_name('DEMOLISH')) {
b8f014e7 57 $method->{code}->execute($self);
3a0c064a 58 }
59}
60
61sub DESTROY {
3a0c064a 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 100sub dump {
101 my $self = shift;
102 require Data::Dumper;
1a386a6c 103 local $Data::Dumper::Maxdepth = shift if @_;
f742dfef 104 Data::Dumper::Dumper $self;
105}
106
fcd84ca9 1071;
108
109__END__
110
111=pod
112
113=head1 NAME
114
e522431d 115Moose::Object - The base object for Moose
fcd84ca9 116
fcd84ca9 117=head1 DESCRIPTION
118
6ba6d68c 119This serves as the base object for all Moose classes. Every
120effort will be made to ensure that all classes which C<use Moose>
121will inherit from this class. It provides a default constructor
122and destructor, which run all the BUILD and DEMOLISH methods in
123the class tree.
124
125You don't actually I<need> to inherit from this in order to
126use Moose though. It is just here to make life easier.
127
fcd84ca9 128=head1 METHODS
129
130=over 4
131
132=item B<meta>
133
6ba6d68c 134This will return the metaclass associated with the given class.
135
fcd84ca9 136=item B<new>
137
e606ae5f 138This will call C<BUILDARGS>, create a new instance and call C<BUILDALL>.
139
140=item B<BUILDARGS>
141
142This method processes an argument list into a hash reference. It is used by
143C<new>.
e522431d 144
c0e30cf5 145=item B<BUILDALL>
146
d7f17ebb 147This will call every C<BUILD> method in the inheritance hierarchy,
148and pass it a hash-ref of the the C<%params> passed to C<new>.
e522431d 149
c0e30cf5 150=item B<DEMOLISHALL>
151
e522431d 152This will call every C<DEMOLISH> method in the inheritance hierarchy.
153
ef333f17 154=item B<does ($role_name)>
155
02a0fb52 156This will check if the invocant's class C<does> a given C<$role_name>.
157This is similar to C<isa> for object, but it checks the roles instead.
158
e606ae5f 159=item B<DOES ($class_or_role_name)>
160
161A Moose Role aware implementation of L<UNIVERSAL/DOES>.
162
163C<DOES> is equivalent to C<isa> or C<does>.
164
f742dfef 165=item B<dump ($maxdepth)>
166
6549b0d1 167C'mon, how many times have you written the following code while debugging:
f742dfef 168
169 use Data::Dumper;
170 warn Dumper $obj;
171
172It can get seriously annoying, so why not just use this.
173
fcd84ca9 174=back
175
176=head1 BUGS
177
178All complex software has bugs lurking in it, and this module is no
179exception. If you find a bug please either email me, or add the bug
180to cpan-RT.
181
fcd84ca9 182=head1 AUTHOR
183
184Stevan Little E<lt>stevan@iinteractive.comE<gt>
185
186=head1 COPYRIGHT AND LICENSE
187
2840a3b2 188Copyright 2006-2009 by Infinity Interactive, Inc.
fcd84ca9 189
190L<http://www.iinteractive.com>
191
192This library is free software; you can redistribute it and/or modify
193it under the same terms as Perl itself.
194
9922fa6f 195=cut