support traits/metaclasses in clone_and_inherit_whatever_id_dont_remember_the_name_of...
[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
0677220d 10use Carp 'confess';
11
7c60f730 12our $VERSION = '0.12';
d44714be 13our $AUTHORITY = 'cpan:STEVAN';
fcd84ca9 14
15sub new {
2c0cbef7 16 my $class = shift;
8a157bab 17 my %params;
18 if (scalar @_ == 1) {
86629f93 19 if (defined $_[0]) {
20 (ref($_[0]) eq 'HASH')
21 || confess "Single parameters to new() must be a HASH ref";
22 %params = %{$_[0]};
23 }
8a157bab 24 }
25 else {
26 %params = @_;
27 }
fb1e11d5 28 my $self = $class->meta->new_object(%params);
29 $self->BUILDALL(\%params);
30 return $self;
fcd84ca9 31}
32
c0e30cf5 33sub BUILDALL {
d44714be 34 # NOTE: we ask Perl if we even
35 # need to do this first, to avoid
36 # extra meta level calls
fb1e11d5 37 return unless $_[0]->can('BUILD');
38 my ($self, $params) = @_;
39 foreach my $method (reverse $self->meta->find_all_methods_by_name('BUILD')) {
40 $method->{code}->($self, $params);
41 }
c0e30cf5 42}
43
44sub DEMOLISHALL {
d44714be 45 # NOTE: we ask Perl if we even
46 # need to do this first, to avoid
47 # extra meta level calls
fb1e11d5 48 return unless $_[0]->can('DEMOLISH');
49 my $self = shift;
7c60f730 50 {
51 local $@;
52 foreach my $method ($self->meta->find_all_methods_by_name('DEMOLISH')) {
53 $method->{code}->($self);
54 }
fb1e11d5 55 }
c0e30cf5 56}
57
58sub DESTROY { goto &DEMOLISHALL }
59
ef333f17 60# new does() methods will be created
61# as approiate see Moose::Meta::Role
0677220d 62sub does {
bdabd620 63 my ($self, $role_name) = @_;
0677220d 64 (defined $role_name)
65 || confess "You much supply a role name to does()";
bdabd620 66 my $meta = $self->meta;
67 foreach my $class ($meta->class_precedence_list) {
587e457d 68 my $m = $meta->initialize($class);
bdabd620 69 return 1
587e457d 70 if $m->can('does_role') && $m->does_role($role_name);
bdabd620 71 }
72 return 0;
0677220d 73}
ef333f17 74
f742dfef 75# RANT:
76# Cmon, how many times have you written
77# the following code while debugging:
78#
79# use Data::Dumper;
80# warn Dumper \%thing;
81#
82# It can get seriously annoying, so why
83# not just do this ...
84sub dump {
85 my $self = shift;
86 require Data::Dumper;
1a386a6c 87 local $Data::Dumper::Maxdepth = shift if @_;
f742dfef 88 Data::Dumper::Dumper $self;
89}
90
fcd84ca9 911;
92
93__END__
94
95=pod
96
97=head1 NAME
98
e522431d 99Moose::Object - The base object for Moose
fcd84ca9 100
fcd84ca9 101=head1 DESCRIPTION
102
6ba6d68c 103This serves as the base object for all Moose classes. Every
104effort will be made to ensure that all classes which C<use Moose>
105will inherit from this class. It provides a default constructor
106and destructor, which run all the BUILD and DEMOLISH methods in
107the class tree.
108
109You don't actually I<need> to inherit from this in order to
110use Moose though. It is just here to make life easier.
111
fcd84ca9 112=head1 METHODS
113
114=over 4
115
116=item B<meta>
117
6ba6d68c 118This will return the metaclass associated with the given class.
119
fcd84ca9 120=item B<new>
121
e522431d 122This will create a new instance and call C<BUILDALL>.
123
c0e30cf5 124=item B<BUILDALL>
125
d7f17ebb 126This will call every C<BUILD> method in the inheritance hierarchy,
127and pass it a hash-ref of the the C<%params> passed to C<new>.
e522431d 128
c0e30cf5 129=item B<DEMOLISHALL>
130
e522431d 131This will call every C<DEMOLISH> method in the inheritance hierarchy.
132
ef333f17 133=item B<does ($role_name)>
134
02a0fb52 135This will check if the invocant's class C<does> a given C<$role_name>.
136This is similar to C<isa> for object, but it checks the roles instead.
137
f742dfef 138=item B<dump ($maxdepth)>
139
140Cmon, how many times have you written the following code while debugging:
141
142 use Data::Dumper;
143 warn Dumper $obj;
144
145It can get seriously annoying, so why not just use this.
146
fcd84ca9 147=back
148
149=head1 BUGS
150
151All complex software has bugs lurking in it, and this module is no
152exception. If you find a bug please either email me, or add the bug
153to cpan-RT.
154
fcd84ca9 155=head1 AUTHOR
156
157Stevan Little E<lt>stevan@iinteractive.comE<gt>
158
159=head1 COPYRIGHT AND LICENSE
160
778db3ac 161Copyright 2006-2008 by Infinity Interactive, Inc.
fcd84ca9 162
163L<http://www.iinteractive.com>
164
165This library is free software; you can redistribute it and/or modify
166it under the same terms as Perl itself.
167
9922fa6f 168=cut